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