Passed
Branch feature/2.0 (ef99fd)
by Jonathan
11:25
created

LineagesPage   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 27
c 2
b 0
f 0
dl 0
loc 62
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 32 3
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, 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\Exceptions\HttpNotFoundException;
20
use Fisharebest\Webtrees\Http\ViewResponseTrait;
21
use Fisharebest\Webtrees\Services\IndividualListService;
22
use Fisharebest\Webtrees\Services\ModuleService;
23
use MyArtJaub\Webtrees\Module\PatronymicLineage\PatronymicLineageModule;
24
use MyArtJaub\Webtrees\Module\PatronymicLineage\Model\LineageBuilder;
25
use Psr\Http\Message\ResponseInterface;
26
use Psr\Http\Message\ServerRequestInterface;
27
use Psr\Http\Server\RequestHandlerInterface;
28
29
/**
30
 * Request handler for displaying lineages associated to a surname
31
 */
32
class LineagesPage implements RequestHandlerInterface
33
{
34
    use ViewResponseTrait;
35
36
    /**
37
     * @var PatronymicLineageModule $module
38
     */
39
    private $module;
40
    
41
    /**
42
     * @var IndividualListService $indilist_service
43
     */
44
    private $indilist_service;
45
    
46
    /**
47
     * Constructor for LineagesPage Request handler
48
     *
49
     * @param ModuleService $module_service
50
     * @param IndividualListService $indilist_service
51
     */
52
    public function __construct(ModuleService $module_service, IndividualListService $indilist_service)
53
    {
54
            $this->module = $module_service->findByInterface(PatronymicLineageModule::class)->first();
55
        $this->indilist_service = $indilist_service;
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
        $tree = $request->getAttribute('tree');
69
        assert($tree instanceof Tree);
70
        
71
        $surname = $request->getAttribute('surname');
72
        
73
        $initial = mb_substr($surname, 0, 1);
74
        $initials_list = collect($this->indilist_service->surnameAlpha(false, false, I18N::locale()))
75
            ->reject(function ($count, $initial) {
76
77
                return $initial === '@' || $initial === ',';
78
            });
79
            
80
        $title = I18N::translate('Patronymic Lineages') . ' — ' . $surname;
81
        
82
        $lineages = app()->make(LineageBuilder::class, ['surname' => $surname])->buildLineages();
83
        
84
        return $this->viewResponse($this->module->name() . '::lineages-page', [
85
            'title'         =>  $title,
86
            'module'        =>  $this->module,
87
            'tree'          =>  $tree,
88
            'initials_list' =>  $initials_list,
89
            'initial'       =>  $initial,
90
            'show_all'      =>  'no',
91
            'surname'       =>  $surname,
92
            'lineages'      =>  $lineages,
93
            'nb_lineages'   =>  $lineages->count()
94
        ]);
95
    }
96
}
97