Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.73% covered (success)
92.73%
51 / 55
81.82% covered (warning)
81.82%
9 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
Ticketprinter
92.73% covered (success)
92.73%
51 / 55
81.82% covered (warning)
81.82%
9 / 11
28.30
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
70.00% covered (warning)
70.00%
7 / 10
0.00% covered (danger)
0.00%
0 / 1
3.24
 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 $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($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()
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, $button)
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            $button[$button['type']]['id'] = $value;
108        }
109
110        if ('request' == $button['type']) {
111            $button['scope'] = [
112                'id' => explode('-', $value)[0]
113            ];
114        }
115
116        return $button;
117    }
118
119    protected function getButtonValue($string, $type)
120    {
121        $value = (in_array($type, ['l', 'r'])) ?
122            Validator::value(substr($string, 1))->isString() :
123            Validator::value(substr($string, 1))->isNumber();
124        return $value->getValue();
125    }
126
127    protected function getButtonType($string): string
128    {
129        return substr($string, 0, 1);
130    }
131
132    protected function getExternalLinkData($value, $button)
133    {
134        if (preg_match("/\[([^\]]*)\]/", $value, $matches)) {
135            $data = explode('|', $matches[1]);
136            $button['url'] = (isset($data[0])) ? $data[0] : '';
137            $button['name'] = (isset($data[1])) ? $data[1] : "Information";
138        }
139        return $button;
140    }
141}