Conditions | 6 |
Paths | 14 |
Total Lines | 58 |
Code Lines | 41 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
34 | public function getSecurityStatistics(int $weeks = 4): array |
||
35 | { |
||
36 | $startDate = new \DateTime(sprintf('-%d weeks', $weeks)); |
||
37 | |||
38 | $eventTypes = [ |
||
39 | SecurityEventTypeEnum::PASSWORD_CHANGE->value, |
||
40 | SecurityEventTypeEnum::PASSWORD_RESET_REQUEST->value, |
||
41 | SecurityEventTypeEnum::PASSWORD_RESET_COMPLETE->value, |
||
42 | SecurityEventTypeEnum::EMAIL_CHANGE->value, |
||
43 | SecurityEventTypeEnum::REGISTRATION->value, |
||
44 | ]; |
||
45 | |||
46 | $qb = $this->entityManager->createQueryBuilder(); |
||
47 | $qb->select('se.eventType, COUNT(se.id) as count') |
||
48 | ->from(SecurityEvent::class, 'se') |
||
49 | ->where('se.createdAt >= :startDate') |
||
50 | ->andWhere('se.eventType IN (:eventTypes)') |
||
51 | ->setParameter('startDate', $startDate) |
||
52 | ->setParameter('eventTypes', $eventTypes) |
||
53 | ->groupBy('se.eventType') |
||
54 | ->orderBy('count', 'DESC'); |
||
55 | |||
56 | $results = $qb->getQuery()->getResult(); |
||
57 | |||
58 | $statistics = []; |
||
59 | foreach ($results as $result) { |
||
60 | $eventType = SecurityEventTypeEnum::from($result['eventType']); |
||
61 | $statistics[] = [ |
||
62 | 'type' => $eventType, |
||
63 | 'count' => $result['count'], |
||
64 | 'label' => $eventType->getLabel(), |
||
65 | 'icon' => $eventType->getIcon(), |
||
66 | 'severity' => $eventType->getSeverity() |
||
67 | ]; |
||
68 | } |
||
69 | |||
70 | // Ajouter les types sans événements |
||
71 | foreach ($eventTypes as $type) { |
||
72 | $found = false; |
||
73 | foreach ($statistics as $stat) { |
||
74 | if ($stat['type']->value === $type) { |
||
75 | $found = true; |
||
76 | break; |
||
77 | } |
||
78 | } |
||
79 | if (!$found) { |
||
80 | $eventType = SecurityEventTypeEnum::from($type); |
||
81 | $statistics[] = [ |
||
82 | 'type' => $eventType, |
||
83 | 'count' => 0, |
||
84 | 'label' => $eventType->getLabel(), |
||
85 | 'icon' => $eventType->getIcon(), |
||
86 | 'severity' => $eventType->getSeverity() |
||
87 | ]; |
||
88 | } |
||
89 | } |
||
90 | |||
91 | return $statistics; |
||
92 | } |
||
160 |