AncestorsList   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 44
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A handle() 0 24 3
1
<?php
2
3
/**
4
 * webtrees-lib: MyArtJaub library for webtrees
5
 *
6
 * @package MyArtJaub\Webtrees
7
 * @subpackage Sosa
8
 * @author Jonathan Jaubart <[email protected]>
9
 * @copyright Copyright (c) 2009-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\Sosa\Http\RequestHandlers;
16
17
use Fisharebest\Webtrees\Auth;
18
use Fisharebest\Webtrees\DefaultUser;
19
use Fisharebest\Webtrees\I18N;
20
use Fisharebest\Webtrees\Tree;
21
use Fisharebest\Webtrees\Validator;
22
use Fisharebest\Webtrees\Http\ViewResponseTrait;
23
use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
24
use Fisharebest\Webtrees\Services\ModuleService;
25
use MyArtJaub\Webtrees\Module\Sosa\SosaModule;
26
use MyArtJaub\Webtrees\Module\Sosa\Services\SosaRecordsService;
27
use MyArtJaub\Webtrees\Module\Sosa\Services\SosaStatisticsService;
28
use Psr\Http\Message\ResponseInterface;
29
use Psr\Http\Message\ServerRequestInterface;
30
use Psr\Http\Server\RequestHandlerInterface;
31
32
/**
33
 * Request handler for listing Sosa ancestors. Only handle the main page, with generation selection.
34
 */
35
class AncestorsList implements RequestHandlerInterface
36
{
37
    use ViewResponseTrait;
38
39
    private ?SosaModule $module;
40
41
    /**
42
     * Constructor for AncestorsList Request Handler
43
     *
44
     * @param ModuleService $module_service
45
     */
46
    public function __construct(ModuleService $module_service)
47
    {
48
        $this->module = $module_service->findByInterface(SosaModule::class)->first();
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     * @see \Psr\Http\Server\RequestHandlerInterface::handle()
54
     */
55
    public function handle(ServerRequestInterface $request): ResponseInterface
56
    {
57
        if ($this->module === null) {
58
            throw new HttpNotFoundException(I18N::translate('The attached module could not be found.'));
59
        }
60
61
        $tree = Validator::attributes($request)->tree();
62
        $user = Auth::check() ? Validator::attributes($request)->user() : new DefaultUser();
63
64
        /** @var SosaStatisticsService $sosa_stats_service */
65
        $sosa_stats_service = app()->makeWith(SosaStatisticsService::class, ['tree' => $tree, 'user' => $user]);
66
67
        $current_gen = Validator::queryParams($request)->integer(
68
            'gen',
69
            Validator::attributes($request)->integer('gen', 0)
70
        );
71
72
        return $this->viewResponse($this->module->name() . '::list-ancestors-page', [
73
            'module_name'       =>  $this->module->name(),
74
            'title'             =>  I18N::translate('Sosa Ancestors'),
75
            'tree'              =>  $tree,
76
            'root_indi'         =>  $sosa_stats_service->rootIndividual(),
77
            'max_gen'           =>  $sosa_stats_service->maxGeneration(),
78
            'current_gen'       =>  $current_gen
79
        ]);
80
    }
81
}
82