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

SurnamesList::handle()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 37
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 26
c 2
b 0
f 0
nc 4
nop 1
dl 0
loc 37
rs 8.8817
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 Psr\Http\Message\ResponseInterface;
25
use Psr\Http\Message\ServerRequestInterface;
26
use Psr\Http\Server\RequestHandlerInterface;
27
28
/**
29
 * Request handler for displaying list of surnames
30
 */
31
class SurnamesList implements RequestHandlerInterface
32
{
33
    use ViewResponseTrait;
34
35
    /**
36
     * @var PatronymicLineageModule $module
37
     */
38
    private $module;
39
    
40
    /**
41
     * @var IndividualListService $indilist_service
42
     */
43
    private $indilist_service;
44
    
45
    /**
46
     * Constructor for SurnamesList Request Handler
47
     *
48
     * @param ModuleService $module_service
49
     * @param IndividualListService $indilist_service
50
     */
51
    public function __construct(ModuleService $module_service, IndividualListService $indilist_service)
52
    {
53
        $this->module = $module_service->findByInterface(PatronymicLineageModule::class)->first();
54
        $this->indilist_service = $indilist_service;
55
    }
56
    
57
    /**
58
     * {@inheritDoc}
59
     * @see \Psr\Http\Server\RequestHandlerInterface::handle()
60
     */
61
    public function handle(ServerRequestInterface $request): ResponseInterface
62
    {
63
        if ($this->module === null) {
64
            throw new HttpNotFoundException(I18N::translate('The attached module could not be found.'));
65
        }
66
        
67
        $tree = $request->getAttribute('tree');
68
        assert($tree instanceof Tree);
69
        
70
        $initial = $request->getAttribute('alpha');
71
        $initials_list = collect($this->indilist_service->surnameAlpha(false, false, I18N::locale()))
72
            ->reject(function ($count, $initial) {
73
74
                return $initial === '@' || $initial === ',';
75
            });
76
            
77
        $show_all = $request->getQueryParams()['show_all'] ?? 'no';
78
        
79
        if ($show_all === 'yes') {
80
            $title = I18N::translate('Patronymic Lineages') . ' — ' . I18N::translate('All');
81
            $surnames = $this->indilist_service->surnames('', '', false, false, I18N::locale());
82
        } elseif ($initial !== null && mb_strlen($initial) == 1) {
83
            $title = I18N::translate('Patronymic Lineages') . ' — ' . $initial;
84
            $surnames = $this->indilist_service->surnames('', $initial, false, false, I18N::locale());
85
        } else {
86
            $title =  I18N::translate('Patronymic Lineages');
87
            $surnames = [];
88
        }
89
        
90
        return $this->viewResponse($this->module->name() . '::surnames-page', [
91
            'title'         =>  $title,
92
            'module'        =>  $this->module,
93
            'tree'          =>  $tree,
94
            'initials_list' =>  $initials_list,
95
            'initial'       =>  $initial,
96
            'show_all'      =>  $show_all,
97
            'surnames'      =>  $surnames
98
        ]);
99
    }
100
}
101