Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.67% covered (success)
91.67%
55 / 60
81.82% covered (warning)
81.82%
9 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
Ticketprinter
91.67% covered (success)
91.67%
55 / 60
81.82% covered (warning)
81.82%
9 / 11
30.52
0.00% covered (danger)
0.00%
0 / 1
 getDefaults
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getHashWith
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 isEnabled
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toStructuredButtonList
88.89% covered (warning)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
4.02
 getScopeList
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 getClusterList
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 getValidButtonWithType
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 getButtonData
73.33% covered (warning)
73.33%
11 / 15
0.00% covered (danger)
0.00%
0 / 1
5.47
 getButtonValue
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getButtonType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getExternalLinkData
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3namespace BO\Zmsentities;
4
5use BO\Mellon\Validator;
6use BO\Zmsentities\Helper\Property;
7
8class Ticketprinter extends Schema\Entity
9{
10    public const string PRIMARY = 'hash';
11
12    public static $schema = "ticketprinter.json";
13
14    protected array $allowedButtonTypes = [
15        's' => 'scope',
16        /*'c' => 'cluster',*/
17        'l' => 'link',
18        'r' => 'request'
19    ];
20
21    /**
22     * @return (false|int)[]
23     *
24     */
25    #[\Override]
26    public function getDefaults()
27    {
28        return [
29            'enabled' => false,
30            'reload' => 30
31        ];
32    }
33
34    public function getHashWith(mixed $organisiationId): static
35    {
36        $this->hash = $organisiationId . "abcdefghijklmnopqrstuvwxyz";
37        //$this->hash = $organisiationId . bin2hex(openssl_random_pseudo_bytes(16));
38        return $this;
39    }
40
41    public function isEnabled(): mixed
42    {
43        return $this->enabled;
44    }
45
46    public function toStructuredButtonList(): static
47    {
48        $ticketprinter = clone $this;
49        $ticketprinter->buttons = array();
50        if (! $ticketprinter->toProperty()->buttonlist->isAvailable() || '' === trim((string) $ticketprinter->buttonlist)) {
51            return $ticketprinter;
52        }
53
54        $buttonList = explode(',', (string) $ticketprinter->buttonlist);
55        foreach ($buttonList as $string) {
56            $button = $ticketprinter->getValidButtonWithType($string);
57            $ticketprinter->buttons[] = $ticketprinter->getButtonData($string, $button);
58        }
59        return $ticketprinter;
60    }
61
62    public function getScopeList(): Collection\ScopeList
63    {
64        $scopeList = new Collection\ScopeList();
65        if ($this->toProperty()->buttons->isAvailable()) {
66            foreach ($this->buttons as $button) {
67                if (in_array($button['type'], ['scope', 'request'])) {
68                    $scopeList->addEntity(new Scope($button['scope']));
69                }
70            }
71        }
72        return $scopeList;
73    }
74
75    public function getClusterList(): Collection\ClusterList
76    {
77        $clusterList = new Collection\ClusterList();
78        if ($this->toProperty()->buttons->isAvailable()) {
79            foreach ($this->buttons as $button) {
80                if ('cluster' == $button['type']) {
81                    $clusterList->addEntity(new Cluster($button['cluster']));
82                }
83            }
84        }
85        return $clusterList;
86    }
87
88    protected function getValidButtonWithType(string $string): array
89    {
90        $type = $this->getButtonType($string);
91        $value = $this->getButtonValue($string, $type);
92        if (! Property::__keyExists($type, $this->allowedButtonTypes) || ! $value) {
93            throw new Exception\TicketprinterUnvalidButton();
94        }
95        return array(
96            'type' => $this->allowedButtonTypes[$type]
97        );
98    }
99
100    protected function getButtonData(string $string, array $button): mixed
101    {
102        $value = $this->getButtonValue($string, $this->getButtonType($string));
103        if ('link' == $button['type']) {
104            $button = $this->getExternalLinkData($value, $button);
105        } else {
106            $button['url'] = '/' . $button['type'] . '/' . $value . '/';
107            $buttonType = $button['type'];
108            $nested = [];
109            if (isset($button[$buttonType]) && is_array($button[$buttonType])) {
110                $nested = $button[$buttonType];
111            }
112            $nested['id'] = $value;
113            $button[$buttonType] = $nested;
114        }
115
116        if ('request' == $button['type']) {
117            $button['scope'] = [
118                'id' => explode('-', $value)[0]
119            ];
120        }
121
122        return $button;
123    }
124
125    protected function getButtonValue(string $string, string $type): mixed
126    {
127        $value = (in_array($type, ['l', 'r'])) ?
128            Validator::value(substr($string, 1))->isString() :
129            Validator::value(substr($string, 1))->isNumber();
130        return $value->getValue();
131    }
132
133    protected function getButtonType(string $string): string
134    {
135        return substr($string, 0, 1);
136    }
137
138    protected function getExternalLinkData(mixed $value, mixed $button): mixed
139    {
140        if (preg_match("/\[([^\]]*)\]/", $value, $matches)) {
141            $data = explode('|', $matches[1]);
142            $button['url'] = (isset($data[0])) ? $data[0] : '';
143            $button['name'] = (isset($data[1])) ? $data[1] : "Information";
144        }
145        return $button;
146    }
147}