Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
89.58% covered (warning)
89.58%
86 / 96
91.67% covered (success)
91.67%
11 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
Ticketprinter
89.58% covered (warning)
89.58%
86 / 96
91.67% covered (success)
91.67%
11 / 12
24.65
0.00% covered (danger)
0.00%
0 / 1
 readEntity
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 readList
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 readByHash
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 readByButtonList
60.00% covered (warning)
60.00%
15 / 25
0.00% covered (danger)
0.00%
0 / 1
12.10
 readExceptions
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 readWithContactData
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 readSingleScopeFromButtonList
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 writeEntityWithHash
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
 writeEntity
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 readByOrganisationId
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 deleteEntity
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 readExpiredTicketprinterList
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace BO\Zmsdb;
4
5use BO\Zmsentities\Ticketprinter as Entity;
6use BO\Zmsentities\Collection\TicketprinterList as Collection;
7
8/**
9 *
10 * @SuppressWarnings(CouplingBetweenObjects)
11 */
12class Ticketprinter extends Base
13{
14    /**
15     * read entity
16     *
17     * @param
18     * itemId
19     *
20     * @return Resource Entity
21     */
22    public function readEntity($itemId)
23    {
24        $query = new Query\Ticketprinter(Query\Base::SELECT);
25        $query
26            ->addEntityMapping()
27            ->addConditionTicketprinterId($itemId);
28        $ticketprinter = $this->fetchOne($query, new Entity());
29        $ticketprinter = $this->readWithContactData($ticketprinter);
30        $ticketprinter->enabled = (1 == $ticketprinter->enabled);
31        return $ticketprinter;
32    }
33
34    /**
35     * read list of ticketprinters
36     *
37     * @return Resource Collection
38     */
39    protected function readList($statement)
40    {
41        $ticketprinterList = new Collection();
42        while ($entityData = $statement->fetch(\PDO::FETCH_ASSOC)) {
43            $entity = new Entity($entityData);
44            $ticketprinterList->addEntity($entity);
45        }
46        return $ticketprinterList;
47    }
48
49    /**
50     * read Ticketprinter by Hash
51     *
52     * @param
53     * hash
54     *
55     * @return Resource Entity
56     */
57    public function readByHash($hash)
58    {
59        $query = new Query\Ticketprinter(Query\Base::SELECT);
60        $query
61            ->addEntityMapping()
62            ->addConditionHash($hash);
63        $ticketprinter = $this->fetchOne($query, new Entity());
64        $ticketprinter = $this->readWithContactData($ticketprinter);
65        $ticketprinter->enabled = (1 == $ticketprinter->enabled);
66        return $ticketprinter;
67    }
68
69    /**
70     * read Ticketprinter by comma separated buttonlist
71     *
72     * @param
73     * ticketprinter Entity
74     * now DateTime
75     *
76     * @return Resource Entity
77     */
78    public function readByButtonList(Entity $ticketprinter, \DateTimeImmutable $now)
79    {
80        if (count($ticketprinter->buttons) > 6) {
81            throw new Exception\Ticketprinter\TooManyButtons();
82        }
83
84        foreach ($ticketprinter->buttons as $key => $button) {
85            if ('scope' == $button['type']) {
86                $query = new Scope();
87                $scope = $query->readWithWorkstationCount($button['scope']['id'], $now);
88                if (! $scope) {
89                    throw new Exception\Ticketprinter\UnvalidButtonList();
90                }
91                $ticketprinter->buttons[$key]['scope'] = $scope;
92                $ticketprinter->buttons[$key]['enabled'] = $query->readIsEnabled($scope->id, $now);
93                $ticketprinter->buttons[$key]['name'] = $scope->getPreference('ticketprinter', 'buttonName');
94            }
95
96            if ('request' == $button['type']) {
97                $scopeId = explode('-', $button['request']['id'])[0];
98                $requestId = explode('-', $button['request']['id'])[1];
99                $request = (new Request())->readEntity('dldb', $requestId);
100                $scope = (new Scope())->readWithWorkstationCount($scopeId, $now);
101
102                if (! $request || ! $scope) {
103                    throw new Exception\Ticketprinter\UnvalidButtonList();
104                }
105                $ticketprinter->buttons[$key]['scope'] = $scope;
106
107                $ticketprinter->buttons[$key]['requestId'] = $requestId;
108                $ticketprinter->buttons[$key]['enabled'] = (new Scope())->readIsEnabled($scope->id, $now);
109                $ticketprinter->buttons[$key]['name'] = $request->getProperty('name');
110            }
111        }
112        $this->readExceptions($ticketprinter);
113        $ticketprinter = $this->readWithContactData($ticketprinter);
114        return $ticketprinter;
115    }
116
117    protected function readExceptions(Entity $ticketprinter)
118    {
119        $query = new Scope();
120        $scope = $this->readSingleScopeFromButtonList($ticketprinter);
121        if ($scope && ! $query->readIsGivenNumberInContingent($scope['id'])) {
122            throw new Exception\Scope\GivenNumberCountExceeded();
123        }
124    }
125
126    protected function readWithContactData(Entity $entity)
127    {
128        $contact = new \BO\Zmsentities\Contact();
129
130        /* cluster not allowed anymore as button (2018-01-30, Abnahme mit TE)
131        if (1 == $entity->getClusterList()->count() && 0 == $entity->getScopeList()->count()) {
132            $contact->name = $entity->getClusterList()->getFirst()->name;
133        } elseif (0 == $entity->getClusterList()->count() && 1 == $entity->getScopeList()->count()) {
134            $department = (new Department())->readByScopeId($entity->getScopeList()->getFirst()->id);
135            $contact->name = $department->name;
136        }
137        */
138
139        if (1 == $entity->getScopeList()->count()) {
140            $department = (new Department())->readByScopeId($entity->getScopeList()->getFirst()->id);
141            $contact->name = $department->name;
142        }
143
144        $entity->contact = $contact;
145        return $entity;
146    }
147
148    public function readSingleScopeFromButtonList(Entity $ticketprinter)
149    {
150        $scope = null;
151        if (1 == $ticketprinter->getScopeList()->count()) {
152            $scope = $ticketprinter->getScopeList()->getFirst();
153            $scope = (new Scope())->readEntity($scope['id']);
154        }
155        /* cluster not allowed anymore as button (2018-01-30, Abnahme mit TE)
156        elseif (1 == $ticketprinter->getClusterList()->count()) {
157            $scopeList = $ticketprinter->getClusterList()->getFirst()->scopes;
158            $scopeList = new \BO\Zmsentities\Collection\ScopeList($scopeList);
159            if (1 == $scopeList->count()) {
160                $scope = (new Scope())->readEntity($scopeList->getFirst()['id']);
161            }
162        }
163        */
164        return $scope;
165    }
166
167    /**
168     * write a cookie for ticketprinter
169     *
170     * @param
171     * organisationId
172     *
173     * @return Entity
174     */
175    public function writeEntityWithHash($organisationId, $ticketprinterName = '')
176    {
177        $query = new Query\Ticketprinter(Query\Base::INSERT);
178        $ticketprinter = (new Entity())->getHashWith($organisationId);
179        $ticketprinter->name = $ticketprinterName;
180
181        $organisation = (new Organisation())->readEntity($organisationId);
182        $owner = (new Owner())->readByOrganisationId($organisationId);
183
184        $values = $query->reverseEntityMapping($ticketprinter, $organisation->id);
185        //get owner by organisation
186        $owner = (new Owner())->readByOrganisationId($organisationId);
187        $values['kundenid'] = $owner->id;
188        $query->addValues($values);
189        $this->writeItem($query);
190        $lastInsertId = $this->getWriter()->lastInsertId();
191        return $this->readEntity($lastInsertId);
192    }
193
194    /**
195     * write a ticketprinter
196     *
197     * @param
198     * entity,
199     * organisationId
200     *
201     * @return Entity
202     */
203    public function writeEntity(Entity $entity, $organisationId)
204    {
205        $query = new Query\Ticketprinter(Query\Base::INSERT);
206        $values = $query->reverseEntityMapping($entity, $organisationId);
207
208        //get owner by organisation
209        $owner = (new Owner())->readByOrganisationId($organisationId);
210        $values['kundenid'] = $owner->id;
211
212        $query->addValues($values);
213        $this->writeItem($query);
214        $lastInsertId = $this->getWriter()->lastInsertId();
215        return $this->readEntity($lastInsertId);
216    }
217
218    /**
219     * read list of ticketprinter by organisation
220     *
221     * @param
222     * organisationId
223     *
224     * @return Resource Collection
225     */
226    public function readByOrganisationId($organisationId)
227    {
228        $query = new Query\Ticketprinter(Query\Base::SELECT);
229        $query
230            ->addEntityMapping()
231            ->addConditionOrganisationId($organisationId);
232        $statement = $this->fetchStatement($query);
233        return $this->readList($statement);
234    }
235
236    /**
237    * remove an ticketprinter
238    *
239    * @param
240    * itemId
241    *
242    * @return Resource Status
243    */
244    public function deleteEntity($itemId)
245    {
246        $query =  new Query\Ticketprinter(Query\Base::DELETE);
247        $query->addConditionTicketprinterId($itemId);
248        return $this->deleteItem($query);
249    }
250
251    public function readExpiredTicketprinterList($expirationDate)
252    {
253        $selectQuery = new Query\Ticketprinter(Query\Base::SELECT);
254        $selectQuery
255            ->addEntityMapping()
256            ->addConditionDeleteInterval($expirationDate);
257        $statement = $this->fetchStatement($selectQuery);
258        return $this->readList($statement);
259    }
260}