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