Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 49 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ProcessPreconfirmationMail | |
0.00% |
0 / 49 |
|
0.00% |
0 / 4 |
240 | |
0.00% |
0 / 1 |
| readResponse | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
| writeMail | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
| testProcessData | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
42 | |||
| getProcessListOverview | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
12 | |||
| 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\Mellon\Validator; |
| 12 | use BO\Zmsdb\Process as ProcessRepository; |
| 13 | use BO\Zmsdb\Config; |
| 14 | use BO\Zmsdb\Department; |
| 15 | use BO\Zmsentities\Process; |
| 16 | use BO\Zmsentities\Collection\ProcessList as Collection; |
| 17 | |
| 18 | /** |
| 19 | * @SuppressWarnings(Coupling) |
| 20 | */ |
| 21 | class ProcessPreconfirmationMail extends BaseController |
| 22 | { |
| 23 | /** |
| 24 | * @SuppressWarnings(Param) |
| 25 | * @return string |
| 26 | */ |
| 27 | public function readResponse( |
| 28 | \Psr\Http\Message\RequestInterface $request, |
| 29 | \Psr\Http\Message\ResponseInterface $response, |
| 30 | array $args |
| 31 | ) { |
| 32 | \BO\Zmsdb\Connection\Select::setCriticalReadSession(); |
| 33 | $input = Validator::input()->isJson()->assertValid()->getValue(); |
| 34 | $process = new Process($input); |
| 35 | $process->testValid(); |
| 36 | $this->testProcessData($process); |
| 37 | $process = (new ProcessRepository())->readEntity($process->id, $process->authKey); |
| 38 | $mail = $this->writeMail($process); |
| 39 | $message = Response\Message::create($request); |
| 40 | $message->data = ($mail->hasId()) ? $mail : null; |
| 41 | |
| 42 | $response = Render::withLastModified($response, time(), '0'); |
| 43 | $response = Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode()); |
| 44 | return $response; |
| 45 | } |
| 46 | |
| 47 | protected static function writeMail(Process $process) |
| 48 | { |
| 49 | $config = (new Config())->readEntity(); |
| 50 | $department = (new Department())->readByScopeId($process->scope['id']); |
| 51 | $status = ($process->isWithAppointment()) ? 'preconfirmed' : 'queued'; |
| 52 | $collection = static::getProcessListOverview($process, $config); |
| 53 | |
| 54 | $mail = (new \BO\Zmsentities\Mail()) |
| 55 | ->setTemplateProvider(new \BO\Zmsdb\Helper\MailTemplateProvider($process)) |
| 56 | ->toResolvedEntity($collection, $config, $status) |
| 57 | ->withDepartment($department); |
| 58 | $mail->testValid(); |
| 59 | if ($process->getFirstClient()->hasEmail() && $process->scope->hasEmailFrom()) { |
| 60 | $mail = (new \BO\Zmsdb\Mail())->writeInQueue($mail, \App::$now, false); |
| 61 | \App::$log->debug("Send mail", [$mail]); |
| 62 | } |
| 63 | return $mail; |
| 64 | } |
| 65 | |
| 66 | protected function testProcessData($process) |
| 67 | { |
| 68 | $authCheck = (new ProcessRepository())->readAuthKeyByProcessId($process->id); |
| 69 | if (! $authCheck) { |
| 70 | throw new Exception\Process\ProcessNotFound(); |
| 71 | } elseif ($authCheck['authKey'] != $process->authKey && $authCheck['authName'] != $process->authKey) { |
| 72 | throw new Exception\Process\AuthKeyMatchFailed(); |
| 73 | } elseif ( |
| 74 | $process->toProperty()->scope->preferences->client->emailRequired->get() && |
| 75 | ! $process->getFirstClient()->hasEmail() |
| 76 | ) { |
| 77 | throw new Exception\Process\EmailRequired(); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | public static function getProcessListOverview($process, $config) |
| 82 | { |
| 83 | $collection = (new Collection())->addEntity($process); |
| 84 | if ( |
| 85 | in_array( |
| 86 | getenv('ZMS_ENV'), |
| 87 | explode(',', $config->getPreference('appointments', 'enableSummaryByMail')) |
| 88 | ) && $process->getFirstClient()->hasEmail() |
| 89 | ) { |
| 90 | $processList = (new ProcessRepository())->readListByMailAndStatusList( |
| 91 | $process->getFirstClient()->email, |
| 92 | [ |
| 93 | Process::STATUS_PRECONFIRMED, |
| 94 | Process::STATUS_PICKUP |
| 95 | ], |
| 96 | 2, |
| 97 | 50 |
| 98 | ); |
| 99 | |
| 100 | //add list of found processes without the main process |
| 101 | $collection->addList($processList->withOutProcessId($process->getId())); |
| 102 | } |
| 103 | return $collection; |
| 104 | } |
| 105 | } |