Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
67.69% |
44 / 65 |
|
63.64% |
7 / 11 |
CRAP | |
0.00% |
0 / 1 |
| Ticketprinter | |
67.69% |
44 / 65 |
|
63.64% |
7 / 11 |
38.32 | |
0.00% |
0 / 1 |
| __construct | |
58.33% |
7 / 12 |
|
0.00% |
0 / 1 |
2.29 | |||
| getEntity | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getScopeId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getOrganisation | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setScopeId | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| getHashFromRequest | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| getByHash | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| writeNewWithHash | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| setRequestParameters | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getAssembledEntity | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
| readOrganisation | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * |
| 5 | * @package Zmsticketprinter |
| 6 | * @copyright BerlinOnline Stadtportal GmbH & Co. KG |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | namespace BO\Zmsticketprinter\Helper; |
| 11 | |
| 12 | use BO\Zmsentities\Ticketprinter as Entity; |
| 13 | use BO\Zmsentities\Organisation; |
| 14 | use BO\Zmsclient\Ticketprinter as TicketprinterClient; |
| 15 | use BO\Zmsticketprinter\Exception\OrganisationNotFound as OrganisationNotFoundException; |
| 16 | use Psr\Http\Message\RequestInterface; |
| 17 | |
| 18 | class 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 | \App::$log->warning('Error in getByHash, creating new organisation hash', [ |
| 42 | 'exception' => $e->getMessage(), |
| 43 | ]); |
| 44 | $entity = $this->writeNewWithHash($request, $entity); |
| 45 | } |
| 46 | |
| 47 | $this->entity = \App::$http->readPostResult('/ticketprinter/', $entity)->getEntity(); |
| 48 | } |
| 49 | |
| 50 | public function getEntity(): Entity |
| 51 | { |
| 52 | return $this->entity; |
| 53 | } |
| 54 | |
| 55 | public function getScopeId(): int |
| 56 | { |
| 57 | return $this->scopeId; |
| 58 | } |
| 59 | |
| 60 | public function getOrganisation(): Organisation |
| 61 | { |
| 62 | return $this->organisation; |
| 63 | } |
| 64 | |
| 65 | protected function setScopeId(array $args, RequestInterface $request) |
| 66 | { |
| 67 | $validator = $request->getAttribute('validator'); |
| 68 | $scopeId = $validator->getParameter('scopeId')->isNumber()->getValue(); |
| 69 | if (isset($args['scopeId'])) { |
| 70 | $scopeId = $validator::value($args['scopeId'])->isNumber()->getValue(); |
| 71 | } |
| 72 | return $scopeId; |
| 73 | } |
| 74 | |
| 75 | public static function getHashFromRequest(RequestInterface $request): string |
| 76 | { |
| 77 | $cookies = $request->getCookieParams(); |
| 78 | $hash = TicketprinterClient::getHash(); |
| 79 | if (array_key_exists(TicketprinterClient::HASH_COOKIE_NAME, $cookies) && ! $hash) { |
| 80 | $hash = $cookies[TicketprinterClient::HASH_COOKIE_NAME]; |
| 81 | } |
| 82 | return $hash; |
| 83 | } |
| 84 | |
| 85 | protected function getByHash(string $hash, Entity $entity): Entity |
| 86 | { |
| 87 | $entityWithHash = \App::$http->readGetResult('/ticketprinter/' . $hash . '/')->getEntity(); |
| 88 | $entity->hash = $entityWithHash->hash; |
| 89 | $entity->enabled = $entityWithHash->enabled; |
| 90 | return $entity; |
| 91 | } |
| 92 | |
| 93 | protected function writeNewWithHash(RequestInterface $request, Entity $entity): Entity |
| 94 | { |
| 95 | if (null === $this->organisation) { |
| 96 | throw new OrganisationNotFoundException(); |
| 97 | } |
| 98 | $entityWithHash = \App::$http->readGetResult( |
| 99 | '/organisation/' . $this->organisation->getId() . '/hash/', |
| 100 | ['name' => (isset($this->requestParams['name'])) ? $this->requestParams['name'] : ''] |
| 101 | )->getEntity(); |
| 102 | TicketprinterClient::setHash($entityWithHash->hash, $request); |
| 103 | $entity->hash = $entityWithHash->hash; |
| 104 | $entity->enabled = $entityWithHash->enabled; |
| 105 | return $entity; |
| 106 | } |
| 107 | |
| 108 | protected function setRequestParameters(RequestInterface $request): void |
| 109 | { |
| 110 | $validator = $request->getAttribute('validator'); |
| 111 | $this->requestParams = $validator->getParameter('ticketprinter')->isArray()->getValue(); |
| 112 | } |
| 113 | |
| 114 | protected function getAssembledEntity(): Entity |
| 115 | { |
| 116 | $entity = new Entity($this->requestParams); |
| 117 | if ($this->scopeId) { |
| 118 | $entity = new Entity(); |
| 119 | $entity->buttonlist = 's' . $this->scopeId; |
| 120 | $entity->customText1 = trim((string)($this->requestParams['customText1'] ?? '')); |
| 121 | $entity->customText2 = trim((string)($this->requestParams['customText2'] ?? '')); |
| 122 | } |
| 123 | $entity = $entity->toStructuredButtonList(); |
| 124 | return $entity; |
| 125 | } |
| 126 | |
| 127 | protected function readOrganisation(): Organisation |
| 128 | { |
| 129 | $organisation = null; |
| 130 | $ticketprinter = $this->getAssembledEntity(); |
| 131 | if ($this->scopeId) { |
| 132 | $organisation = \App::$http->readGetResult( |
| 133 | '/scope/' . $this->scopeId . '/organisation/', |
| 134 | ['resolveReferences' => 2] |
| 135 | )->getEntity(); |
| 136 | } |
| 137 | $nextButton = array_shift($ticketprinter->buttons); |
| 138 | while (! $organisation && $nextButton) { |
| 139 | if (in_array($nextButton['type'], ['scope', 'request'])) { |
| 140 | $organisation = \App::$http->readGetResult( |
| 141 | '/scope/' . $nextButton['scope']['id'] . '/organisation/', |
| 142 | ['resolveReferences' => 2] |
| 143 | )->getEntity(); |
| 144 | } |
| 145 | $nextButton = array_shift($ticketprinter->buttons); |
| 146 | } |
| 147 | return $organisation; |
| 148 | } |
| 149 | } |