Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.33% |
28 / 30 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ProcessDeleteMail | |
93.33% |
28 / 30 |
|
66.67% |
2 / 3 |
8.02 | |
0.00% |
0 / 1 |
| readResponse | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
1 | |||
| writeMail | |
83.33% |
10 / 12 |
|
0.00% |
0 / 1 |
3.04 | |||
| testProcessData | |
100.00% |
6 / 6 |
|
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\Mellon\Validator; |
| 12 | use BO\Zmsdb\Mail as Query; |
| 13 | use BO\Zmsdb\Config; |
| 14 | use BO\Zmsdb\Process as ProcessRepository; |
| 15 | use BO\Zmsdb\Department as DepartmentRepository; |
| 16 | use BO\Zmsentities\Process; |
| 17 | |
| 18 | /** |
| 19 | * @SuppressWarnings(Coupling) |
| 20 | */ |
| 21 | class ProcessDeleteMail 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 | $input = Validator::input()->isJson()->assertValid()->getValue(); |
| 33 | $process = new Process($input); |
| 34 | $initiator = Validator::param('initiator')->isString()->getValue(); |
| 35 | |
| 36 | $process->testValid(); |
| 37 | $this->testProcessData($process); |
| 38 | |
| 39 | \BO\Zmsdb\Connection\Select::getWriteConnection(); |
| 40 | |
| 41 | $mail = $this->writeMail($process, $initiator); |
| 42 | |
| 43 | $message = Response\Message::create($request); |
| 44 | $message->data = $mail; |
| 45 | |
| 46 | $response = Render::withLastModified($response, time(), '0'); |
| 47 | $response = Render::withJson($response, $message, 200); |
| 48 | return $response; |
| 49 | } |
| 50 | |
| 51 | protected static function writeMail(Process $process, $initiator = null) |
| 52 | { |
| 53 | $config = (new Config())->readEntity(); |
| 54 | $department = (new DepartmentRepository())->readByScopeId($process->scope['id']); |
| 55 | $collection = ProcessConfirmationMail::getProcessListOverview($process, $config); |
| 56 | |
| 57 | $mail = (new \BO\Zmsentities\Mail()) |
| 58 | ->setTemplateProvider(new \BO\Zmsdb\Helper\MailTemplateProvider($process)) |
| 59 | ->toResolvedEntity($collection, $config, 'deleted', $initiator) |
| 60 | ->withDepartment($department); |
| 61 | $mail->testValid(); |
| 62 | if ($process->getFirstClient()->hasEmail() && $process->scope->hasEmailFrom()) { |
| 63 | $mail = (new \BO\Zmsdb\Mail())->writeInQueue($mail, \App::$now, false); |
| 64 | \App::$log->debug("Send mail", [$mail]); |
| 65 | } |
| 66 | return $mail; |
| 67 | } |
| 68 | |
| 69 | protected function testProcessData($process) |
| 70 | { |
| 71 | $authCheck = (new ProcessRepository())->readAuthKeyByProcessId($process->getId()); |
| 72 | if (! $authCheck) { |
| 73 | throw new Exception\Process\ProcessNotFound(); |
| 74 | } elseif ( |
| 75 | $process->toProperty()->scope->preferences->client->emailRequired->get() && |
| 76 | ! $process->getFirstClient()->hasEmail() |
| 77 | ) { |
| 78 | throw new Exception\Process\EmailRequired(); |
| 79 | } |
| 80 | } |
| 81 | } |