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

MissingAncestorsList::handle()   B

Complexity

Conditions 9
Paths 3

Size

Total Lines 52
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 36
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 52
rs 8.0555

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