Total Complexity | 55 |
Total Lines | 453 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
Complex classes like AuditReader often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AuditReader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class AuditReader |
||
22 | { |
||
23 | public const UPDATE = 'update'; |
||
24 | public const ASSOCIATE = 'associate'; |
||
25 | public const DISSOCIATE = 'dissociate'; |
||
26 | public const INSERT = 'insert'; |
||
27 | public const REMOVE = 'remove'; |
||
28 | |||
29 | public const PAGE_SIZE = 50; |
||
30 | |||
31 | /** |
||
32 | * @var AuditConfiguration |
||
33 | */ |
||
34 | private $configuration; |
||
35 | |||
36 | /** |
||
37 | * @var EntityManagerInterface |
||
38 | */ |
||
39 | private $entityManager; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | private $filters = []; |
||
45 | |||
46 | /** |
||
47 | * AuditReader constructor. |
||
48 | * |
||
49 | * @param AuditConfiguration $configuration |
||
50 | * @param EntityManagerInterface $entityManager |
||
51 | */ |
||
52 | public function __construct( |
||
53 | AuditConfiguration $configuration, |
||
54 | EntityManagerInterface $entityManager |
||
55 | ) { |
||
56 | $this->configuration = $configuration; |
||
57 | $this->entityManager = $entityManager; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @return AuditConfiguration |
||
62 | */ |
||
63 | public function getConfiguration(): AuditConfiguration |
||
64 | { |
||
65 | return $this->configuration; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Set the filter(s) for AuditEntry retrieving. |
||
70 | * |
||
71 | * @param array|string $filter |
||
72 | * |
||
73 | * @return AuditReader |
||
74 | */ |
||
75 | public function filterBy($filter): self |
||
76 | { |
||
77 | $filters = []; |
||
78 | |||
79 | if (!\is_array($filter)) { |
||
80 | $filter = [$filter]; |
||
81 | } |
||
82 | |||
83 | foreach ($filter as $f) { |
||
84 | if ($this->isAllowedFilter($f)) { |
||
85 | $filters[] = $f; |
||
86 | } |
||
87 | } |
||
88 | |||
89 | $this->filters = $filters; |
||
90 | |||
91 | return $this; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Returns current filter. |
||
96 | * |
||
97 | * @return array |
||
98 | */ |
||
99 | public function getFilters(): array |
||
100 | { |
||
101 | return $this->filters; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Returns an array of audit table names indexed by entity FQN. |
||
106 | * |
||
107 | * @throws \Doctrine\ORM\ORMException |
||
108 | * |
||
109 | * @return array |
||
110 | */ |
||
111 | public function getEntities(): array |
||
112 | { |
||
113 | $metadataDriver = $this->entityManager->getConfiguration()->getMetadataDriverImpl(); |
||
114 | $entities = []; |
||
115 | if (null !== $metadataDriver) { |
||
116 | $entities = $metadataDriver->getAllClassNames(); |
||
117 | } |
||
118 | $audited = []; |
||
119 | foreach ($entities as $entity) { |
||
120 | if ($this->configuration->isAuditable($entity)) { |
||
121 | $audited[$entity] = $this->getEntityTableName($entity); |
||
122 | } |
||
123 | } |
||
124 | ksort($audited); |
||
125 | |||
126 | return $audited; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Returns an array of audited entries/operations. |
||
131 | * |
||
132 | * @param object|string $entity |
||
133 | * @param null|int|string $id |
||
134 | * @param null|int $page |
||
135 | * @param null|int $pageSize |
||
136 | * @param null|string $transactionHash |
||
137 | * @param bool $strict |
||
138 | * |
||
139 | * @throws AccessDeniedException |
||
140 | * @throws InvalidArgumentException |
||
141 | * |
||
142 | * @return array |
||
143 | */ |
||
144 | public function getAudits($entity, $id = null, ?int $page = null, ?int $pageSize = null, ?string $transactionHash = null, bool $strict = true): array |
||
145 | { |
||
146 | $this->checkAuditable($entity); |
||
147 | $this->checkRoles($entity, Security::VIEW_SCOPE); |
||
148 | |||
149 | $queryBuilder = $this->getAuditsQueryBuilder($entity, $id, $page, $pageSize, $transactionHash, $strict); |
||
150 | |||
151 | /** @var Statement $statement */ |
||
152 | $statement = $queryBuilder->execute(); |
||
153 | $statement->setFetchMode(PDO::FETCH_CLASS, AuditEntry::class); |
||
154 | |||
155 | return $statement->fetchAll(); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Returns an array of all audited entries/operations for a given transaction hash |
||
160 | * indexed by entity FQCN. |
||
161 | * |
||
162 | * @param string $transactionHash |
||
163 | * |
||
164 | * @throws InvalidArgumentException |
||
165 | * @throws \Doctrine\ORM\ORMException |
||
166 | * |
||
167 | * @return array |
||
168 | */ |
||
169 | public function getAuditsByTransactionHash(string $transactionHash): array |
||
170 | { |
||
171 | $results = []; |
||
172 | |||
173 | $entities = $this->getEntities(); |
||
174 | foreach ($entities as $entity => $tablename) { |
||
175 | try { |
||
176 | $audits = $this->getAudits($entity, null, null, null, $transactionHash); |
||
177 | if (\count($audits) > 0) { |
||
178 | $results[$entity] = $audits; |
||
179 | } |
||
180 | } catch (AccessDeniedException $e) { |
||
181 | // acces denied |
||
182 | } |
||
183 | } |
||
184 | |||
185 | return $results; |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Returns an array of audited entries/operations. |
||
190 | * |
||
191 | * @param object|string $entity |
||
192 | * @param null|int|string $id |
||
193 | * @param int $page |
||
194 | * @param int $pageSize |
||
195 | * |
||
196 | * @throws AccessDeniedException |
||
197 | * @throws InvalidArgumentException |
||
198 | * |
||
199 | * @return Pagerfanta |
||
200 | */ |
||
201 | public function getAuditsPager($entity, $id = null, int $page = 1, int $pageSize = self::PAGE_SIZE): Pagerfanta |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Returns the amount of audited entries/operations. |
||
218 | * |
||
219 | * @param object|string $entity |
||
220 | * @param null|int|string $id |
||
221 | * |
||
222 | * @throws AccessDeniedException |
||
223 | * @throws InvalidArgumentException |
||
224 | * |
||
225 | * @return int |
||
226 | */ |
||
227 | public function getAuditsCount($entity, $id = null): int |
||
228 | { |
||
229 | $queryBuilder = $this->getAuditsQueryBuilder($entity, $id); |
||
230 | |||
231 | $result = $queryBuilder |
||
232 | ->resetQueryPart('select') |
||
233 | ->resetQueryPart('orderBy') |
||
234 | ->select('COUNT(id)') |
||
235 | ->execute() |
||
236 | ->fetchColumn(0) |
||
237 | ; |
||
238 | |||
239 | return false === $result ? 0 : $result; |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * @param object|string $entity |
||
244 | * @param string $id |
||
245 | * |
||
246 | * @throws AccessDeniedException |
||
247 | * @throws InvalidArgumentException |
||
248 | * |
||
249 | * @return mixed[] |
||
250 | */ |
||
251 | public function getAudit($entity, $id) |
||
252 | { |
||
253 | $this->checkAuditable($entity); |
||
254 | $this->checkRoles($entity, Security::VIEW_SCOPE); |
||
255 | |||
256 | $connection = $this->entityManager->getConnection(); |
||
257 | |||
258 | /** |
||
259 | * @var \Doctrine\DBAL\Query\QueryBuilder |
||
260 | */ |
||
261 | $queryBuilder = $connection->createQueryBuilder(); |
||
262 | $queryBuilder |
||
263 | ->select('*') |
||
264 | ->from($this->getEntityAuditTableName($entity)) |
||
265 | ->where('id = :id') |
||
266 | ->setParameter('id', $id) |
||
267 | ; |
||
268 | |||
269 | if (!empty($this->filters)) { |
||
270 | $queryBuilder |
||
271 | ->andWhere('type IN (:filters)') |
||
272 | ->setParameter('filters', $this->filters, Connection::PARAM_STR_ARRAY) |
||
273 | ; |
||
274 | } |
||
275 | |||
276 | /** @var Statement $statement */ |
||
277 | $statement = $queryBuilder->execute(); |
||
278 | $statement->setFetchMode(PDO::FETCH_CLASS, AuditEntry::class); |
||
279 | |||
280 | return $statement->fetchAll(); |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Returns the table name of $entity. |
||
285 | * |
||
286 | * @param object|string $entity |
||
287 | * |
||
288 | * @return string |
||
289 | */ |
||
290 | public function getEntityTableName($entity): string |
||
291 | { |
||
292 | return $this->getClassMetadata($entity)->getTableName(); |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * Returns the audit table name for $entity. |
||
297 | * |
||
298 | * @param object|string $entity |
||
299 | * |
||
300 | * @return string |
||
301 | */ |
||
302 | public function getEntityAuditTableName($entity): string |
||
303 | { |
||
304 | $entityName = \is_string($entity) ? $entity : \get_class($entity); |
||
305 | $schema = $this->getClassMetadata($entityName)->getSchemaName() ? $this->getClassMetadata($entityName)->getSchemaName().'.' : ''; |
||
306 | |||
307 | return sprintf('%s%s%s%s', $schema, $this->configuration->getTablePrefix(), $this->getEntityTableName($entityName), $this->configuration->getTableSuffix()); |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * @return EntityManagerInterface |
||
312 | */ |
||
313 | public function getEntityManager(): EntityManagerInterface |
||
314 | { |
||
315 | return $this->entityManager; |
||
316 | } |
||
317 | |||
318 | private function isAllowedFilter(string $filter): bool |
||
319 | { |
||
320 | return \in_array($filter, [self::UPDATE, self::ASSOCIATE, self::DISSOCIATE, self::INSERT, self::REMOVE], true); |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * Returns an array of audited entries/operations. |
||
325 | * |
||
326 | * @param object|string $entity |
||
327 | * @param null|int|string $id |
||
328 | * @param null|int $page |
||
329 | * @param null|int $pageSize |
||
330 | * @param null|string $transactionHash |
||
331 | * @param bool $strict |
||
332 | * |
||
333 | * @throws AccessDeniedException |
||
334 | * @throws InvalidArgumentException |
||
335 | * |
||
336 | * @return QueryBuilder |
||
337 | */ |
||
338 | private function getAuditsQueryBuilder($entity, $id = null, ?int $page = null, ?int $pageSize = null, ?string $transactionHash = null, bool $strict = true): QueryBuilder |
||
339 | { |
||
340 | $this->checkAuditable($entity); |
||
341 | $this->checkRoles($entity, Security::VIEW_SCOPE); |
||
342 | |||
343 | if (null !== $page && $page < 1) { |
||
344 | throw new \InvalidArgumentException('$page must be greater or equal than 1.'); |
||
345 | } |
||
346 | |||
347 | if (null !== $pageSize && $pageSize < 1) { |
||
348 | throw new \InvalidArgumentException('$pageSize must be greater or equal than 1.'); |
||
349 | } |
||
350 | |||
351 | $storage = $this->selectStorage(); |
||
352 | $connection = $storage->getConnection(); |
||
353 | |||
354 | $queryBuilder = $connection->createQueryBuilder(); |
||
355 | $queryBuilder |
||
356 | ->select('*') |
||
357 | ->from($this->getEntityAuditTableName($entity), 'at') |
||
358 | ->orderBy('created_at', 'DESC') |
||
359 | ->addOrderBy('id', 'DESC') |
||
360 | ; |
||
361 | |||
362 | $metadata = $this->getClassMetadata($entity); |
||
363 | if ($strict && $metadata instanceof ORMMetadata && ORMMetadata::INHERITANCE_TYPE_SINGLE_TABLE === $metadata->inheritanceType) { |
||
364 | $queryBuilder |
||
365 | ->andWhere('discriminator = :discriminator') |
||
366 | ->setParameter('discriminator', \is_object($entity) ? \get_class($entity) : $entity) |
||
367 | ; |
||
368 | } |
||
369 | |||
370 | if (null !== $pageSize) { |
||
371 | $queryBuilder |
||
372 | ->setFirstResult(($page - 1) * $pageSize) |
||
373 | ->setMaxResults($pageSize) |
||
374 | ; |
||
375 | } |
||
376 | |||
377 | if (null !== $id) { |
||
378 | $queryBuilder |
||
379 | ->andWhere('object_id = :object_id') |
||
380 | ->setParameter('object_id', $id) |
||
381 | ; |
||
382 | } |
||
383 | |||
384 | if (!empty($this->filters)) { |
||
385 | $queryBuilder |
||
386 | ->andWhere('type IN (:filters)') |
||
387 | ->setParameter('filters', $this->filters, Connection::PARAM_STR_ARRAY) |
||
388 | ; |
||
389 | } |
||
390 | |||
391 | if (null !== $transactionHash) { |
||
392 | $queryBuilder |
||
393 | ->andWhere('transaction_hash = :transaction_hash') |
||
394 | ->setParameter('transaction_hash', $transactionHash) |
||
395 | ; |
||
396 | } |
||
397 | |||
398 | return $queryBuilder; |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * @param object|string $entity |
||
403 | * |
||
404 | * @return ClassMetadata |
||
405 | */ |
||
406 | private function getClassMetadata($entity): ClassMetadata |
||
409 | } |
||
410 | |||
411 | /** |
||
412 | * @return EntityManagerInterface |
||
413 | */ |
||
414 | private function selectStorage(): EntityManagerInterface |
||
415 | { |
||
416 | return $this->configuration->getEntityManager() ?? $this->entityManager; |
||
417 | } |
||
418 | |||
419 | /** |
||
420 | * Throws an InvalidArgumentException if given entity is not auditable. |
||
421 | * |
||
422 | * @param object|string $entity |
||
423 | * |
||
424 | * @throws InvalidArgumentException |
||
425 | */ |
||
426 | private function checkAuditable($entity): void |
||
427 | { |
||
428 | if (!$this->configuration->isAuditable($entity)) { |
||
429 | throw new InvalidArgumentException('Entity '.$entity.' is not auditable.'); |
||
430 | } |
||
431 | } |
||
432 | |||
433 | /** |
||
434 | * Throws an AccessDeniedException if user not is granted to access audits for the given entity. |
||
435 | * |
||
436 | * @param object|string $entity |
||
437 | * @param string $scope |
||
438 | * |
||
439 | * @throws AccessDeniedException |
||
440 | */ |
||
441 | private function checkRoles($entity, string $scope): void |
||
474 | } |
||
475 | } |
||
476 |