Passed
Branch feature/2.0 (be78a0)
by Jonathan
11:57
created

AncestorsList::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 10
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-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\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\Exceptions\HttpNotFoundException;
22
use Fisharebest\Webtrees\Http\ViewResponseTrait;
23
use Fisharebest\Webtrees\Services\ModuleService;
24
use MyArtJaub\Webtrees\Module\Sosa\SosaModule;
25
use MyArtJaub\Webtrees\Module\Sosa\Services\SosaRecordsService;
26
use MyArtJaub\Webtrees\Module\Sosa\Services\SosaStatisticsService;
27
use Psr\Http\Message\ResponseInterface;
28
use Psr\Http\Message\ServerRequestInterface;
29
use Psr\Http\Server\RequestHandlerInterface;
30
31
/**
32
 * Request handler for listing Sosa ancestors. Only handle the main page, with generation selection.
33
 */
34
class AncestorsList implements RequestHandlerInterface
35
{
36
    use ViewResponseTrait;
37
38
    /**
39
     * @var SosaModule $module
40
     */
41
    private $module;
42
43
    /**
44
     * @var SosaRecordsService $sosa_record_service
45
     */
46
    private $sosa_record_service;
47
48
    /**
49
     * Constructor for AncestorsList Request Handler
50
     *
51
     * @param ModuleService $module_service
52
     * @param SosaRecordsService $sosa_record_service
53
     */
54
    public function __construct(
55
        ModuleService $module_service,
56
        SosaRecordsService $sosa_record_service
57
    ) {
58
        $this->module = $module_service->findByInterface(SosaModule::class)->first();
59
        $this->sosa_record_service = $sosa_record_service;
60
    }
61
62
    /**
63
     * {@inheritDoc}
64
     * @see \Psr\Http\Server\RequestHandlerInterface::handle()
65
     */
66
    public function handle(ServerRequestInterface $request): ResponseInterface
67
    {
68
        if ($this->module === null) {
69
            throw new HttpNotFoundException(I18N::translate('The attached module could not be found.'));
70
        }
71
72
        $tree = $request->getAttribute('tree');
73
        assert($tree instanceof Tree);
74
75
        $user = Auth::check() ? $request->getAttribute('user') : new DefaultUser();
76
77
        /** @var SosaStatisticsService $sosa_stats_service */
78
        $sosa_stats_service = app()->makeWith(SosaStatisticsService::class, ['tree' => $tree, 'user' => $user]);
79
80
        $current_gen = (int) ($request->getAttribute('gen') ?? $request->getQueryParams()['gen'] ?? 0);
81
82
        return $this->viewResponse($this->module->name() . '::list-ancestors-page', [
83
            'module_name'       =>  $this->module->name(),
84
            'title'             =>  I18N::translate('Sosa Ancestors'),
85
            'tree'              =>  $tree,
86
            'root_indi'         =>  $sosa_stats_service->rootIndividual(),
87
            'max_gen'           =>  $sosa_stats_service->maxGeneration(),
88
            'current_gen'       =>  $current_gen
89
        ]);
90
    }
91
}
92