Lines
94.44%
34 / 36
Functions and Methods
50.00%
2 / 4
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| ProcessDeleteMail | 94.44% | 34 / 36 | 50.00% | 2 / 4 | 11.02 | 0.00% | 0 / 1 | |||
| readResponse | 93.75% | 15 / 16 | 0.00% | 0 / 1 | 2.00 | |||||
| writeMail | 100.00% | 12 / 12 | 100.00% | 1 / 1 | 3 | |||||
| testProcessAccess | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 3 | |||||
| testEmailRequired | 66.66% | 2 / 3 | 0.00% | 0 / 1 | 3.33 | |||||
| 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\Mellon\Validator; | |
| 12 | use BO\Zmsbackend\Mail\Service\Mail; | |
| 13 | use BO\Zmsbackend\Config\Service\Config; | |
| 14 | use BO\Zmsbackend\Process\Service\Process as ProcessRepository; | |
| 15 | use BO\Zmsbackend\Department\Service\Department as DepartmentRepository; | |
| 16 | use BO\Zmsentities\Process; | |
| 17 | ||
| 18 | /** | |
| 19 | * @SuppressWarnings(Coupling) | |
| 20 | */ | |
| 21 | class ProcessDeleteMail extends \BO\Zmsbackend\Api\BaseController | |
| 22 | { | |
| 23 | /** | |
| 24 | * @SuppressWarnings(Param) | |
| 25 | * @return string | |
| 26 | */ | |
| 27 | #[\Override] | |
| 28 | public function readResponse( | |
| 29 | \Psr\Http\Message\RequestInterface $request, | |
| 30 | \Psr\Http\Message\ResponseInterface $response, | |
| 31 | array $args | |
| 32 | ) { | |
| 33 | $input = Validator::input()->isJson()->assertValid()->getValue(); | |
| 34 | $process = new Process($input); | |
| 35 | $initiator = Validator::param('initiator')->isString()->getValue(); | |
| 36 | ||
| 37 | $process->testValid(); | |
| 38 | $this->testProcessAccess($process); | |
| 39 | $process = (new ProcessRepository())->readEntity($process->id, $process->authKey); | |
| 40 | if (!$process instanceof Process) { | |
| 41 | throw new \BO\Zmsbackend\Process\Exception\ProcessNotFound(); | |
| 42 | } | |
| 43 | $this->testEmailRequired($process); | |
| 44 | ||
| 45 | \BO\Zmsbackend\Connection\Select::getWriteConnection(); | |
| 46 | ||
| 47 | $mail = $this->writeMail($process, $initiator); | |
| 48 | ||
| 49 | $message = \BO\Zmsbackend\Api\Response\Message::create($request); | |
| 50 | $message->data = $mail; | |
| 51 | ||
| 52 | $response = Render::withLastModified($response, time(), '0'); | |
| 53 | $response = Render::withJson($response, $message, 200); | |
| 54 | return $response; | |
| 55 | } | |
| 56 | ||
| 57 | protected static function writeMail(Process $process, $initiator = null) | |
| 58 | { | |
| 59 | $config = (new \BO\Zmsbackend\Config\Service\Config())->readEntity(); | |
| 60 | $department = (new DepartmentRepository())->readByScopeId($process->scope->id); | |
| 61 | $collection = ProcessConfirmationMail::getProcessListOverview($process, $config); | |
| 62 | ||
| 63 | $mail = (new \BO\Zmsentities\Mail()) | |
| 64 | ->setTemplateProvider(new \BO\Zmsbackend\Helper\MailTemplateProvider($process)) | |
| 65 | ->toResolvedEntity($collection, $config, 'deleted', $initiator) | |
| 66 | ->withDepartment($department); | |
| 67 | $mail->testValid(); | |
| 68 | if ($process->getFirstClient()->hasEmail() && $process->scope->hasEmailFrom()) { | |
| 69 | $mail = (new \BO\Zmsbackend\Mail\Service\Mail())->writeInQueue($mail, \App::$now, false); | |
| 70 | \App::$log->debug("Send mail", [$mail]); | |
| 71 | } | |
| 72 | return $mail; | |
| 73 | } | |
| 74 | ||
| 75 | /** | |
| 76 | * @return void | |
| 77 | */ | |
| 78 | protected function testProcessAccess(Process $process) | |
| 79 | { | |
| 80 | $authCheck = (new ProcessRepository())->readAuthKeyByProcessId($process->getId()); | |
| 81 | if (! $authCheck) { | |
| 82 | throw new \BO\Zmsbackend\Process\Exception\ProcessNotFound(); | |
| 83 | } | |
| 84 | if ($authCheck['authKey'] !== $process->authKey) { | |
| 85 | throw new \BO\Zmsbackend\Process\Exception\AuthKeyMatchFailed(); | |
| 86 | } | |
| 87 | } | |
| 88 | ||
| 89 | /** | |
| 90 | * @return void | |
| 91 | */ | |
| 92 | protected function testEmailRequired(Process $process) | |
| 93 | { | |
| 94 | if ( | |
| 95 | $process->toProperty()->scope->preferences->client->emailRequired->get() && | |
| 96 | ! $process->getFirstClient()->hasEmail() | |
| 97 | ) { | |
| 98 | throw new \BO\Zmsbackend\Process\Exception\EmailRequired(); | |
| 99 | } | |
| 100 | } | |
| 101 | } |