Skip to content

Commit de31952

Browse files
committed
[WIP] endpoint Event/Withdraw , ApiResponse, Event/SubmissionWithdrawnEvent refactored
1 parent 4f7fe3a commit de31952

File tree

4 files changed

+56
-35
lines changed

4 files changed

+56
-35
lines changed

Classes/Api/Event/Withdraw.php

+31-29
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@
1616

1717
use Cpsit\EventSubmission\Domain\Model\ApiResponseInterface;
1818
use Cpsit\EventSubmission\Domain\Model\Job;
19+
use Cpsit\EventSubmission\Domain\Repository\JobRepository;
1920
use Cpsit\EventSubmission\Event\SubmissionWithdrawnEvent;
21+
use Cpsit\EventSubmission\Exceptions\InvalidArgumentException;
2022
use Cpsit\EventSubmission\Factory\ApiResponse\ApiResponseFactoryFactory;
2123
use Cpsit\EventSubmission\Factory\ApiResponse\ApiResponseFactoryInterface;
22-
use Cpsit\EventSubmission\Factory\Job\JobFactory;
2324
use Cpsit\EventSubmission\Type\SubmissionStatus;
24-
use Nng\Nnhelpers\Utilities\Db;
2525
use Nng\Nnrestapi\Annotations as Api;
2626
use Nng\Nnrestapi\Api\AbstractApi;
2727
use Psr\EventDispatcher\EventDispatcherInterface;
28+
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
2829

2930
/**
3031
* Event API end point for PUT method
@@ -35,10 +36,12 @@ final class Withdraw extends AbstractApi implements EventApiInterface
3536
{
3637
public const RESPONSE_NAME = 'EventWithdrawApiResponse';
3738
protected ApiResponseFactoryInterface $responseFactory;
39+
public const MESSAGE_INVALID_ARGUMENT = 'Invalid or missing argument %s.';
3840

3941
public function __construct(
40-
ApiResponseFactoryFactory $apiResponseFactoryFactory,
41-
private readonly Db $db,
42+
ApiResponseFactoryFactory $apiResponseFactoryFactory,
43+
private readonly JobRepository $jobRepository,
44+
private readonly PersistenceManager $persistenceManager,
4245
private readonly EventDispatcherInterface $eventDispatcher,
4346
)
4447
{
@@ -83,35 +86,27 @@ public function __construct(
8386
public function withdraw(): array
8487
{
8588
$arguments = $this->request->getArguments();
86-
$responseCode = ApiResponseInterface::EVENT_WITHDRAW_ERROR;
87-
$id = $arguments[self::PARAMETER_ID];
88-
$responseData = [
89-
'id' => $id,
90-
];
89+
$responseCode = ApiResponseInterface::EVENT_WITHDRAW_ERROR;
9190

92-
// find job by identifier
93-
// Note: job could be approved and imported already
94-
$job = $this->db->findOneByValues(
95-
Job::TABLE_NAME,
96-
[
97-
Job::FIELD_UUID => $id,
98-
]
99-
);
100-
101-
// update job status
102-
if (!empty($job)) {
103-
$data = [
104-
Job::FIELD_APPROVED => 0,
105-
Job::FIELD_STATUS => SubmissionStatus::WITHDRAWN,
106-
];
107-
108-
$where = [
109-
Job::FIELD_UID => $job[Job::FIELD_UID],
91+
try {
92+
if(!is_array($arguments) || empty($arguments[self::PARAMETER_ID])) {
93+
$message = sprintf(self::MESSAGE_INVALID_ARGUMENT, self::PARAMETER_ID);
94+
throw new InvalidArgumentException($message, $responseCode);
95+
}
96+
$id = $arguments[self::PARAMETER_ID];
97+
$responseData = [
98+
'id' => $id,
11099
];
111-
$result = $this->db->update(Job::TABLE_NAME, $data, $where);
112100

101+
$job = $this->jobRepository->findOneByUuid($id);
113102

114-
if ($result === 1) {
103+
// update job status
104+
// Note: job could be approved and imported already
105+
if ($job instanceof Job) {
106+
$job->setStatus(SubmissionStatus::WITHDRAWN)
107+
->setApproved(false);
108+
$this->jobRepository->update($job);
109+
$this->persistenceManager->persistAll();
115110
$responseCode = ApiResponseInterface::EVENT_WITHDRAW_SUCCESS;
116111
$this->eventDispatcher->dispatch(
117112
new SubmissionWithdrawnEvent(
@@ -120,6 +115,13 @@ public function withdraw(): array
120115
)
121116
);
122117
}
118+
119+
} catch (\Exception $exception) {
120+
return $this->responseFactory
121+
->errorResponse()
122+
->setMessage($exception->getMessage())
123+
->setCode($exception->getCode())
124+
->toArray();
123125
}
124126

125127
return $this->responseFactory

Classes/Domain/Model/ApiResponse.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,30 @@ public function getCode(): int
2828
return $this->code;
2929
}
3030

31-
public function setCode(int $code): void
31+
public function setCode(int $code): self
3232
{
3333
$this->code = $code;
34+
return $this;
3435
}
3536

3637
public function getData(): array
3738
{
3839
return $this->data;
3940
}
4041

41-
public function setData(array $data): void
42+
public function setData(array $data): self
4243
{
4344
$this->data = $data;
45+
return $this;
4446
}
4547

4648
/**
4749
* @param string $message
4850
*/
49-
public function setMessage(string $message): void
51+
public function setMessage(string $message): self
5052
{
5153
$this->message = $message;
54+
return $this;
5255
}
5356

5457
public function getMessage(): string

Classes/Event/SubmissionWithdrawnEvent.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Cpsit\EventSubmission\Event;
44

55
/*
6-
* This file is part of the iki_event_approval project.
6+
* This file is part of the event_submission project.
77
*
88
* It is free software; you can redistribute it and/or modify it under
99
* the terms of the GNU General Public License, either version 2
@@ -15,11 +15,11 @@
1515
final readonly class SubmissionWithdrawnEvent
1616
{
1717
public function __construct(
18-
private array $job,
18+
private Job $job,
1919
private array $settings
2020
) {}
2121

22-
public function getJob(): array
22+
public function getJob(): Job
2323
{
2424
return $this->job;
2525
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Cpsit\EventSubmission\Exceptions;
4+
5+
/*
6+
* This file is part of the iki_event_approval project.
7+
*
8+
* It is free software; you can redistribute it and/or modify it under
9+
* the terms of the GNU General Public License, either version 2
10+
* of the License, or any later version.
11+
*/
12+
13+
class InvalidArgumentException extends \Exception
14+
{
15+
16+
}

0 commit comments

Comments
 (0)