Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.48% covered (success)
90.48%
57 / 63
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProcessUpdate
90.48% covered (success)
90.48%
57 / 63
0.00% covered (danger)
0.00%
0 / 2
16.22
0.00% covered (danger)
0.00%
0 / 1
 readResponse
91.07% covered (success)
91.07%
51 / 56
0.00% covered (danger)
0.00%
0 / 1
10.07
 testProcessData
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
6.10
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\Zmsdb\Config;
12use BO\Zmsdb\Log;
13use BO\Zmsdb\Mail;
14use BO\Mellon\Validator;
15use BO\Zmsdb\Process;
16
17/**
18 * @SuppressWarnings(Coupling)
19 * @return String
20 */
21class ProcessUpdate extends BaseController
22{
23    /**
24     * @SuppressWarnings(Param)
25     * @SuppressWarnings(Complexity)
26     * @return String
27     */
28    public function readResponse(
29        \Psr\Http\Message\RequestInterface $request,
30        \Psr\Http\Message\ResponseInterface $response,
31        array $args
32    ) {
33        $slotsRequired = Validator::param('slotsRequired')->isNumber()->getValue();
34        $slotType = Validator::param('slotType')->isString()->getValue();
35        $clientKey = Validator::param('clientkey')->isString()->getValue();
36        $initiator = Validator::param('initiator')->isString()->getValue();
37        $resolveReferences = Validator::param('resolveReferences')->isNumber()->setDefault(2)->getValue();
38        $input = Validator::input()->isJson()->assertValid()->getValue();
39        $entity = new \BO\Zmsentities\Process($input);
40        $entity->testValid();
41        $this->testProcessData($entity, ! $initiator);
42
43        \BO\Zmsdb\Connection\Select::setCriticalReadSession();
44        $workstation = (new Helper\User($request))->readWorkstation();
45
46        if ($slotType || $slotsRequired) {
47            $process = Process::init()->updateEntityWithSlots(
48                $entity,
49                \App::$now,
50                $slotType,
51                $slotsRequired,
52                $resolveReferences,
53                $workstation->getUseraccount()
54            );
55            Helper\Matching::testCurrentScopeHasRequest($process);
56        } elseif ($clientKey) {
57            $apiClient = (new \BO\Zmsdb\Apiclient())->readEntity($clientKey);
58            if (!$apiClient || !isset($apiClient->accesslevel) || $apiClient->accesslevel == 'blocked') {
59                throw new Exception\Process\ApiclientInvalid();
60            }
61            $entity->apiclient = $apiClient;
62            $process = (new Process())->updateEntity(
63                $entity,
64                \App::$now,
65                $resolveReferences,
66                null,
67                $workstation->getUseraccount()
68            );
69        } else {
70            $process = (new Process())->updateEntity(
71                $entity,
72                \App::$now,
73                $resolveReferences,
74                null,
75                $workstation->getUseraccount()
76            );
77
78            Log::writeProcessLog(
79                "UPDATE (Process::updateEntity) $process ",
80                Log::ACTION_CALLED,
81                $process,
82                $workstation->getUseraccount()
83            );
84        }
85
86        if ($initiator && $process->hasScopeAdmin() && $process->sendAdminMailOnUpdated()) {
87            $config = (new Config())->readEntity();
88
89            $mail = (new \BO\Zmsentities\Mail())
90                    ->setTemplateProvider(new \BO\Zmsdb\Helper\MailTemplateProvider($process))
91                    ->toResolvedEntity($process, $config, 'updated', $initiator);
92            (new Mail())->writeInQueueWithAdmin($mail);
93        }
94        $message = Response\Message::create($request);
95        $message->data = $process;
96
97        $response = Render::withLastModified($response, time(), '0');
98
99        return Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode());
100    }
101
102    protected function testProcessData($entity, bool $checkMailLimit = true)
103    {
104        $authCheck = (new Process())->readAuthKeyByProcessId($entity->id);
105
106        if ($checkMailLimit && ! (new Process())->isAppointmentAllowedWithSameMail($entity)) {
107            throw new Exception\Process\MoreThanAllowedAppointmentsPerMail();
108        }
109
110        if (! $authCheck) {
111            throw new Exception\Process\ProcessNotFound();
112        } elseif ($authCheck['authKey'] != $entity->authKey && $authCheck['authName'] != $entity->authKey) {
113            throw new Exception\Process\AuthKeyMatchFailed();
114        }
115    }
116}