Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.86% covered (success)
92.86%
52 / 56
81.82% covered (warning)
81.82%
9 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
Ticketprinter
92.86% covered (success)
92.86%
52 / 56
81.82% covered (warning)
81.82%
9 / 11
28.29
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
90.00% covered (success)
90.00%
9 / 10
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 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    #[\Override]
22    public function getDefaults()
23    {
24        return [
25            'enabled' => false,
26            'reload' => 30
27        ];
28    }
29
30    public function getHashWith($organisiationId)
31    {
32        $this->hash = $organisiationId . "abcdefghijklmnopqrstuvwxyz";
33        //$this->hash = $organisiationId . bin2hex(openssl_random_pseudo_bytes(16));
34        return $this;
35    }
36
37    public function isEnabled()
38    {
39        return $this->enabled;
40    }
41
42    public function toStructuredButtonList()
43    {
44        $ticketprinter = clone $this;
45        $ticketprinter->buttons = array();
46        if (! $ticketprinter->toProperty()->buttonlist->isAvailable() || '' === trim((string) $ticketprinter->buttonlist)) {
47            return $ticketprinter;
48        }
49
50        $buttonList = explode(',', (string) $ticketprinter->buttonlist);
51        foreach ($buttonList as $string) {
52            $button = array();
53            $button = $ticketprinter->getValidButtonWithType($string);
54            $ticketprinter->buttons[] = $ticketprinter->getButtonData($string, $button);
55        }
56        return $ticketprinter;
57    }
58
59    public function getScopeList()
60    {
61        $scopeList = new Collection\ScopeList();
62        if ($this->toProperty()->buttons->isAvailable()) {
63            foreach ($this->buttons as $button) {
64                if (in_array($button['type'], ['scope', 'request'])) {
65                    $scopeList->addEntity(new Scope($button['scope']));
66                }
67            }
68        }
69        return $scopeList;
70    }
71
72    public function getClusterList()
73    {
74        $clusterList = new Collection\ClusterList();
75        if ($this->toProperty()->buttons->isAvailable()) {
76            foreach ($this->buttons as $button) {
77                if ('cluster' == $button['type']) {
78                    $clusterList->addEntity(new Cluster($button['cluster']));
79                }
80            }
81        }
82        return $clusterList;
83    }
84
85    protected function getValidButtonWithType($string)
86    {
87        $type = $this->getButtonType($string);
88        $value = $this->getButtonValue($string, $type);
89        if (! Property::__keyExists($type, $this->allowedButtonTypes) || ! $value) {
90            throw new Exception\TicketprinterUnvalidButton();
91        }
92        return array(
93            'type' => $this->allowedButtonTypes[$type]
94        );
95    }
96
97    protected function getButtonData($string, $button)
98    {
99        $value = $this->getButtonValue($string, $this->getButtonType($string));
100        if ('link' == $button['type']) {
101            $button = $this->getExternalLinkData($value, $button);
102        } else {
103            $button['url'] = '/' . $button['type'] . '/' . $value . '/';
104            $button[$button['type']]['id'] = $value;
105        }
106
107        if ('request' == $button['type']) {
108            $button['scope'] = [
109                'id' => explode('-', $value)[0]
110            ];
111        }
112
113        return $button;
114    }
115
116    protected function getButtonValue($string, $type)
117    {
118        $value = (in_array($type, ['l', 'r'])) ?
119            Validator::value(substr($string, 1))->isString() :
120            Validator::value(substr($string, 1))->isNumber();
121        return $value->getValue();
122    }
123
124    protected function getButtonType($string)
125    {
126        return substr($string, 0, 1);
127    }
128
129    protected function getExternalLinkData($value, $button)
130    {
131        if (preg_match("/\[([^\]]*)\]/", $value, $matches)) {
132            $data = explode('|', $matches[1]);
133            $button['url'] = (isset($data[0])) ? $data[0] : '';
134            $button['name'] = (isset($data[1])) ? $data[1] : "Information";
135        }
136        return $button;
137    }
138}