Total Complexity | 48 |
Total Lines | 360 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like IndividualPage 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 IndividualPage, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
71 | class IndividualPage implements RequestHandlerInterface |
||
72 | { |
||
73 | use ViewResponseTrait; |
||
74 | |||
75 | /** @var ClipboardService */ |
||
76 | private $clipboard_service; |
||
77 | |||
78 | /** @var ModuleService */ |
||
79 | private $module_service; |
||
80 | |||
81 | /** @var UserService */ |
||
82 | private $user_service; |
||
83 | |||
84 | /** |
||
85 | * IndividualPage constructor. |
||
86 | * |
||
87 | * @param ClipboardService $clipboard_service |
||
88 | * @param ModuleService $module_service |
||
89 | * @param UserService $user_service |
||
90 | */ |
||
91 | public function __construct(ClipboardService $clipboard_service, ModuleService $module_service, UserService $user_service) |
||
92 | { |
||
93 | $this->clipboard_service = $clipboard_service; |
||
94 | $this->module_service = $module_service; |
||
95 | $this->user_service = $user_service; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @param ServerRequestInterface $request |
||
100 | * |
||
101 | * @return ResponseInterface |
||
102 | */ |
||
103 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
104 | { |
||
105 | $tree = $request->getAttribute('tree'); |
||
106 | assert($tree instanceof Tree); |
||
107 | |||
108 | $xref = $request->getAttribute('xref'); |
||
109 | assert(is_string($xref)); |
||
110 | |||
111 | $individual = Individual::getInstance($xref, $tree); |
||
112 | $individual = Auth::checkIndividualAccess($individual); |
||
113 | |||
114 | // Redirect to correct xref/slug |
||
115 | if ($individual->xref() !== $xref || $request->getAttribute('slug') !== $individual->slug()) { |
||
116 | return redirect($individual->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY); |
||
117 | } |
||
118 | |||
119 | // What is (was) the age of the individual |
||
120 | $bdate = $individual->getBirthDate(); |
||
121 | $ddate = $individual->getDeathDate(); |
||
122 | if ($bdate->isOK() && !$individual->isDead()) { |
||
123 | // If living display age |
||
124 | $age = ' (' . I18N::translate('age') . ' ' . FunctionsDate::getAgeAtEvent(Date::getAgeGedcom($bdate, new Date(strtoupper(date('d M Y'))))) . ')'; |
||
125 | } elseif ($bdate->isOK() && $ddate->isOK()) { |
||
126 | // If dead, show age at death |
||
127 | $age = ' (' . I18N::translate('age') . ' ' . FunctionsDate::getAgeAtEvent(Date::getAgeGedcom($bdate, $ddate)) . ')'; |
||
128 | } else { |
||
129 | $age = ''; |
||
130 | } |
||
131 | |||
132 | // What images are linked to this individual |
||
133 | $individual_media = new Collection(); |
||
134 | foreach ($individual->facts(['OBJE']) as $fact) { |
||
135 | $media_object = $fact->target(); |
||
136 | if ($media_object instanceof Media) { |
||
137 | $media_file = $media_object->firstImageFile(); |
||
138 | if ($media_file instanceof MediaFile) { |
||
139 | $individual_media->add($media_file); |
||
140 | } |
||
141 | } |
||
142 | } |
||
143 | |||
144 | $name_records = new Collection(); |
||
145 | foreach ($individual->facts(['NAME']) as $n => $name_fact) { |
||
146 | $name_records->add($this->formatNameRecord($tree, $n, $name_fact)); |
||
147 | } |
||
148 | |||
149 | $sex_records = new Collection(); |
||
150 | foreach ($individual->facts(['SEX']) as $n => $sex_fact) { |
||
151 | $sex_records->add($this->formatSexRecord($sex_fact)); |
||
152 | } |
||
153 | |||
154 | // If this individual is linked to a user account, show the link |
||
155 | $user_link = ''; |
||
156 | if (Auth::isAdmin()) { |
||
157 | $users = $this->user_service->findByIndividual($individual); |
||
158 | foreach ($users as $user) { |
||
159 | $user_link = ' — <a href="' . e(route('admin-users', ['filter' => $user->email()])) . '">' . e($user->userName()) . '</a>'; |
||
160 | } |
||
161 | } |
||
162 | |||
163 | return $this->viewResponse('individual-page', [ |
||
164 | 'age' => $age, |
||
165 | 'clipboard_facts' => $this->clipboard_service->pastableFacts($individual, new Collection()), |
||
166 | 'individual' => $individual, |
||
167 | 'individual_media' => $individual_media, |
||
168 | 'meta_description' => $this->metaDescription($individual), |
||
169 | 'meta_robots' => 'index,follow', |
||
170 | 'name_records' => $name_records, |
||
171 | 'sex_records' => $sex_records, |
||
172 | 'sidebars' => $this->getSidebars($individual), |
||
173 | 'tabs' => $this->getTabs($individual), |
||
174 | 'significant' => $this->significant($individual), |
||
175 | 'title' => $individual->fullName() . ' ' . $individual->lifespan(), |
||
176 | 'tree' => $tree, |
||
177 | 'user_link' => $user_link, |
||
178 | ]); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * @param Individual $individual |
||
183 | * |
||
184 | * @return string |
||
185 | */ |
||
186 | private function metaDescription(Individual $individual): string |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Format a name record |
||
236 | * |
||
237 | * @param Tree $tree |
||
238 | * @param int $n |
||
239 | * @param Fact $fact |
||
240 | * |
||
241 | * @return string |
||
242 | */ |
||
243 | private function formatNameRecord(Tree $tree, $n, Fact $fact): string |
||
244 | { |
||
245 | $individual = $fact->record(); |
||
246 | |||
247 | // Create a dummy record, so we can extract the formatted NAME value from it. |
||
248 | $dummy = new Individual( |
||
249 | 'xref', |
||
250 | "0 @xref@ INDI\n1 DEAT Y\n" . $fact->gedcom(), |
||
251 | null, |
||
252 | $individual->tree() |
||
253 | ); |
||
254 | $dummy->setPrimaryName(0); // Make sure we use the name from "1 NAME" |
||
255 | |||
256 | $container_class = 'card'; |
||
257 | $content_class = 'collapse'; |
||
258 | $aria = 'false'; |
||
259 | |||
260 | if ($n === 0) { |
||
261 | $content_class = 'collapse show'; |
||
262 | $aria = 'true'; |
||
263 | } |
||
264 | if ($fact->isPendingDeletion()) { |
||
265 | $container_class .= ' wt-old'; |
||
266 | } elseif ($fact->isPendingAddition()) { |
||
267 | $container_class .= ' wt-new'; |
||
268 | } |
||
269 | |||
270 | ob_start(); |
||
271 | echo '<dl class="row mb-0"><dt class="col-md-4 col-lg-3">', I18N::translate('Name'), '</dt>'; |
||
272 | echo '<dd class="col-md-8 col-lg-9">', $dummy->fullName(), '</dd>'; |
||
273 | $ct = preg_match_all('/\n2 (\w+) (.*)/', $fact->gedcom(), $nmatch, PREG_SET_ORDER); |
||
274 | for ($i = 0; $i < $ct; $i++) { |
||
275 | $tag = $nmatch[$i][1]; |
||
276 | if ($tag !== 'SOUR' && $tag !== 'NOTE' && $tag !== 'SPFX') { |
||
277 | echo '<dt class="col-md-4 col-lg-3">', GedcomTag::getLabel($tag, $individual), '</dt>'; |
||
278 | echo '<dd class="col-md-8 col-lg-9">'; // Before using dir="auto" on this field, note that Gecko treats this as an inline element but WebKit treats it as a block element |
||
279 | if (isset($nmatch[$i][2])) { |
||
280 | $name = e($nmatch[$i][2]); |
||
281 | $name = str_replace('/', '', $name); |
||
282 | $name = preg_replace('/(\S*)\*/', '<span class="starredname">\\1</span>', $name); |
||
283 | switch ($tag) { |
||
284 | case 'TYPE': |
||
285 | echo GedcomCodeName::getValue($name, $individual); |
||
286 | break; |
||
287 | case 'SURN': |
||
288 | // The SURN field is not necessarily the surname. |
||
289 | // Where it is not a substring of the real surname, show it after the real surname. |
||
290 | $surname = e($dummy->getAllNames()[0]['surname']); |
||
291 | $surns = preg_replace('/, */', ' ', $nmatch[$i][2]); |
||
292 | if (strpos($dummy->getAllNames()[0]['surname'], $surns) !== false) { |
||
293 | echo '<span dir="auto">' . $surname . '</span>'; |
||
294 | } else { |
||
295 | echo I18N::translate('%1$s (%2$s)', '<span dir="auto">' . $surname . '</span>', '<span dir="auto">' . $name . '</span>'); |
||
296 | } |
||
297 | break; |
||
298 | default: |
||
299 | echo '<span dir="auto">' . $name . '</span>'; |
||
300 | break; |
||
301 | } |
||
302 | } |
||
303 | echo '</dd>'; |
||
304 | } |
||
305 | } |
||
306 | echo '</dl>'; |
||
307 | if (strpos($fact->gedcom(), "\n2 SOUR") !== false) { |
||
308 | echo '<div id="indi_sour" class="clearfix">', FunctionsPrintFacts::printFactSources($tree, $fact->gedcom(), 2), '</div>'; |
||
309 | } |
||
310 | if (strpos($fact->gedcom(), "\n2 NOTE") !== false) { |
||
311 | echo '<div id="indi_note" class="clearfix">', FunctionsPrint::printFactNotes($tree, $fact->gedcom(), 2), '</div>'; |
||
312 | } |
||
313 | $content = ob_get_clean(); |
||
314 | |||
315 | if ($fact->canEdit()) { |
||
316 | $edit_links = |
||
317 | '<a class="btn btn-link" href="#" data-confirm="' . I18N::translate('Are you sure you want to delete this fact?') . '" data-post-url="' . e(route(DeleteFact::class, ['tree' => $individual->tree()->name(), 'xref' => $individual->xref(), 'fact_id' => $fact->id()])) . '" title="' . I18N::translate('Delete this name') . '">' . view('icons/delete') . '<span class="sr-only">' . I18N::translate('Delete this name') . '</span></a>' . |
||
318 | '<a class="btn btn-link" href="' . e(route('edit-name', ['xref' => $individual->xref(), 'fact_id' => $fact->id(), 'tree' => $individual->tree()->name()])) . '" title="' . I18N::translate('Edit the name') . '">' . view('icons/edit') . '<span class="sr-only">' . I18N::translate('Edit the name') . '</span></a>'; |
||
319 | } else { |
||
320 | $edit_links = ''; |
||
321 | } |
||
322 | |||
323 | return ' |
||
324 | <div class="' . $container_class . '"> |
||
325 | <div class="card-header" role="tab" id="name-header-' . $n . '"> |
||
326 | <a data-toggle="collapse" data-parent="#individual-names" href="#name-content-' . $n . '" aria-expanded="' . $aria . '" aria-controls="name-content-' . $n . '">' . $dummy->fullName() . '</a> |
||
327 | ' . $edit_links . ' |
||
328 | </div> |
||
329 | <div id="name-content-' . $n . '" class="' . $content_class . '" role="tabpanel" aria-labelledby="name-header-' . $n . '"> |
||
330 | <div class="card-body">' . $content . '</div> |
||
331 | </div> |
||
332 | </div>'; |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * print information for a sex record |
||
337 | * |
||
338 | * @param Fact $fact |
||
339 | * |
||
340 | * @return string |
||
341 | */ |
||
342 | private function formatSexRecord(Fact $fact): string |
||
343 | { |
||
344 | $individual = $fact->record(); |
||
345 | |||
346 | switch ($fact->value()) { |
||
347 | case 'M': |
||
348 | $sex = I18N::translate('Male'); |
||
349 | break; |
||
350 | case 'F': |
||
351 | $sex = I18N::translate('Female'); |
||
352 | break; |
||
353 | default: |
||
354 | $sex = I18N::translateContext('unknown gender', 'Unknown'); |
||
355 | break; |
||
356 | } |
||
357 | |||
358 | $container_class = 'card'; |
||
359 | if ($fact->isPendingDeletion()) { |
||
360 | $container_class .= ' wt-old'; |
||
361 | } elseif ($fact->isPendingAddition()) { |
||
362 | $container_class .= ' wt-new'; |
||
363 | } |
||
364 | |||
365 | if ($individual->canEdit()) { |
||
366 | $edit_links = '<a class="btn btn-link" href="' . e(route(EditFact::class, ['xref' => $individual->xref(), 'fact_id' => $fact->id(), 'tree' => $individual->tree()->name()])) . '" title="' . I18N::translate('Edit the gender') . '">' . view('icons/edit') . '<span class="sr-only">' . I18N::translate('Edit the gender') . '</span></a>'; |
||
367 | } else { |
||
368 | $edit_links = ''; |
||
369 | } |
||
370 | |||
371 | return ' |
||
372 | <div class="' . $container_class . '"> |
||
373 | <div class="card-header" role="tab" id="name-header-add"> |
||
374 | <div class="card-title mb-0"> |
||
375 | <b>' . I18N::translate('Gender') . '</b> ' . $sex . $edit_links . ' |
||
376 | </div> |
||
377 | </div> |
||
378 | </div>'; |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * Which tabs should we show on this individual's page. |
||
383 | * We don't show empty tabs. |
||
384 | * |
||
385 | * @param Individual $individual |
||
386 | * |
||
387 | * @return Collection<ModuleSidebarInterface> |
||
388 | */ |
||
389 | public function getSidebars(Individual $individual): Collection |
||
390 | { |
||
391 | return $this->module_service->findByComponent(ModuleSidebarInterface::class, $individual->tree(), Auth::user()) |
||
392 | ->filter(static function (ModuleSidebarInterface $sidebar) use ($individual): bool { |
||
393 | return $sidebar->hasSidebarContent($individual); |
||
394 | }); |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * Which tabs should we show on this individual's page. |
||
399 | * We don't show empty tabs. |
||
400 | * |
||
401 | * @param Individual $individual |
||
402 | * |
||
403 | * @return Collection<ModuleTabInterface> |
||
404 | */ |
||
405 | public function getTabs(Individual $individual): Collection |
||
410 | }); |
||
411 | } |
||
412 | |||
413 | /** |
||
414 | * What are the significant elements of this page? |
||
415 | * The layout will need them to generate URLs for charts and reports. |
||
416 | * |
||
417 | * @param Individual $individual |
||
418 | * |
||
419 | * @return stdClass |
||
420 | */ |
||
421 | private function significant(Individual $individual): stdClass |
||
431 | ]; |
||
432 | } |
||
434 |