Total Complexity | 44 |
Total Lines | 398 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like EventRepository 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 EventRepository, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | class EventRepository |
||
39 | { |
||
40 | /** |
||
41 | * Sorting directions. |
||
42 | */ |
||
43 | private const SORT_ASC = 'ASC'; |
||
44 | private const SORT_DESC = 'DESC'; |
||
45 | |||
46 | /** |
||
47 | * Event facts. |
||
48 | */ |
||
49 | private const EVENT_BIRTH = 'BIRT'; |
||
50 | private const EVENT_DEATH = 'DEAT'; |
||
51 | private const EVENT_MARRIAGE = 'MARR'; |
||
52 | private const EVENT_DIVORCE = 'DIV'; |
||
53 | |||
54 | private Tree $tree; |
||
55 | |||
56 | /** |
||
57 | * @param Tree $tree |
||
58 | */ |
||
59 | public function __construct(Tree $tree) |
||
60 | { |
||
61 | $this->tree = $tree; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @param array<string> $events |
||
66 | */ |
||
67 | private function countEvents(array $events): int |
||
68 | { |
||
69 | return DB::table('dates') |
||
70 | ->where('d_file', '=', $this->tree->id()) |
||
71 | ->whereIn('d_fact', $events) |
||
72 | ->count(); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @param array<string> $events |
||
77 | */ |
||
78 | private function countOtherEvents(array $events): int |
||
79 | { |
||
80 | return DB::table('dates') |
||
81 | ->where('d_file', '=', $this->tree->id()) |
||
82 | ->whereNotIn('d_fact', $events) |
||
83 | ->count(); |
||
84 | } |
||
85 | |||
86 | public function totalEvents(): string |
||
87 | { |
||
88 | return I18N::number($this->countOtherEvents(['CHAN'])); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @return string |
||
93 | */ |
||
94 | public function totalEventsBirth(): string |
||
95 | { |
||
96 | return I18N::number($this->countEvents(Gedcom::BIRTH_EVENTS)); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @return string |
||
101 | */ |
||
102 | public function totalBirths(): string |
||
103 | { |
||
104 | return I18N::number($this->countIndividualsWithEvents([self::EVENT_BIRTH])); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @return string |
||
109 | */ |
||
110 | public function totalEventsDeath(): string |
||
111 | { |
||
112 | return I18N::number($this->countEvents(Gedcom::DEATH_EVENTS)); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @return string |
||
117 | */ |
||
118 | public function totalDeaths(): string |
||
119 | { |
||
120 | return I18N::number($this->countIndividualsWithEvents([self::EVENT_DEATH])); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @return string |
||
125 | */ |
||
126 | public function totalEventsMarriage(): string |
||
127 | { |
||
128 | return I18N::number($this->countEvents(Gedcom::MARRIAGE_EVENTS)); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @return string |
||
133 | */ |
||
134 | public function totalMarriages(): string |
||
135 | { |
||
136 | return I18N::number($this->countFamiliesWithEvents([self::EVENT_MARRIAGE])); |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @return string |
||
141 | */ |
||
142 | public function totalEventsDivorce(): string |
||
143 | { |
||
144 | return I18N::number($this->countEvents(Gedcom::DIVORCE_EVENTS)); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @return string |
||
149 | */ |
||
150 | public function totalDivorces(): string |
||
151 | { |
||
152 | return I18N::number($this->countFamiliesWithEvents([self::EVENT_DIVORCE])); |
||
153 | } |
||
154 | |||
155 | public function totalEventsOther(): string |
||
156 | { |
||
157 | $events = array_merge( |
||
158 | ['CHAN'], |
||
159 | Gedcom::BIRTH_EVENTS, |
||
160 | Gedcom::DEATH_EVENTS, |
||
161 | Gedcom::MARRIAGE_EVENTS, |
||
162 | Gedcom::DIVORCE_EVENTS |
||
163 | ); |
||
164 | |||
165 | return I18N::number($this->countOtherEvents($events)); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Returns the first/last event record from the given list of event facts. |
||
170 | * |
||
171 | * @param string $direction The sorting direction of the query (To return first or last record) |
||
172 | * |
||
173 | * @return object{id:string,year:int,fact:string,type:string}|null |
||
174 | */ |
||
175 | private function eventQuery(string $direction): ?object |
||
176 | { |
||
177 | $events = [ |
||
178 | ...Gedcom::BIRTH_EVENTS, |
||
179 | ...Gedcom::DEATH_EVENTS, |
||
180 | ...Gedcom::MARRIAGE_EVENTS, |
||
181 | ...Gedcom::DIVORCE_EVENTS, |
||
182 | ]; |
||
183 | |||
184 | |||
185 | return DB::table('dates') |
||
186 | ->select(['d_gid as id', 'd_year as year', 'd_fact AS fact', 'd_type AS type']) |
||
187 | ->where('d_file', '=', $this->tree->id()) |
||
188 | ->whereIn('d_fact', $events) |
||
189 | ->where('d_julianday1', '<>', 0) |
||
190 | ->orderBy('d_julianday1', $direction) |
||
191 | ->orderBy('d_type') |
||
192 | ->limit(1) |
||
193 | ->get() |
||
194 | ->map(static fn (object $row): object => (object) [ |
||
195 | 'id' => $row->id, |
||
196 | 'year' => (int) $row->year, |
||
197 | 'fact' => $row->fact, |
||
198 | 'type' => $row->type, |
||
199 | ]) |
||
200 | ->first(); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Returns the formatted first/last occurring event. |
||
205 | * |
||
206 | * @param string $direction The sorting direction |
||
207 | * |
||
208 | * @return string |
||
209 | */ |
||
210 | private function getFirstLastEvent(string $direction): string |
||
211 | { |
||
212 | $row = $this->eventQuery($direction); |
||
213 | $result = I18N::translate('This information is not available.'); |
||
214 | |||
215 | if ($row !== null) { |
||
216 | $record = Registry::gedcomRecordFactory()->make($row->id, $this->tree); |
||
217 | |||
218 | if ($record instanceof GedcomRecord && $record->canShow()) { |
||
219 | $result = $record->formatList(); |
||
220 | } else { |
||
221 | $result = I18N::translate('This information is private and cannot be shown.'); |
||
222 | } |
||
223 | } |
||
224 | |||
225 | return $result; |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * @return string |
||
230 | */ |
||
231 | public function firstEvent(): string |
||
232 | { |
||
233 | return $this->getFirstLastEvent(self::SORT_ASC); |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * @return string |
||
238 | */ |
||
239 | public function lastEvent(): string |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Returns the formatted year of the first/last occurring event. |
||
246 | * |
||
247 | * @param string $direction The sorting direction |
||
248 | * |
||
249 | * @return string |
||
250 | */ |
||
251 | private function getFirstLastEventYear(string $direction): string |
||
252 | { |
||
253 | $row = $this->eventQuery($direction); |
||
254 | |||
255 | if ($row === null) { |
||
256 | return ''; |
||
257 | } |
||
258 | |||
259 | if ($row->year < 0) { |
||
260 | $row->year = abs($row->year) . ' B.C.'; |
||
261 | } |
||
262 | |||
263 | return (new Date($row->type . ' ' . $row->year)) |
||
264 | ->display(); |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * @return string |
||
269 | */ |
||
270 | public function firstEventYear(): string |
||
271 | { |
||
272 | return $this->getFirstLastEventYear(self::SORT_ASC); |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * @return string |
||
277 | */ |
||
278 | public function lastEventYear(): string |
||
279 | { |
||
280 | return $this->getFirstLastEventYear(self::SORT_DESC); |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Returns the formatted type of the first/last occurring event. |
||
285 | * |
||
286 | * @param string $direction The sorting direction |
||
287 | * |
||
288 | * @return string |
||
289 | */ |
||
290 | private function getFirstLastEventType(string $direction): string |
||
291 | { |
||
292 | $row = $this->eventQuery($direction); |
||
293 | |||
294 | if ($row === null) { |
||
295 | return ''; |
||
296 | } |
||
297 | |||
298 | foreach ([Individual::RECORD_TYPE, Family::RECORD_TYPE] as $record_type) { |
||
299 | $element = Registry::elementFactory()->make($record_type . ':' . $row->fact); |
||
300 | |||
301 | if (!$element instanceof UnknownElement) { |
||
302 | return $element->label(); |
||
303 | } |
||
304 | } |
||
305 | |||
306 | return $row->fact; |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * @return string |
||
311 | */ |
||
312 | public function firstEventType(): string |
||
313 | { |
||
314 | return $this->getFirstLastEventType(self::SORT_ASC); |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * @return string |
||
319 | */ |
||
320 | public function lastEventType(): string |
||
321 | { |
||
322 | return $this->getFirstLastEventType(self::SORT_DESC); |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * Returns the formatted name of the first/last occurring event. |
||
327 | * |
||
328 | * @param string $direction The sorting direction |
||
329 | * |
||
330 | * @return string |
||
331 | */ |
||
332 | private function getFirstLastEventName(string $direction): string |
||
333 | { |
||
334 | $row = $this->eventQuery($direction); |
||
335 | |||
336 | if ($row !== null) { |
||
337 | $record = Registry::gedcomRecordFactory()->make($row->id, $this->tree); |
||
338 | |||
339 | if ($record instanceof GedcomRecord) { |
||
340 | return '<a href="' . e($record->url()) . '">' . $record->fullName() . '</a>'; |
||
341 | } |
||
342 | } |
||
343 | |||
344 | return ''; |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * @return string |
||
349 | */ |
||
350 | public function firstEventName(): string |
||
351 | { |
||
352 | return $this->getFirstLastEventName(self::SORT_ASC); |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * @return string |
||
357 | */ |
||
358 | public function lastEventName(): string |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * Returns the formatted place of the first/last occurring event. |
||
365 | * |
||
366 | * @param string $direction The sorting direction |
||
367 | * |
||
368 | * @return string |
||
369 | */ |
||
370 | private function getFirstLastEventPlace(string $direction): string |
||
371 | { |
||
372 | $row = $this->eventQuery($direction); |
||
373 | |||
374 | if ($row !== null) { |
||
375 | $record = Registry::gedcomRecordFactory()->make($row->id, $this->tree); |
||
376 | $fact = null; |
||
377 | |||
378 | if ($record instanceof GedcomRecord) { |
||
379 | $fact = $record->facts([$row->fact])->first(); |
||
380 | } |
||
381 | |||
382 | if ($fact instanceof Fact) { |
||
383 | return $fact->place()->shortName(); |
||
384 | } |
||
385 | } |
||
386 | |||
387 | return I18N::translate('Private'); |
||
388 | } |
||
389 | |||
390 | /** |
||
391 | * @return string |
||
392 | */ |
||
393 | public function firstEventPlace(): string |
||
394 | { |
||
395 | return $this->getFirstLastEventPlace(self::SORT_ASC); |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * @return string |
||
400 | */ |
||
401 | public function lastEventPlace(): string |
||
404 | } |
||
405 | |||
406 | /** |
||
407 | * @param array<string> $events |
||
408 | */ |
||
409 | private function countFamiliesWithEvents(array $events): int |
||
410 | { |
||
411 | return DB::table('dates') |
||
412 | ->join('families', static function (JoinClause $join): void { |
||
413 | $join |
||
414 | ->on('f_id', '=', 'd_gid') |
||
415 | ->on('f_file', '=', 'd_file'); |
||
416 | }) |
||
417 | ->where('d_file', '=', $this->tree->id()) |
||
418 | ->whereIn('d_fact', $events) |
||
419 | ->count(); |
||
420 | } |
||
421 | |||
422 | /** |
||
423 | * @param array<string> $events |
||
424 | */ |
||
425 | private function countIndividualsWithEvents(array $events): int |
||
426 | { |
||
427 | return DB::table('dates') |
||
428 | ->join('individuals', static function (JoinClause $join): void { |
||
429 | $join |
||
436 | } |
||
437 | } |
||
438 |