Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
70.13% covered (warning)
70.13%
54 / 77
53.85% covered (warning)
53.85%
7 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
Ticketprinter
70.13% covered (warning)
70.13%
54 / 77
53.85% covered (warning)
53.85%
7 / 13
51.41
0.00% covered (danger)
0.00%
0 / 1
 __construct
64.29% covered (warning)
64.29%
9 / 14
0.00% covered (danger)
0.00%
0 / 1
3.41
 getEntity
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getScopeId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getOrganisation
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setScopeId
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getHashFromRequest
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 getByHash
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 writeNewWithHash
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
 setRequestParameters
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getAssembledEntity
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 readOrganisation
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
5
 readOrganisationByScopeId
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
3.01
 isMissingScope
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
3.07
1<?php
2
3/**
4 *
5 * @package Zmsticketprinter
6 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
7 *
8 */
9
10namespace BO\Zmsticketprinter\Helper;
11
12use BO\Zmsentities\Ticketprinter as Entity;
13use BO\Zmsentities\Organisation;
14use BO\Zmsclient\Exception as ClientException;
15use BO\Zmsclient\Ticketprinter as TicketprinterClient;
16use BO\Zmsticketprinter\Exception\OrganisationNotFound as OrganisationNotFoundException;
17use Psr\Http\Message\RequestInterface;
18
19class Ticketprinter
20{
21    protected $entity = null;
22
23    protected $scopeId = null;
24
25    protected $organisation = null;
26
27    protected $requestParams = [];
28
29    public function __construct($args, RequestInterface $request)
30    {
31        $this->setRequestParameters($request);
32        $this->scopeId = $this->setScopeId($args, $request);
33        $this->organisation = $this->readOrganisation();
34        if (null === $this->organisation) {
35            throw new OrganisationNotFoundException();
36        }
37        $entity = $this->getAssembledEntity();
38
39        //$hash = static::getHashFromRequest($request);
40        $hash = $this->organisation->id . 'abcdefghijklmnopqrstuvwxyz';
41
42        try {
43            $entity = $this->getByHash($hash, $entity);
44        } catch (\Exception $e) {
45            \App::$log->warning('Error in getByHash, creating new organisation hash', [
46                'exception' => $e->getMessage(),
47            ]);
48            $entity = $this->writeNewWithHash($request, $entity);
49        }
50
51        $this->entity = \App::$http->readPostResult('/ticketprinter/', $entity)->getEntity();
52    }
53
54    public function getEntity(): Entity
55    {
56        return $this->entity;
57    }
58
59    public function getScopeId(): int
60    {
61        return $this->scopeId;
62    }
63
64    public function getOrganisation(): Organisation
65    {
66        return $this->organisation;
67    }
68
69    protected function setScopeId(array $args, RequestInterface $request)
70    {
71        $validator = $request->getAttribute('validator');
72        $scopeId = $validator->getParameter('scopeId')->isNumber()->getValue();
73        if (isset($args['scopeId'])) {
74            $scopeId = $validator::value($args['scopeId'])->isNumber()->getValue();
75        }
76        return $scopeId;
77    }
78
79    public static function getHashFromRequest(RequestInterface $request): string
80    {
81        $cookies = $request->getCookieParams();
82        $hash = TicketprinterClient::getHash();
83        if (array_key_exists(TicketprinterClient::HASH_COOKIE_NAME, $cookies) && ! $hash) {
84            $hash = $cookies[TicketprinterClient::HASH_COOKIE_NAME];
85        }
86        return $hash;
87    }
88
89    protected function getByHash(string $hash, Entity $entity): Entity
90    {
91        $entityWithHash = \App::$http->readGetResult('/ticketprinter/' . $hash . '/')->getEntity();
92        $entity->hash = $entityWithHash->hash;
93        $entity->enabled = $entityWithHash->enabled;
94        return $entity;
95    }
96
97    protected function writeNewWithHash(RequestInterface $request, Entity $entity): Entity
98    {
99        if (null === $this->organisation) {
100            throw new OrganisationNotFoundException();
101        }
102        $entityWithHash = \App::$http->readGetResult(
103            '/organisation/' . $this->organisation->getId() . '/hash/',
104            ['name' => (isset($this->requestParams['name'])) ? $this->requestParams['name'] : '']
105        )->getEntity();
106        TicketprinterClient::setHash($entityWithHash->hash, $request);
107        $entity->hash = $entityWithHash->hash;
108        $entity->enabled = $entityWithHash->enabled;
109        return $entity;
110    }
111
112    protected function setRequestParameters(RequestInterface $request): void
113    {
114        $validator = $request->getAttribute('validator');
115        $this->requestParams = $validator->getParameter('ticketprinter')->isArray()->getValue();
116    }
117
118    protected function getAssembledEntity(): Entity
119    {
120        $entity = new Entity($this->requestParams);
121        if ($this->scopeId) {
122            $entity = new Entity();
123            $entity->buttonlist = 's' . $this->scopeId;
124            $entity->customText1 = trim((string)($this->requestParams['customText1'] ?? ''));
125            $entity->customText2 = trim((string)($this->requestParams['customText2'] ?? ''));
126        }
127        $entity = $entity->toStructuredButtonList();
128        return $entity;
129    }
130
131    protected function readOrganisation(): ?Organisation
132    {
133        $organisation = null;
134        $ticketprinter = $this->getAssembledEntity();
135        if ($this->scopeId) {
136            $organisation = $this->readOrganisationByScopeId($this->scopeId);
137        }
138        $nextButton = array_shift($ticketprinter->buttons);
139        while (! $organisation && $nextButton) {
140            if (in_array($nextButton['type'], ['scope', 'request'])) {
141                $organisation = $this->readOrganisationByScopeId($nextButton['scope']['id']);
142            }
143            $nextButton = array_shift($ticketprinter->buttons);
144        }
145        return $organisation;
146    }
147
148    protected function readOrganisationByScopeId(mixed $scopeId): ?Organisation
149    {
150        try {
151            return \App::$http->readGetResult(
152                '/scope/' . $scopeId . '/organisation/',
153                ['resolveReferences' => 2]
154            )->getEntity();
155        } catch (ClientException $exception) {
156            if (! $this->isMissingScope($exception)) {
157                throw $exception;
158            }
159            \App::$log->warning('Ticketprinter: skip missing scope id', [
160                'scopeId' => $scopeId,
161            ]);
162            return null;
163        }
164    }
165
166    protected function isMissingScope(ClientException $exception): bool
167    {
168        if ((int) $exception->getCode() === 404) {
169            return true;
170        }
171        $template = (string) $exception->template;
172        return str_contains($template, 'ScopeNotFound')
173            || str_contains($template, 'OrganisationNotFound');
174    }
175}