Conditions | 6 |
Paths | 13 |
Total Lines | 64 |
Code Lines | 42 |
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 |
||
94 | public function getWeeklyTrends(int $weeks = 4): array |
||
95 | { |
||
96 | $trends = []; |
||
97 | $eventTypes = [ |
||
98 | SecurityEventTypeEnum::PASSWORD_CHANGE->value, |
||
99 | SecurityEventTypeEnum::PASSWORD_RESET_REQUEST->value, |
||
100 | SecurityEventTypeEnum::PASSWORD_RESET_COMPLETE->value, |
||
101 | SecurityEventTypeEnum::EMAIL_CHANGE->value, |
||
102 | SecurityEventTypeEnum::REGISTRATION->value, |
||
103 | ]; |
||
104 | |||
105 | // Semaine en cours + 4 dernières semaines complètes = 5 semaines au total |
||
106 | for ($i = $weeks; $i >= 0; $i--) { |
||
107 | // Calculer le début de la semaine (lundi) |
||
108 | $weekStart = new \DateTime(); |
||
109 | $weekStart->modify(sprintf('-%d weeks', $i)); |
||
110 | $weekStart->modify('monday this week'); |
||
111 | $weekStart->setTime(0, 0, 0); |
||
112 | |||
113 | // Calculer la fin de la semaine (dimanche) |
||
114 | $weekEnd = clone $weekStart; |
||
115 | $weekEnd->modify('+6 days'); |
||
116 | $weekEnd->setTime(23, 59, 59); |
||
117 | |||
118 | // Pour la semaine en cours (i = 0), ne pas dépasser aujourd'hui |
||
119 | if ($i === 0) { |
||
120 | $now = new \DateTime(); |
||
121 | if ($weekEnd > $now) { |
||
122 | $weekEnd = $now; |
||
123 | } |
||
124 | } |
||
125 | |||
126 | $qb = $this->entityManager->createQueryBuilder(); |
||
127 | $qb->select('se.eventType, COUNT(se.id) as count') |
||
128 | ->from(SecurityEvent::class, 'se') |
||
129 | ->where('se.createdAt >= :weekStart') |
||
130 | ->andWhere('se.createdAt < :weekEnd') |
||
131 | ->andWhere('se.eventType IN (:eventTypes)') |
||
132 | ->setParameter('weekStart', $weekStart) |
||
133 | ->setParameter('weekEnd', $weekEnd) |
||
134 | ->setParameter('eventTypes', $eventTypes) |
||
135 | ->groupBy('se.eventType'); |
||
136 | |||
137 | $weekResults = $qb->getQuery()->getResult(); |
||
138 | |||
139 | $weekData = []; |
||
140 | foreach ($eventTypes as $type) { |
||
141 | $weekData[$type] = 0; |
||
142 | } |
||
143 | |||
144 | foreach ($weekResults as $result) { |
||
145 | $weekData[$result['eventType']] = $result['count']; |
||
146 | } |
||
147 | |||
148 | $trends[] = [ |
||
149 | 'week' => $weekStart->format('W'), |
||
150 | 'year' => $weekStart->format('Y'), |
||
151 | 'start_date' => $weekStart, |
||
152 | 'end_date' => $weekEnd, |
||
153 | 'data' => $weekData |
||
154 | ]; |
||
155 | } |
||
156 | |||
157 | return $trends; |
||
158 | } |
||
160 |