Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
68.85% covered (warning)
68.85%
42 / 61
63.64% covered (warning)
63.64%
7 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
Ticketprinter
68.85% covered (warning)
68.85%
42 / 61
63.64% covered (warning)
63.64%
7 / 11
36.63
0.00% covered (danger)
0.00%
0 / 1
 __construct
70.00% covered (warning)
70.00%
7 / 10
0.00% covered (danger)
0.00%
0 / 1
2.11
 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%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 readOrganisation
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
5
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\Ticketprinter as TicketprinterClient;
15use BO\Zmsticketprinter\Exception\OrganisationNotFound as OrganisationNotFoundException;
16use Psr\Http\Message\RequestInterface;
17
18class Ticketprinter
19{
20    protected $entity = null;
21
22    protected $scopeId = null;
23
24    protected $organisation = null;
25
26    protected $requestParams = [];
27
28    public function __construct($args, RequestInterface $request)
29    {
30        $this->setRequestParameters($request);
31        $this->scopeId = $this->setScopeId($args, $request);
32        $this->organisation = $this->readOrganisation();
33        $entity = $this->getAssembledEntity();
34
35        //$hash = static::getHashFromRequest($request);
36        $hash = $this->organisation->id . 'abcdefghijklmnopqrstuvwxyz';
37
38        try {
39            $entity = $this->getByHash($hash, $entity);
40        } catch (\Exception $e) {
41            error_log('Error in getByHash creating new organisation hash: ' . $e->getMessage());
42            $entity = $this->writeNewWithHash($request, $entity);
43        }
44
45        $this->entity = \App::$http->readPostResult('/ticketprinter/', $entity)->getEntity();
46    }
47
48    public function getEntity(): Entity
49    {
50        return $this->entity;
51    }
52
53    public function getScopeId(): int
54    {
55        return $this->scopeId;
56    }
57
58    public function getOrganisation(): Organisation
59    {
60        return $this->organisation;
61    }
62
63    protected function setScopeId(array $args, RequestInterface $request)
64    {
65        $validator = $request->getAttribute('validator');
66        $scopeId = $validator->getParameter('scopeId')->isNumber()->getValue();
67        if (isset($args['scopeId'])) {
68            $scopeId = $validator::value($args['scopeId'])->isNumber()->getValue();
69        }
70        return $scopeId;
71    }
72
73    public static function getHashFromRequest(RequestInterface $request): string
74    {
75        $cookies = $request->getCookieParams();
76        $hash = TicketprinterClient::getHash();
77        if (array_key_exists(TicketprinterClient::HASH_COOKIE_NAME, $cookies) && ! $hash) {
78            $hash = $cookies[TicketprinterClient::HASH_COOKIE_NAME];
79        }
80        return $hash;
81    }
82
83    protected function getByHash(string $hash, Entity $entity): Entity
84    {
85        $entityWithHash = \App::$http->readGetResult('/ticketprinter/' . $hash . '/')->getEntity();
86        $entity->hash = $entityWithHash->hash;
87        $entity->enabled = $entityWithHash->enabled;
88        return $entity;
89    }
90
91    protected function writeNewWithHash(RequestInterface $request, Entity $entity): Entity
92    {
93        if (null === $this->organisation) {
94            throw new OrganisationNotFoundException();
95        }
96        $entityWithHash = \App::$http->readGetResult(
97            '/organisation/' . $this->organisation->getId() . '/hash/',
98            ['name' => (isset($this->requestParams['name'])) ? $this->requestParams['name'] : '']
99        )->getEntity();
100        TicketprinterClient::setHash($entityWithHash->hash, $request);
101        $entity->hash = $entityWithHash->hash;
102        $entity->enabled = $entityWithHash->enabled;
103        return $entity;
104    }
105
106    protected function setRequestParameters(RequestInterface $request): void
107    {
108        $validator = $request->getAttribute('validator');
109        $this->requestParams = $validator->getParameter('ticketprinter')->isArray()->getValue();
110    }
111
112    protected function getAssembledEntity(): Entity
113    {
114        $entity = new Entity($this->requestParams);
115        if ($this->scopeId) {
116            $entity = new Entity();
117            $entity->buttonlist = 's' . $this->scopeId;
118        }
119        $entity = $entity->toStructuredButtonList();
120        return $entity;
121    }
122
123    protected function readOrganisation(): Organisation
124    {
125        $organisation = null;
126        $ticketprinter = $this->getAssembledEntity();
127        if ($this->scopeId) {
128            $organisation = \App::$http->readGetResult(
129                '/scope/' . $this->scopeId . '/organisation/',
130                ['resolveReferences' => 2]
131            )->getEntity();
132        }
133        $nextButton = array_shift($ticketprinter->buttons);
134        while (! $organisation && $nextButton) {
135            if (in_array($nextButton['type'], ['scope', 'request'])) {
136                $organisation = \App::$http->readGetResult(
137                    '/scope/' . $nextButton['scope']['id'] . '/organisation/',
138                    ['resolveReferences' => 2]
139                )->getEntity();
140            }
141            $nextButton = array_shift($ticketprinter->buttons);
142        }
143        return $organisation;
144    }
145}