Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
70.00% covered (warning)
70.00%
28 / 40
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProcessConfirm
70.00% covered (warning)
70.00%
28 / 40
33.33% covered (danger)
33.33%
1 / 3
12.70
0.00% covered (danger)
0.00%
0 / 1
 readResponse
95.65% covered (success)
95.65%
22 / 23
0.00% covered (danger)
0.00%
0 / 1
3
 writeMails
8.33% covered (danger)
8.33%
1 / 12
0.00% covered (danger)
0.00%
0 / 1
9.93
 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\Zmsdb\Process;
12use BO\Zmsdb\Mail;
13use BO\Zmsdb\Config;
14use BO\Mellon\Validator;
15
16/**
17 * @SuppressWarnings(CouplingBetweenObjects)
18 */
19class ProcessConfirm extends BaseController
20{
21    /**
22     * @SuppressWarnings(Param)
23     * @return String
24     */
25    public function readResponse(
26        \Psr\Http\Message\RequestInterface $request,
27        \Psr\Http\Message\ResponseInterface $response,
28        array $args
29    ) {
30        \BO\Zmsdb\Connection\Select::setCriticalReadSession();
31
32        $resolveReferences = Validator::param('resolveReferences')->isNumber()->setDefault(3)->getValue();
33        $input = Validator::input()->isJson()->assertValid()->getValue();
34        $entity = new \BO\Zmsentities\Process($input);
35        $entity->testValid();
36        $this->testProcessData($entity);
37
38        $userAccount = (new Helper\User($request))->readWorkstation()->getUseraccount();
39        $process = (new Process())->readEntity($entity->id, $entity->authKey);
40        if ('preconfirmed' != $process->status && 'reserved' != $process->status) {
41            throw new Exception\Process\ProcessNotPreconfirmedAnymore();
42        }
43
44        $process = (new Process())->updateProcessStatus(
45            $process,
46            'confirmed',
47            \App::$now,
48            $resolveReferences,
49            $userAccount
50        );
51        $this->writeMails($request, $process);
52        $message = Response\Message::create($request);
53        $message->data = $process;
54
55        $response = Render::withLastModified($response, time(), '0');
56        $response = Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode());
57        return $response;
58    }
59    protected function writeMails($request, $process)
60    {
61        if ($process->hasScopeAdmin() && $process->sendAdminMailOnConfirmation()) {
62            $authority = $request->getUri()->getAuthority();
63            $validator = $request->getAttribute('validator');
64            $initiator = $validator->getParameter('initiator')
65                ->isString()
66                ->setDefault("$authority API-User")
67                ->getValue();
68            $config = (new Config())->readEntity();
69            $mail = (new \BO\Zmsentities\Mail())
70            ->setTemplateProvider(new \BO\Zmsdb\Helper\MailTemplateProvider($process))
71            ->toResolvedEntity($process, $config, 'appointment', $initiator);
72            (new Mail())->writeInQueueWithAdmin($mail, \App::$now);
73        }
74    }
75    protected function testProcessData($entity)
76    {
77        $authCheck = (new Process())->readAuthKeyByProcessId($entity->id);
78        if (! $authCheck) {
79            throw new Exception\Process\ProcessNotFound();
80        } elseif ($authCheck['authKey'] != $entity->authKey && $authCheck['authName'] != $entity->authKey) {
81            throw new Exception\Process\AuthKeyMatchFailed();
82        }
83    }
84}