Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
75 / 75 |
|
100.00% |
10 / 10 |
CRAP | |
100.00% |
1 / 1 |
Apikey | |
100.00% |
75 / 75 |
|
100.00% |
10 / 10 |
20 | |
100.00% |
1 / 1 |
readEntity | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
writeEntity | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
updateEntity | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
readQuota | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
readQuotaList | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
writeQuota | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
updateQuota | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
4 | |||
deleteEntity | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 | |||
readExpiredQuotaListByPeriod | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
writeDeletedQuota | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace BO\Zmsdb; |
4 | |
5 | use BO\Zmsentities\Apikey as Entity; |
6 | |
7 | class Apikey extends Base |
8 | { |
9 | public static $cache = []; |
10 | |
11 | public function readEntity($apiKey) |
12 | { |
13 | $query = new Query\Apikey(Query\Base::SELECT); |
14 | $query |
15 | ->addEntityMapping() |
16 | ->addResolvedReferences(0) |
17 | ->addConditionApikey($apiKey); |
18 | $entity = $this->fetchOne($query, new Entity()); |
19 | if ($entity->hasId()) { |
20 | $entity->quota = $this->readQuotaList($apiKey); |
21 | } |
22 | return $entity; |
23 | } |
24 | |
25 | /** |
26 | * write a new apikey |
27 | * |
28 | * @param |
29 | * entity |
30 | * |
31 | * @return Entity |
32 | */ |
33 | public function writeEntity(Entity $entity) |
34 | { |
35 | $query = new Query\Apikey(Query\Base::INSERT); |
36 | $query->addValues([ |
37 | 'key' => $entity->key, |
38 | 'createIP' => $entity->createIP, |
39 | 'ts' => (new \DateTimeImmutable())->getTimestamp() |
40 | ]); |
41 | if ($this->writeItem($query)) { |
42 | $this->updateQuota($entity->key, $entity); |
43 | } |
44 | return $this->readEntity($entity->key); |
45 | } |
46 | |
47 | /** |
48 | * update an existing active apikey quota |
49 | * |
50 | * @param |
51 | * entity |
52 | * |
53 | * @return Entity |
54 | */ |
55 | public function updateEntity($apiKey, Entity $entity) |
56 | { |
57 | $this->updateQuota($apiKey, $entity); |
58 | return $this->readEntity($apiKey, 1); |
59 | } |
60 | |
61 | /** |
62 | * read specified api quota |
63 | * |
64 | * @param |
65 | * apiKey |
66 | * entity |
67 | * |
68 | * @return Entity |
69 | */ |
70 | public function readQuota($apiKey, $route) |
71 | { |
72 | $query = new Query\Apiquota(Query\Base::SELECT); |
73 | $query |
74 | ->addEntityMapping() |
75 | ->addResolvedReferences(0) |
76 | ->addConditionApikey($apiKey) |
77 | ->addConditionRoute($route); |
78 | $statement = $this->fetchStatement($query); |
79 | $data = $statement->fetch(\PDO::FETCH_ASSOC); |
80 | return $data; |
81 | } |
82 | |
83 | /** |
84 | * read api quotas by apikey |
85 | * |
86 | * @param |
87 | * apiKey |
88 | * entity |
89 | * |
90 | * @return Entity |
91 | */ |
92 | public function readQuotaList($apiKey) |
93 | { |
94 | $data = $this |
95 | ->getReader() |
96 | ->fetchAll( |
97 | Query\Apiquota::getQueryReadApiQuotaListByKey(), |
98 | [ |
99 | 'key' => $apiKey |
100 | ] |
101 | ); |
102 | return ($data) ? $data : []; |
103 | } |
104 | |
105 | /** |
106 | * write initial api quotas |
107 | * |
108 | * @param |
109 | * entity |
110 | * |
111 | * @return Entity |
112 | */ |
113 | public function writeQuota($apiKey, $route, $period, $requests) |
114 | { |
115 | $query = new Query\Apiquota(Query\Base::INSERT); |
116 | $query->addValues([ |
117 | 'key' => $apiKey, |
118 | 'route' => $route, |
119 | 'period' => $period, |
120 | 'requests' => $requests, |
121 | 'ts' => (new \DateTimeImmutable())->getTimestamp() |
122 | ]); |
123 | $this->writeItem($query); |
124 | } |
125 | |
126 | /** |
127 | * update api quotas |
128 | * |
129 | * @param |
130 | * apiKey |
131 | * entity |
132 | * |
133 | * @return Entity |
134 | */ |
135 | public function updateQuota($apiKey, Entity $entity) |
136 | { |
137 | if (isset($entity->quota)) { |
138 | foreach ($entity->quota as $quota) { |
139 | if ($this->readQuota($apiKey, $quota['route'])) { |
140 | $query = new Query\Apiquota(Query\Base::UPDATE); |
141 | $query->addConditionApikey($apiKey); |
142 | $query->addConditionRoute($quota['route']); |
143 | $query->addValues([ |
144 | 'key' => $apiKey, |
145 | 'route' => $quota['route'], |
146 | 'period' => $quota['period'], |
147 | 'requests' => $quota['requests'] |
148 | ]); |
149 | $this->writeItem($query); |
150 | } else { |
151 | $this->writeQuota($apiKey, $quota['route'], $quota['period'], $quota['requests']); |
152 | } |
153 | } |
154 | } |
155 | } |
156 | |
157 | /** |
158 | * delete an existing outdated apikey |
159 | * |
160 | * @param |
161 | * apikey |
162 | * |
163 | * @return Entity |
164 | */ |
165 | public function deleteEntity($apiKey) |
166 | { |
167 | $entity = $this->readEntity($apiKey); |
168 | if ($entity) { |
169 | $query = new Query\Apikey(Query\Base::DELETE); |
170 | $query->addConditionApikey($apiKey); |
171 | if ($this->deleteItem($query)) { |
172 | $queryQuota = new Query\Apiquota(Query\Base::DELETE); |
173 | $queryQuota->addConditionApikey($apiKey); |
174 | $this->deleteItem($queryQuota); |
175 | }; |
176 | } |
177 | |
178 | return ($entity) ? $entity : null; |
179 | } |
180 | |
181 | /** |
182 | * delete api quota by its period setting and creation timestamp |
183 | * |
184 | * @param |
185 | * apikey |
186 | * |
187 | * @return Entity |
188 | */ |
189 | public function readExpiredQuotaListByPeriod(\DateTimeInterface $dateTime) |
190 | { |
191 | $data = $this->fetchAll(Query\Apiquota::getQueryReadApiQuotaExpired($dateTime)); |
192 | return ($data) ? $data : null; |
193 | } |
194 | |
195 | /** |
196 | * delete an existing outdated apikey |
197 | * |
198 | * @param |
199 | * apikey |
200 | * |
201 | * @return Entity |
202 | */ |
203 | public function writeDeletedQuota($quotaId) |
204 | { |
205 | $query = new Query\Apiquota(Query\Base::DELETE); |
206 | $query->addConditionQuotaId($quotaId); |
207 | return $this->deleteItem($query); |
208 | } |
209 | } |