LineagesPage::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * webtrees-lib: MyArtJaub library for webtrees
5
 *
6
 * @package MyArtJaub\Webtrees
7
 * @subpackage PatronymicLineage
8
 * @author Jonathan Jaubart <[email protected]>
9
 * @copyright Copyright (c) 2020-2022, Jonathan Jaubart
10
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3
11
 */
12
13
declare(strict_types=1);
14
15
namespace MyArtJaub\Webtrees\Module\PatronymicLineage\Http\RequestHandlers;
16
17
use Fisharebest\Webtrees\I18N;
18
use Fisharebest\Webtrees\Tree;
19
use Fisharebest\Webtrees\Validator;
20
use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
21
use Fisharebest\Webtrees\Http\ViewResponseTrait;
22
use Fisharebest\Webtrees\Module\IndividualListModule;
23
use Fisharebest\Webtrees\Services\ModuleService;
24
use MyArtJaub\Webtrees\Module\PatronymicLineage\PatronymicLineageModule;
25
use MyArtJaub\Webtrees\Module\PatronymicLineage\Model\LineageBuilder;
26
use Psr\Http\Message\ResponseInterface;
27
use Psr\Http\Message\ServerRequestInterface;
28
use Psr\Http\Server\RequestHandlerInterface;
29
30
/**
31
 * Request handler for displaying lineages associated to a surname
32
 */
33
class LineagesPage implements RequestHandlerInterface
34
{
35
    use ViewResponseTrait;
36
37
    /**
38
     * @var PatronymicLineageModule|null $module
39
     */
40
    private $module;
41
42
    /**
43
     * @var IndividualListModule|null $indilist_module
44
     */
45
    private $indilist_module;
46
47
    /**
48
     * Constructor for LineagesPage Request handler
49
     *
50
     * @param ModuleService $module_service
51
     */
52
    public function __construct(ModuleService $module_service)
53
    {
54
        $this->module = $module_service->findByInterface(PatronymicLineageModule::class)->first();
55
        $this->indilist_module = $module_service->findByInterface(IndividualListModule::class)->first();
56
    }
57
58
    /**
59
     * {@inheritDoc}
60
     * @see \Psr\Http\Server\RequestHandlerInterface::handle()
61
     */
62
    public function handle(ServerRequestInterface $request): ResponseInterface
63
    {
64
        if ($this->module === null) {
65
            throw new HttpNotFoundException(I18N::translate('The attached module could not be found.'));
66
        }
67
68
        if ($this->indilist_module === null) {
69
            throw new HttpNotFoundException(I18N::translate('There is no module to handle individual lists.'));
70
        }
71
72
        $tree = Validator::attributes($request)->tree();
73
        $surname = Validator::attributes($request)->string('surname', '');
74
75
        $initial = mb_substr($surname, 0, 1);
76
        $initials_list = collect($this->indilist_module->surnameAlpha($tree, false, false, I18N::locale()))
77
            ->reject(function (int $count, string $initial): bool {
78
                return $initial === '@' || $initial === ',';
79
            });
80
81
        $title = I18N::translate('Patronymic Lineages') . ' — ' . $surname;
82
83
        $lineages = app()->make(LineageBuilder::class, ['surname' => $surname])->buildLineages();
84
85
        return $this->viewResponse($this->module->name() . '::lineages-page', [
86
            'title'         =>  $title,
87
            'module'        =>  $this->module,
88
            'tree'          =>  $tree,
89
            'initials_list' =>  $initials_list,
90
            'initial'       =>  $initial,
91
            'show_all'      =>  'no',
92
            'surname'       =>  $surname,
93
            'lineages'      =>  $lineages,
94
            'nb_lineages'   =>  $lineages !== null ? $lineages->count() : 0
95
        ]);
96
    }
97
}
98