Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
74.51% |
38 / 51 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ProcessDelete | |
74.51% |
38 / 51 |
|
50.00% |
2 / 4 |
20.24 | |
0.00% |
0 / 1 |
| readResponse | |
100.00% |
25 / 25 |
|
100.00% |
1 / 1 |
5 | |||
| writeMails | |
8.33% |
1 / 12 |
|
0.00% |
0 / 1 |
16.32 | |||
| writeCancelSearchHistory | |
80.00% |
8 / 10 |
|
0.00% |
0 / 1 |
4.13 | |||
| testProcessData | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @package ZMS API |
| 5 | * @copyright BerlinOnline Stadtportal GmbH & Co. KG |
| 6 | **/ |
| 7 | |
| 8 | namespace BO\Zmsbackend\Process\Api; |
| 9 | |
| 10 | use BO\Slim\Render; |
| 11 | use BO\Zmsbackend\Process\Service\Process; |
| 12 | use BO\Zmsbackend\ProcessSearchHistory\Service\ProcessSearchHistory as HistoryService; |
| 13 | use BO\Zmsbackend\Mail\Service\Mail; |
| 14 | use BO\Zmsbackend\Config\Service\Config; |
| 15 | use BO\Mellon\Validator; |
| 16 | |
| 17 | /** |
| 18 | * @SuppressWarnings(CouplingBetweenObjects) |
| 19 | */ |
| 20 | class ProcessDelete extends \BO\Zmsbackend\Api\BaseController |
| 21 | { |
| 22 | /** |
| 23 | * @SuppressWarnings(Param) |
| 24 | * @return \Psr\Http\Message\ResponseInterface |
| 25 | */ |
| 26 | #[\Override] |
| 27 | public function readResponse( |
| 28 | \Psr\Http\Message\RequestInterface $request, |
| 29 | \Psr\Http\Message\ResponseInterface $response, |
| 30 | array $args |
| 31 | ) { |
| 32 | $workstation = (new \BO\Zmsbackend\Helper\User($request))->readWorkstation(); |
| 33 | \BO\Zmsbackend\Connection\Select::getWriteConnection(); |
| 34 | $process = (new \BO\Zmsbackend\Process\Service\Process())->readEntity($args['id'], new \BO\Zmsbackend\Helper\NoAuth(), 2); |
| 35 | $this->testProcessData($process, $args['authKey']); |
| 36 | $this->writeCancelSearchHistory($process, HistoryService::STATUS_CANCELLED_BY_CITIZEN); |
| 37 | if ('reserved' == $process->status) { |
| 38 | if (!(new \BO\Zmsbackend\Process\Service\Process())->writeBlockedEntity($process, false, $workstation->getUseraccount())) { |
| 39 | throw new \BO\Zmsbackend\Process\Exception\ProcessDeleteFailed(); // @codeCoverageIgnore |
| 40 | } |
| 41 | $processDeleted = $process; |
| 42 | } else { |
| 43 | (new \BO\Zmsbackend\OverviewCalendar\Service\OverviewCalendar())->perform( |
| 44 | \BO\Zmsbackend\Calendar\Repository\OverviewCalendar::CANCEL_BY_PROCESS, |
| 45 | ['process_id' => (int)$process->id] |
| 46 | ); |
| 47 | |
| 48 | $processDeleted = (new \BO\Zmsbackend\Process\Service\Process())->writeCanceledEntity( |
| 49 | $args['id'], |
| 50 | $args['authKey'], |
| 51 | null, |
| 52 | $workstation->getUseraccount() |
| 53 | ); |
| 54 | if (!$processDeleted || !$processDeleted->hasId()) { |
| 55 | throw new \BO\Zmsbackend\Process\Exception\ProcessDeleteFailed(); // @codeCoverageIgnore |
| 56 | } |
| 57 | } |
| 58 | $this->writeMails($request, $process); |
| 59 | $message = \BO\Zmsbackend\Api\Response\Message::create($request); |
| 60 | $message->data = $processDeleted; |
| 61 | |
| 62 | $response = Render::withLastModified($response, time(), '0'); |
| 63 | $response = Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode()); |
| 64 | return $response; |
| 65 | } |
| 66 | |
| 67 | protected function writeMails(\Psr\Http\Message\RequestInterface $request, \BO\Zmsentities\Process|null $process): void |
| 68 | { |
| 69 | if ($process->hasScopeAdmin() && $process->sendAdminMailOnDeleted() && $process->getStatus() !== 'blocked') { |
| 70 | $authority = $request->getUri()->getAuthority(); |
| 71 | $validator = $request->getAttribute('validator'); |
| 72 | $initiator = $validator->getParameter('initiator') |
| 73 | ->isString() |
| 74 | ->setDefault("$authority API-User") |
| 75 | ->getValue(); |
| 76 | $config = (new \BO\Zmsbackend\Config\Service\Config())->readEntity(); |
| 77 | $mail = (new \BO\Zmsentities\Mail()) |
| 78 | ->setTemplateProvider(new \BO\Zmsbackend\Helper\MailTemplateProvider($process)) |
| 79 | ->toResolvedEntity($process, $config, 'deleted', $initiator); |
| 80 | (new \BO\Zmsbackend\Mail\Service\Mail())->writeInQueueWithAdmin($mail, \App::$now); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | protected function writeCancelSearchHistory( |
| 85 | \BO\Zmsentities\Process $process, |
| 86 | string $historyStatus |
| 87 | ): void { |
| 88 | if (!$process->hasProcessCredentials()) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | $now = class_exists('\App') && isset(\App::$now) |
| 93 | ? \App::$now |
| 94 | : new \DateTimeImmutable(); |
| 95 | |
| 96 | (new HistoryService())->writeHistoryEntry( |
| 97 | $process, |
| 98 | $historyStatus, |
| 99 | $now |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @return void |
| 105 | */ |
| 106 | protected function testProcessData(\BO\Zmsentities\Process|null $process, $authKey) |
| 107 | { |
| 108 | if (!$process) { |
| 109 | throw new \BO\Zmsbackend\Process\Exception\ProcessNotFound(); |
| 110 | } |
| 111 | if ($process['authKey'] !== $authKey) { |
| 112 | throw new \BO\Zmsbackend\Process\Exception\AuthKeyMatchFailed(); |
| 113 | } |
| 114 | } |
| 115 | } |