SurnamesList   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 55
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 35 5
A __construct() 0 3 1
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\ViewResponseTrait;
21
use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
22
use Fisharebest\Webtrees\Module\IndividualListModule;
23
use Fisharebest\Webtrees\Services\ModuleService;
24
use MyArtJaub\Webtrees\Module\PatronymicLineage\PatronymicLineageModule;
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 list of surnames
31
 */
32
class SurnamesList implements RequestHandlerInterface
33
{
34
    use ViewResponseTrait;
35
36
    private ?PatronymicLineageModule $module;
37
38
    /**
39
     * Constructor for SurnamesList Request Handler
40
     *
41
     * @param ModuleService $module_service
42
     */
43
    public function __construct(ModuleService $module_service)
44
    {
45
        $this->module = $module_service->findByInterface(PatronymicLineageModule::class)->first();
46
    }
47
48
    /**
49
     * {@inheritDoc}
50
     * @see \Psr\Http\Server\RequestHandlerInterface::handle()
51
     */
52
    public function handle(ServerRequestInterface $request): ResponseInterface
53
    {
54
        if ($this->module === null) {
55
            throw new HttpNotFoundException(I18N::translate('The attached module could not be found.'));
56
        }
57
58
        $tree = Validator::attributes($request)->tree();
59
        $initial = Validator::attributes($request)->string('alpha', '');
60
61
        $initials_list = collect($this->module->surnameAlpha($tree, false, false, I18N::locale()))
62
            ->reject(function (int $count, string $initial): bool {
63
                return $initial === '@' || $initial === ',';
64
            });
65
66
        $show_all = Validator::queryParams($request)->string('show_all', 'no');
67
68
        if ($show_all === 'yes') {
69
            $title = I18N::translate('Patronymic Lineages') . ' — ' . I18N::translate('All');
70
            $surnames = $this->module->surnames($tree, '', '', false, false, I18N::locale());
71
        } elseif (mb_strlen($initial) === 1) {
72
            $title = I18N::translate('Patronymic Lineages') . ' — ' . $initial;
73
            $surnames = $this->module->surnames($tree, '', $initial, false, false, I18N::locale());
74
        } else {
75
            $title =  I18N::translate('Patronymic Lineages');
76
            $surnames = [];
77
        }
78
79
        return $this->viewResponse($this->module->name() . '::surnames-page', [
80
            'title'         =>  $title,
81
            'module'        =>  $this->module,
82
            'tree'          =>  $tree,
83
            'initials_list' =>  $initials_list,
84
            'initial'       =>  $initial,
85
            'show_all'      =>  $show_all,
86
            'surnames'      =>  $surnames
87
        ]);
88
    }
89
}
90