Lines
95.65%
66 / 69
Functions and Methods
50.00%
2 / 4
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| ProcessDeleteMail | 95.65% | 66 / 69 | 50.00% | 2 / 4 | 21 | 0.00% | 0 / 1 | |||
| readResponse | 95.91% | 47 / 49 | 0.00% | 0 / 1 | 12 | |||||
| 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\Cluster\Service\Cluster as ClusterService; | |
| 15 | use BO\Zmsbackend\Scope\Exception\ScopeNotFound; | |
| 16 | use BO\Zmsbackend\Scope\Service\Scope as ScopeService; | |
| 17 | use BO\Zmsbackend\Process\Service\Process as ProcessRepository; | |
| 18 | use BO\Zmsbackend\Department\Service\Department as DepartmentRepository; | |
| 19 | use BO\Zmsentities\Process; | |
| 20 | ||
| 21 | /** | |
| 22 | * @SuppressWarnings(Coupling) | |
| 23 | */ | |
| 24 | class ProcessDeleteMail extends \BO\Zmsbackend\Api\BaseController | |
| 25 | { | |
| 26 | /** | |
| 27 | * @SuppressWarnings(Param) | |
| 28 | * @return string | |
| 29 | */ | |
| 30 | #[\Override] | |
| 31 | public function readResponse( | |
| 32 | \Psr\Http\Message\RequestInterface $request, | |
| 33 | \Psr\Http\Message\ResponseInterface $response, | |
| 34 | array $args | |
| 35 | ) { | |
| 36 | $input = Validator::input()->isJson()->assertValid()->getValue(); | |
| 37 | $requestProcess = new Process($input); | |
| 38 | $initiator = Validator::param('initiator')->isString()->getValue(); | |
| 39 | ||
| 40 | $requestProcess->testValid(); | |
| 41 | $this->testProcessAccess($requestProcess); | |
| 42 | ||
| 43 | $process = (new ProcessRepository())->readEntity( | |
| 44 | $requestProcess->id, | |
| 45 | $requestProcess->authKey | |
| 46 | ); | |
| 47 | ||
| 48 | if (!$process instanceof Process) { | |
| 49 | throw new \BO\Zmsbackend\Process\Exception\ProcessNotFound(); | |
| 50 | } | |
| 51 | ||
| 52 | $this->testEmailRequired($process); | |
| 53 | ||
| 54 | // Ursprüngliche Buchungsdaten ausschließlich für die Absagemail. | |
| 55 | $mailProcess = clone $process; | |
| 56 | $originalAppointment = $requestProcess->getAppointments()->getFirst(); | |
| 57 | ||
| 58 | $hasOriginalDate = $originalAppointment !== null && | |
| 59 | (int) $originalAppointment->date > 0; | |
| 60 | ||
| 61 | $hasOriginalRequests = isset($input['requests']); | |
| 62 | ||
| 63 | $originalScopeId = (int) $requestProcess->getScopeId(); | |
| 64 | $hasDifferentOriginalScope = $originalScopeId > 0 && | |
| 65 | $originalScopeId !== (int) $process->getScopeId(); | |
| 66 | ||
| 67 | if ( | |
| 68 | 'admin' === $initiator && | |
| 69 | ($hasOriginalDate || $hasOriginalRequests || $hasDifferentOriginalScope) | |
| 70 | ) { | |
| 71 | $workstation = (new \BO\Zmsbackend\Helper\User($request)) | |
| 72 | ->checkPermissions('appointment'); | |
| 73 | ||
| 74 | if ($hasOriginalDate) { | |
| 75 | $mailProcess->getFirstAppointment()->date = | |
| 76 | $originalAppointment->date; | |
| 77 | } | |
| 78 | ||
| 79 | if ($hasOriginalRequests) { | |
| 80 | $mailProcess->requests = clone $requestProcess->getRequests(); | |
| 81 | } | |
| 82 | ||
| 83 | if ($hasDifferentOriginalScope) { | |
| 84 | $originalScope = (new ScopeService())->readEntity($originalScopeId, 1); | |
| 85 | if ($originalScope === null) { | |
| 86 | throw new ScopeNotFound(); | |
| 87 | } | |
| 88 | ||
| 89 | $cluster = (new ClusterService())->readByScopeId( | |
| 90 | $workstation->getScope()->getId(), | |
| 91 | 1 | |
| 92 | ); | |
| 93 | $workstation->validateProcessScopeAccess( | |
| 94 | $workstation->getScopeList($cluster), | |
| 95 | $requestProcess | |
| 96 | ); | |
| 97 | ||
| 98 | $mailProcess->scope = clone $originalScope; | |
| 99 | } | |
| 100 | } | |
| 101 | ||
| 102 | \BO\Zmsbackend\Connection\Select::getWriteConnection(); | |
| 103 | ||
| 104 | $mail = $this->writeMail($mailProcess, $initiator); | |
| 105 | ||
| 106 | $message = \BO\Zmsbackend\Api\Response\Message::create($request); | |
| 107 | $message->data = $mail; | |
| 108 | ||
| 109 | $response = Render::withLastModified($response, time(), '0'); | |
| 110 | $response = Render::withJson($response, $message, 200); | |
| 111 | ||
| 112 | return $response; | |
| 113 | } | |
| 114 | ||
| 115 | protected static function writeMail(Process $process, ?string $initiator = null) | |
| 116 | { | |
| 117 | $config = (new \BO\Zmsbackend\Config\Service\Config())->readEntity(); | |
| 118 | $department = (new DepartmentRepository())->readByScopeId($process->scope->id); | |
| 119 | $collection = ProcessConfirmationMail::getProcessListOverview($process, $config); | |
| 120 | ||
| 121 | $mail = (new \BO\Zmsentities\Mail()) | |
| 122 | ->setTemplateProvider(new \BO\Zmsbackend\Helper\MailTemplateProvider($process)) | |
| 123 | ->toResolvedEntity($collection, $config, 'deleted', $initiator) | |
| 124 | ->withDepartment($department); | |
| 125 | $mail->testValid(); | |
| 126 | if ($process->getFirstClient()->hasEmail() && $process->scope->hasEmailFrom()) { | |
| 127 | $mail = (new \BO\Zmsbackend\Mail\Service\Mail())->writeInQueue($mail, \App::$now, false); | |
| 128 | \App::$log->debug("Send mail", [$mail]); | |
| 129 | } | |
| 130 | return $mail; | |
| 131 | } | |
| 132 | ||
| 133 | /** | |
| 134 | * @return void | |
| 135 | */ | |
| 136 | protected function testProcessAccess(Process $process) | |
| 137 | { | |
| 138 | $authCheck = (new ProcessRepository())->readAuthKeyByProcessId($process->getId()); | |
| 139 | if (! $authCheck) { | |
| 140 | throw new \BO\Zmsbackend\Process\Exception\ProcessNotFound(); | |
| 141 | } | |
| 142 | if ($authCheck['authKey'] !== $process->authKey) { | |
| 143 | throw new \BO\Zmsbackend\Process\Exception\AuthKeyMatchFailed(); | |
| 144 | } | |
| 145 | } | |
| 146 | ||
| 147 | /** | |
| 148 | * @return void | |
| 149 | */ | |
| 150 | protected function testEmailRequired(Process $process) | |
| 151 | { | |
| 152 | if ( | |
| 153 | $process->toProperty()->scope->preferences->client->emailRequired->get() && | |
| 154 | ! $process->getFirstClient()->hasEmail() | |
| 155 | ) { | |
| 156 | throw new \BO\Zmsbackend\Process\Exception\EmailRequired(); | |
| 157 | } | |
| 158 | } | |
| 159 | } |