AncestorsListFamily::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
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 Fig\Http\Message\StatusCodeInterface;
18
use Fisharebest\Webtrees\Auth;
19
use Fisharebest\Webtrees\DefaultUser;
20
use Fisharebest\Webtrees\I18N;
21
use Fisharebest\Webtrees\Registry;
22
use Fisharebest\Webtrees\Validator;
23
use Fisharebest\Webtrees\Http\ViewResponseTrait;
24
use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
25
use Fisharebest\Webtrees\Services\ModuleService;
26
use MyArtJaub\Webtrees\Module\Sosa\SosaModule;
27
use MyArtJaub\Webtrees\Module\Sosa\Services\SosaRecordsService;
28
use Psr\Http\Message\ResponseInterface;
29
use Psr\Http\Message\ServerRequestInterface;
30
use Psr\Http\Server\RequestHandlerInterface;
31
use stdClass;
32
33
/**
34
 * Request handler for tab listing the Sosa families. Provide an AJAX response.
35
 */
36
class AncestorsListFamily implements RequestHandlerInterface
37
{
38
    use ViewResponseTrait;
39
40
    /**
41
     * @var SosaModule|null $module
42
     */
43
    private $module;
44
45
    /**
46
     * @var SosaRecordsService $sosa_record_service
47
     */
48
    private $sosa_record_service;
49
50
    /**
51
     * Constructor for AncestorsListFamily Request Handler
52
     *
53
     * @param ModuleService $module_service
54
     * @param SosaRecordsService $sosa_record_service
55
     */
56
    public function __construct(
57
        ModuleService $module_service,
58
        SosaRecordsService $sosa_record_service
59
    ) {
60
        $this->module = $module_service->findByInterface(SosaModule::class)->first();
61
        $this->sosa_record_service = $sosa_record_service;
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     * @see \Psr\Http\Server\RequestHandlerInterface::handle()
67
     */
68
    public function handle(ServerRequestInterface $request): ResponseInterface
69
    {
70
        $this->layout = 'layouts/ajax';
71
72
        if ($this->module === null) {
73
            throw new HttpNotFoundException(I18N::translate('The attached module could not be found.'));
74
        }
75
76
        $tree = Validator::attributes($request)->tree();
77
        $user = Auth::check() ? Validator::attributes($request)->user() : new DefaultUser();
78
        $current_gen = Validator::attributes($request)->integer('gen', 0);
79
80
        if ($current_gen <= 0) {
81
            return Registry::responseFactory()->response(
82
                'Invalid generation',
83
                StatusCodeInterface::STATUS_UNPROCESSABLE_ENTITY
84
            );
85
        }
86
87
        $list_families = $this->sosa_record_service->listAncestorFamiliesAtGeneration($tree, $user, $current_gen);
88
        $nb_families_all = $list_families->count();
89
90
        /** @var \Illuminate\Support\Collection<int, \Fisharebest\Webtrees\Family> $list_families */
91
        $list_families = $list_families
92
            ->filter(function (stdClass $value) use ($tree): bool {
93
                $fam = Registry::familyFactory()->make($value->f_id, $tree);
94
                return $fam !== null && $fam->canShow();
95
            })
96
            ->mapWithKeys(function (stdClass $value) use ($tree): array {
97
                $fam = Registry::familyFactory()->make($value->f_id, $tree);
98
                return [(int) $value->majs_sosa => $fam];
99
            });
100
101
        $nb_families_shown = $list_families->count();
102
103
        return $this->viewResponse($this->module->name() . '::list-ancestors-fam-tab', [
104
            'module_name'       =>  $this->module->name(),
105
            'title'             =>  I18N::translate('Sosa Ancestors'),
106
            'tree'              =>  $tree,
107
            'list_families'     =>  $list_families,
108
            'nb_families_all'   =>  $nb_families_all,
109
            'nb_families_shown' =>  $nb_families_shown
110
        ]);
111
    }
112
}
113