Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
46 / 46 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
Owner | |
100.00% |
46 / 46 |
|
100.00% |
7 / 7 |
13 | |
100.00% |
1 / 1 |
readEntity | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
readResolvedReferences | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
readList | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
readByOrganisationId | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
deleteEntity | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
writeEntity | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
updateEntity | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace BO\Zmsdb; |
4 | |
5 | use BO\Zmsentities\Owner as Entity; |
6 | use BO\Zmsentities\Collection\OwnerList as Collection; |
7 | |
8 | class Owner extends Base |
9 | { |
10 | /** |
11 | * read entity |
12 | * |
13 | * @param |
14 | * itemId |
15 | * resolveReferences |
16 | * |
17 | * @return Resource Entity |
18 | */ |
19 | public function readEntity($itemId, $resolveReferences = 0) |
20 | { |
21 | $query = new Query\Owner(Query\Base::SELECT); |
22 | $query |
23 | ->addEntityMapping() |
24 | ->addResolvedReferences($resolveReferences) |
25 | ->addConditionOwnerId($itemId); |
26 | $owner = $this->fetchOne($query, new Entity()); |
27 | return $this->readResolvedReferences($owner, $resolveReferences); |
28 | } |
29 | |
30 | public function readResolvedReferences(\BO\Zmsentities\Schema\Entity $entity, $resolveReferences) |
31 | { |
32 | if (0 < $resolveReferences && isset($entity['id'])) { |
33 | //error_log("Owner Level $resolveReferences"); |
34 | $entity['organisations'] = (new Organisation())->readByOwnerId($entity['id'], $resolveReferences - 1); |
35 | } |
36 | return $entity; |
37 | } |
38 | |
39 | /** |
40 | * read list of owners |
41 | * |
42 | * @param |
43 | * resolveReferences |
44 | * |
45 | * @return Resource Collection |
46 | */ |
47 | public function readList($resolveReferences = 0) |
48 | { |
49 | $ownerList = new Collection(); |
50 | $query = new Query\Owner(Query\Base::SELECT); |
51 | $query |
52 | ->addResolvedReferences($resolveReferences) |
53 | ->addEntityMapping(); |
54 | $result = $this->fetchList($query, new Entity()); |
55 | if (count($result)) { |
56 | foreach ($result as $entity) { |
57 | $entity = $this->readResolvedReferences($entity, $resolveReferences); |
58 | $ownerList->addEntity($entity); |
59 | } |
60 | } |
61 | return $ownerList; |
62 | } |
63 | |
64 | public function readByOrganisationId($organisationId, $resolveReferences = 0) |
65 | { |
66 | $query = new Query\Owner(Query\Base::SELECT); |
67 | $query |
68 | ->addEntityMapping() |
69 | ->addResolvedReferences($resolveReferences) |
70 | ->addConditionOrganisationId($organisationId); |
71 | $owner = $this->fetchOne($query, new Entity()); |
72 | return $this->readResolvedReferences($owner, $resolveReferences); |
73 | } |
74 | |
75 | /** |
76 | * remove an owner |
77 | * |
78 | * @param |
79 | * itemId |
80 | * |
81 | * @return Resource Status |
82 | */ |
83 | public function deleteEntity($itemId) |
84 | { |
85 | $entity = $this->readEntity($itemId, 1); |
86 | if (0 < $entity->toProperty()->organisations->get()->count()) { |
87 | throw new Exception\Owner\OrganisationListNotEmpty(); |
88 | } |
89 | $query = new Query\Owner(Query\Base::DELETE); |
90 | $query->addConditionOwnerId($itemId); |
91 | return ($this->deleteItem($query)) ? $entity : null; |
92 | } |
93 | |
94 | /** |
95 | * write an owner |
96 | * |
97 | * @param |
98 | * entity |
99 | * |
100 | * @return Entity |
101 | */ |
102 | public function writeEntity(\BO\Zmsentities\Owner $entity) |
103 | { |
104 | $query = new Query\Owner(Query\Base::INSERT); |
105 | $values = $query->reverseEntityMapping($entity); |
106 | $query->addValues($values); |
107 | $this->writeItem($query); |
108 | $lastInsertId = $this->getWriter()->lastInsertId(); |
109 | return $this->readEntity($lastInsertId); |
110 | } |
111 | |
112 | /** |
113 | * update an owner |
114 | * |
115 | * @param |
116 | * ownerId, entity |
117 | * |
118 | * @return Entity |
119 | */ |
120 | public function updateEntity($ownerId, \BO\Zmsentities\Owner $entity) |
121 | { |
122 | $query = new Query\Owner(Query\Base::UPDATE); |
123 | $query->addConditionOwnerId($ownerId); |
124 | $values = $query->reverseEntityMapping($entity); |
125 | $query->addValues($values); |
126 | $this->writeItem($query); |
127 | return $this->readEntity($ownerId); |
128 | } |
129 | } |