Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ProcessConfirmationNotification
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
3 / 3
10
100.00% covered (success)
100.00%
1 / 1
 readResponse
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
2
 writeNotification
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
4
 testProcessData
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3/**
4 * @package ZMS API
5 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
6 **/
7
8namespace BO\Zmsapi;
9
10use BO\Slim\Render;
11use BO\Mellon\Validator;
12use BO\Zmsdb\Notification as Query;
13use BO\Zmsdb\Config;
14use BO\Zmsdb\Process;
15use BO\Zmsdb\Department;
16
17/**
18 * @SuppressWarnings(Coupling)
19 */
20class ProcessConfirmationNotification extends BaseController
21{
22    /**
23     * @SuppressWarnings(Param)
24     * @return String
25     */
26    public function readResponse(
27        \Psr\Http\Message\RequestInterface $request,
28        \Psr\Http\Message\ResponseInterface $response,
29        array $args
30    ) {
31        $input = Validator::input()->isJson()->assertValid()->getValue();
32        $process = new \BO\Zmsentities\Process($input);
33        $process->testValid();
34        $this->testProcessData($process);
35        $process = (new Process())->readEntity($process->id, $process->authKey);
36
37        \BO\Zmsdb\Connection\Select::getWriteConnection();
38
39        $notification = $this->writeNotification($process);
40        $message = Response\Message::create($request);
41        $message->data = ($notification->hasId()) ? $notification : null;
42
43        $response = Render::withLastModified($response, time(), '0');
44        $response = Render::withJson($response, $message, 200);
45        return $response;
46    }
47
48    protected static function writeNotification(\BO\Zmsentities\Process $process)
49    {
50        $config = (new Config())->readEntity();
51        $department = (new Department())->readByScopeId($process->scope['id']);
52        $notification = (new \BO\Zmsentities\Notification())
53            ->toResolvedEntity(
54                $process,
55                $config,
56                $department,
57                ($process->isWithAppointment()) ? 'appointment' : 'confirmed'
58            );
59        $notification->testValid();
60        if ($process->scope->hasNotificationEnabled() && $process->getFirstClient()->hasTelephone()) {
61            $notification = (new Query())->writeInQueue($notification, \App::$now, false);
62            \App::$log->debug("Send notification", [$notification]);
63        }
64        return $notification;
65    }
66
67    protected function testProcessData(\BO\Zmsentities\Process $process)
68    {
69        $authCheck = (new Process())->readAuthKeyByProcessId($process->id);
70        if (! $authCheck) {
71            throw new Exception\Process\ProcessNotFound();
72        } elseif ($authCheck['authKey'] != $process->authKey && $authCheck['authName'] != $process->authKey) {
73            throw new Exception\Process\AuthKeyMatchFailed();
74        }
75    }
76}