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