MissingAncestorsList::handle()   B
last analyzed

Complexity

Conditions 9
Paths 3

Size

Total Lines 53
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 37
nc 3
nop 1
dl 0
loc 53
rs 7.7724
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Registry;
21
use Fisharebest\Webtrees\Tree;
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\Data\MissingAncestor;
28
use MyArtJaub\Webtrees\Module\Sosa\Services\SosaRecordsService;
29
use MyArtJaub\Webtrees\Module\Sosa\Services\SosaStatisticsService;
30
use Psr\Http\Message\ResponseInterface;
31
use Psr\Http\Message\ServerRequestInterface;
32
use Psr\Http\Server\RequestHandlerInterface;
33
use stdClass;
34
35
/**
36
 * Request handler for listing missing Sosa ancestors
37
 */
38
class MissingAncestorsList implements RequestHandlerInterface
39
{
40
    use ViewResponseTrait;
41
42
    /**
43
     * @var SosaModule|null $module
44
     */
45
    private $module;
46
47
    /**
48
     * @var SosaRecordsService $sosa_record_service
49
     */
50
    private $sosa_record_service;
51
52
    /**
53
     * Constructor for MissingAncestorsList Request Handler
54
     *
55
     * @param ModuleService $module_service
56
     * @param SosaRecordsService $sosa_record_service
57
     */
58
    public function __construct(
59
        ModuleService $module_service,
60
        SosaRecordsService $sosa_record_service
61
    ) {
62
        $this->module = $module_service->findByInterface(SosaModule::class)->first();
63
        $this->sosa_record_service = $sosa_record_service;
64
    }
65
66
    /**
67
     * {@inheritDoc}
68
     * @see \Psr\Http\Server\RequestHandlerInterface::handle()
69
     */
70
    public function handle(ServerRequestInterface $request): ResponseInterface
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
79
        /** @var SosaStatisticsService $sosa_stats_service */
80
        $sosa_stats_service = app()->makeWith(SosaStatisticsService::class, ['tree' => $tree, 'user' => $user]);
81
82
        $current_gen =  Validator::queryParams($request)->integer(
83
            'gen',
84
            Validator::attributes($request)->integer('gen', 0)
85
        );
86
87
        $list_missing = $this->sosa_record_service->listMissingAncestorsAtGeneration($tree, $user, $current_gen);
88
        $nb_missing_diff = $list_missing->sum(function (stdClass $value): int {
89
            return ($value->majs_fat_id === null ? 1 : 0) + ($value->majs_mot_id === null ? 1 : 0);
90
        });
91
92
        $list_missing = $list_missing->map(function (stdClass $value) use ($tree): ?MissingAncestor {
93
            $indi = Registry::individualFactory()->make($value->majs_i_id, $tree);
94
            if ($indi !== null && $indi->canShowName()) {
95
                return new MissingAncestor(
96
                    $indi,
97
                    (int) $value->majs_sosa,
98
                    $value->majs_fat_id === null,
99
                    $value->majs_mot_id === null
100
                );
101
            }
102
            return null;
103
        })->filter();
104
105
        $nb_missing_shown = $list_missing->sum(function (MissingAncestor $value): int {
106
            return ($value->isFatherMissing() ? 1 : 0) + ($value->isMotherMissing() ? 1 : 0);
107
        });
108
109
        return $this->viewResponse($this->module->name() . '::list-missing-page', [
110
            'module_name'       =>  $this->module->name(),
111
            'title'             =>  I18N::translate('Missing Ancestors'),
112
            'tree'              =>  $tree,
113
            'root_indi'         =>  $sosa_stats_service->rootIndividual(),
114
            'max_gen'           =>  $sosa_stats_service->maxGeneration(),
115
            'current_gen'       =>  $current_gen,
116
            'list_missing'      =>  $list_missing,
117
            'nb_missing_diff'   =>  $nb_missing_diff,
118
            'nb_missing_shown'  =>  $nb_missing_shown,
119
            'gen_completeness'  =>
120
                $sosa_stats_service->totalAncestorsAtGeneration($current_gen) / pow(2, $current_gen - 1),
121
            'gen_potential'     =>
122
                $sosa_stats_service->totalAncestorsAtGeneration($current_gen - 1) / pow(2, $current_gen - 2)
123
        ]);
124
    }
125
}
126