Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
95 / 95
100.00% covered (success)
100.00%
13 / 13
CRAP
100.00% covered (success)
100.00%
1 / 1
Organisation
100.00% covered (success)
100.00%
95 / 95
100.00% covered (success)
100.00%
13 / 13
30
100.00% covered (success)
100.00%
1 / 1
 readEntity
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 readResolvedReferences
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 readByScopeId
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 readByDepartmentId
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 readByClusterId
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 readByOwnerId
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
4
 readByHash
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 readList
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
4
 deleteEntity
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 writeEntity
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 updateEntity
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 writeOrganisationTicketprinters
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 updateOrganisationTicketprinters
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace BO\Zmsdb;
4
5use BO\Zmsentities\Organisation as Entity;
6use BO\Zmsentities\Collection\OrganisationList as Collection;
7
8/**
9 *
10 * @SuppressWarnings(Public)
11 *
12 */
13class Organisation extends Base
14{
15    public function readEntity($itemId, $resolveReferences = 0)
16    {
17        $query = new Query\Organisation(Query\Base::SELECT);
18        $query->addEntityMapping()
19            ->addResolvedReferences($resolveReferences)
20            ->addConditionOrganisationId($itemId);
21        $organisation = $this->fetchOne($query, new Entity());
22        if (isset($organisation['id'])) {
23            return $this->readResolvedReferences($organisation, $resolveReferences);
24        }
25        return array();
26    }
27
28    public function readResolvedReferences(\BO\Zmsentities\Schema\Entity $entity, $resolveReferences)
29    {
30        if (0 < $resolveReferences && $entity->hasId()) {
31            //error_log("Organisation Level $resolveReferences");
32            $entity['departments'] = (new Department())
33                ->readByOrganisationId($entity->id, $resolveReferences - 1);
34            $entity['ticketprinters'] = (new Ticketprinter())
35                ->readByOrganisationId($entity->id, $resolveReferences - 1);
36        }
37        return $entity;
38    }
39
40    public function readByScopeId($scopeId, $resolveReferences = 0)
41    {
42        $query = new Query\Organisation(Query\Base::SELECT);
43        $query->addEntityMapping()
44            ->addResolvedReferences($resolveReferences)
45            ->addConditionScopeId($scopeId);
46        $organisation = $this->fetchOne($query, new Entity());
47        return $this->readResolvedReferences($organisation, $resolveReferences);
48    }
49
50    public function readByDepartmentId($departmentId, $resolveReferences = 0)
51    {
52        $query = new Query\Organisation(Query\Base::SELECT);
53        $query->addEntityMapping()
54            ->addResolvedReferences($resolveReferences)
55            ->addConditionDepartmentId($departmentId);
56        $organisation = $this->fetchOne($query, new Entity());
57        return $this->readResolvedReferences($organisation, $resolveReferences);
58    }
59
60    public function readByClusterId($clusterId, $resolveReferences = 0)
61    {
62        $scope = (new Scope())->readByClusterId($clusterId, $resolveReferences)->getFirst();
63        if (! $scope) {
64            throw new Exception\ClusterWithoutScopes();
65        }
66        return $this->readByScopeId($scope->id, $resolveReferences);
67    }
68
69    public function readByOwnerId($ownerId, $resolveReferences = 0)
70    {
71        $organisationList = new Collection();
72        $query = new Query\Organisation(Query\Base::SELECT);
73        $query->addEntityMapping()
74            ->addResolvedReferences($resolveReferences)
75            ->addConditionOwnerId($ownerId);
76        $result = $this->fetchList($query, new Entity());
77        if (count($result)) {
78            foreach ($result as $organisation) {
79                $entity = $this->readResolvedReferences($organisation, $resolveReferences);
80                if ($entity instanceof Entity) {
81                    $organisationList->addEntity($entity);
82                }
83            }
84        }
85        return $organisationList;
86    }
87
88    public function readByHash($hash)
89    {
90        $organisationId = $this->getReader()
91            ->fetchValue((new Query\Ticketprinter(Query\Base::SELECT))
92            ->getOrganisationIdByHash(), ['hash' => $hash]);
93
94        return $this->readEntity($organisationId);
95    }
96
97    public function readList($resolveReferences = 0)
98    {
99        $organisationList = new Collection();
100        $query = new Query\Organisation(Query\Base::SELECT);
101        $query->addEntityMapping()
102            ->addResolvedReferences($resolveReferences);
103        $result = $this->fetchList($query, new Entity());
104        if (count($result)) {
105            foreach ($result as $organisation) {
106                $entity = new Entity($organisation);
107                if ($entity instanceof Entity) {
108                    $entity = $this->readResolvedReferences($entity, $resolveReferences);
109                    $organisationList->addEntity($entity);
110                }
111            }
112        }
113        return $organisationList;
114    }
115
116    public function deleteEntity($itemId)
117    {
118        $entity = $this->readEntity($itemId, 1);
119        if (0 < $entity->toProperty()->departments->get()->count()) {
120            throw new Exception\Organisation\DepartmentListNotEmpty();
121        }
122        $query = new Query\Organisation(Query\Base::DELETE);
123        $query->addConditionOrganisationId($itemId);
124        return ($this->deleteItem($query)) ? $entity : null;
125    }
126
127    public function writeEntity(\BO\Zmsentities\Organisation $entity, $parentId)
128    {
129        $query = new Query\Organisation(Query\Base::INSERT);
130        $values = $query->reverseEntityMapping($entity, $parentId);
131        $query->addValues($values);
132        $this->writeItem($query);
133        $lastInsertId = $this->getWriter()
134            ->lastInsertId();
135        if ($entity->toProperty()->ticketprinters->isAvailable()) {
136            $this->writeOrganisationTicketprinters($lastInsertId, $entity->ticketprinters);
137        }
138        return $this->readEntity($lastInsertId);
139    }
140
141    public function updateEntity($organisationId, \BO\Zmsentities\Organisation $entity)
142    {
143        $query = new Query\Organisation(Query\Base::UPDATE);
144        $query->addConditionOrganisationId($organisationId);
145        $values = $query->reverseEntityMapping($entity);
146        $query->addValues($values);
147        $this->writeItem($query);
148        if ($entity->toProperty()->ticketprinters->isAvailable()) {
149            $this->updateOrganisationTicketprinters($entity->ticketprinters, $organisationId);
150        }
151        return $this->readEntity($organisationId, 1, true);
152    }
153
154    protected function writeOrganisationTicketprinters($organisationId, $ticketprinterList)
155    {
156        $deleteQuery = new Query\Ticketprinter(Query\Base::DELETE);
157        $deleteQuery->addConditionOrganisationId($organisationId);
158        $this->deleteItem($deleteQuery);
159        foreach ($ticketprinterList as $ticketprinter) {
160            $ticketprinter['enabled'] = (isset($ticketprinter['enabled']) && $ticketprinter['enabled']);
161            $ticketprinter = new \BO\Zmsentities\Ticketprinter($ticketprinter);
162            $query = new Ticketprinter();
163            $query->writeEntity($ticketprinter, $organisationId);
164        }
165    }
166
167    protected function updateOrganisationTicketprinters($ticketprinterList, $organisationId)
168    {
169        foreach ($ticketprinterList as $item) {
170            $query = new Query\Ticketprinter(Query\Base::UPDATE);
171            $entity = new \BO\Zmsentities\Ticketprinter($item);
172            $query->addConditionHash($entity->getId());
173            $query->addValues($query->reverseEntityMapping($entity, $organisationId));
174            $this->writeItem($query);
175        }
176    }
177}