ChartDeath   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 77
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A queryRecords() 0 15 1
A chartDeath() 0 26 2
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2023 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Statistics\Google;
21
22
use Fisharebest\Webtrees\DB;
23
use Fisharebest\Webtrees\I18N;
24
use Fisharebest\Webtrees\Statistics\Service\CenturyService;
25
use Fisharebest\Webtrees\Statistics\Service\ColorService;
26
use Fisharebest\Webtrees\Tree;
27
use Illuminate\Support\Collection;
28
use stdClass;
29
30
use function count;
31
use function view;
32
33
/**
34
 * A chart showing the deaths by century.
35
 */
36
class ChartDeath
37
{
38
    private Tree $tree;
39
40
    private CenturyService $century_service;
41
42
    private ColorService $color_service;
43
44
    /**
45
     * @param CenturyService $century_service
46
     * @param ColorService   $color_service
47
     * @param Tree           $tree
48
     */
49
    public function __construct(CenturyService $century_service, ColorService $color_service, Tree $tree)
50
    {
51
        $this->century_service = $century_service;
52
        $this->color_service   = $color_service;
53
        $this->tree            = $tree;
54
    }
55
56
    /**
57
     * Returns the related database records.
58
     *
59
     * @return Collection<array-key,stdClass>
60
     */
61
    private function queryRecords(): Collection
62
    {
63
        return DB::table('dates')
64
            ->selectRaw('ROUND((d_year + 49) / 100, 0) AS century')
65
            ->selectRaw('COUNT(*) AS total')
66
            ->where('d_file', '=', $this->tree->id())
67
            ->where('d_year', '<>', 0)
68
            ->where('d_fact', '=', 'DEAT')
69
            ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
70
            ->groupBy(['century'])
71
            ->orderBy('century')
72
            ->get()
73
            ->map(static fn (object $row): object => (object) [
74
                'century' => (int) $row->century,
75
                'total'   => (float) $row->total,
76
            ]);
77
    }
78
79
    /**
80
     * Create a chart of death places.
81
     *
82
     * @param string|null $color_from
83
     * @param string|null $color_to
84
     *
85
     * @return string
86
     */
87
    public function chartDeath(string|null $color_from = null, string|null $color_to = null): string
88
    {
89
        $color_from ??= 'ffffff';
90
        $color_to ??= '84beff';
91
92
        $data = [
93
            [
94
                I18N::translate('Century'),
95
                I18N::translate('Total')
96
            ],
97
        ];
98
99
        foreach ($this->queryRecords() as $record) {
100
            $data[] = [
101
                $this->century_service->centuryName($record->century),
102
                $record->total
103
            ];
104
        }
105
106
        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
107
108
        return view('statistics/other/charts/pie', [
109
            'title'    => I18N::translate('Deaths by century'),
110
            'data'     => $data,
111
            'colors'   => $colors,
112
            'language' => I18N::languageTag(),
113
        ]);
114
    }
115
}
116