SosaByGenerationGeoAnalysis::results()   B
last analyzed

Complexity

Conditions 7
Paths 5

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 5
nop 2
dl 0
loc 30
rs 8.6666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * webtrees-lib: MyArtJaub library for webtrees
5
 *
6
 * @package MyArtJaub\Webtrees
7
 * @subpackage GeoDispersion
8
 * @author Jonathan Jaubart <[email protected]>
9
 * @copyright Copyright (c) 2021-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\GeoAnalyses;
16
17
use Fisharebest\Webtrees\Auth;
18
use Fisharebest\Webtrees\DefaultUser;
19
use Fisharebest\Webtrees\I18N;
20
use Fisharebest\Webtrees\Individual;
21
use Fisharebest\Webtrees\Registry;
22
use Fisharebest\Webtrees\Tree;
23
use MyArtJaub\Webtrees\Common\GeoDispersion\GeoAnalysis\GeoAnalysisPlace;
24
use MyArtJaub\Webtrees\Common\GeoDispersion\GeoAnalysis\GeoAnalysisResults;
25
use MyArtJaub\Webtrees\Contracts\GeoDispersion\GeoAnalysisInterface;
26
use MyArtJaub\Webtrees\Module\Sosa\Services\SosaRecordsService;
27
use Generator;
28
use stdClass;
29
30
/**
31
 * Analyse the geographical dispersion of the ancestors, detailed by century.
32
 */
33
class SosaByGenerationGeoAnalysis implements GeoAnalysisInterface
34
{
35
    private SosaRecordsService $records_service;
36
37
    /**
38
     * Constructor for SosaByGenerationGeoAnalysis
39
     *
40
     * @param SosaRecordsService $records_service
41
     */
42
    public function __construct(SosaRecordsService $records_service)
43
    {
44
        $this->records_service = $records_service;
45
    }
46
47
    /**
48
     * {@inheritDoc}
49
     * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\GeoAnalysisInterface::title()
50
     */
51
    public function title(): string
52
    {
53
        return I18N::translate('Sosa ancestors places by generation');
54
    }
55
56
    /**
57
     * {@inheritDoc}
58
     * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\GeoAnalysisInterface::itemsDescription()
59
     */
60
    public function itemsDescription(): callable
61
    {
62
        return fn(int $count): string => I18N::plural('ancestor', 'ancestors', $count);
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\GeoAnalysisInterface::results()
68
     */
69
    public function results(Tree $tree, int $depth): GeoAnalysisResults
70
    {
71
        $results = new GeoAnalysisResults();
72
73
        $unique_ancestors = $this->records_service
74
            ->listAncestors($tree, Auth::check() ? Auth::user() : new DefaultUser())
75
            ->uniqueStrict(fn(stdClass $item): string => $item->majs_i_id);
76
77
        foreach ($unique_ancestors as $item) {
78
            $ancestor = Registry::individualFactory()->make($item->majs_i_id, $tree);
79
            if ($ancestor === null || !$ancestor->canShow()) {
80
                continue;
81
            }
82
            $generation = $this->records_service->generation((int) $item->majs_sosa);
83
            $significantplace = new GeoAnalysisPlace($tree, null, $depth);
84
            foreach ($this->significantPlaces($ancestor) as $place) {
85
                $significantplace = new GeoAnalysisPlace($tree, $place, $depth, true);
86
                if ($significantplace->isKnown()) {
87
                    break;
88
                }
89
            }
90
            $results->addPlace($significantplace);
91
            $results->addPlaceInCategory(
92
                I18N::translate('Generation %s', I18N::number($generation)),
93
                $generation,
94
                $significantplace
95
            );
96
        }
97
98
        return $results;
99
    }
100
101
    /**
102
     * Returns significant places in order of priority for an individual
103
     *
104
     * @param Individual $individual
105
     * @return Generator<\Fisharebest\Webtrees\Place>
106
     */
107
    protected function significantPlaces(Individual $individual): Generator
108
    {
109
        yield $individual->getBirthPlace();
110
111
        /** @var \Fisharebest\Webtrees\Fact $fact */
112
        foreach ($individual->facts(['RESI']) as $fact) {
113
            yield $fact->place();
114
        }
115
116
        yield $individual->getDeathPlace();
117
118
        /** @var \Fisharebest\Webtrees\Family $family */
119
        foreach ($individual->childFamilies() as $family) {
120
            foreach ($family->facts(['RESI']) as $fact) {
121
                yield $fact->place();
122
            }
123
        }
124
125
        /** @var \Fisharebest\Webtrees\Family $family */
126
        foreach ($individual->spouseFamilies() as $family) {
127
            foreach ($family->facts(['RESI']) as $fact) {
128
                yield $fact->place();
129
            }
130
        }
131
    }
132
}
133