Passed
Push — 2.1 ( 438dd8...4d644f )
by Greg
15:45 queued 07:28
created

StatisticsService   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 99
c 1
b 0
f 0
dl 0
loc 167
rs 9.6
wmc 35

13 Methods

Rating   Name   Duplication   Size   Complexity  
A countIndividuals() 0 5 1
A __construct() 0 3 1
A countEvents() 0 6 1
D centuryName() 0 52 23
A countFamilies() 0 5 1
A countRepositories() 0 6 1
A countAllRecords() 0 9 1
A countMedia() 0 5 1
A countSources() 0 5 1
A countNotes() 0 6 1
A countOtherEvents() 0 6 1
A countEventsByCentury() 0 13 1
A countIndividualsBySex() 0 6 1
1
<?php
2
3
namespace Fisharebest\Webtrees\Services;
4
5
use Fisharebest\Webtrees\I18N;
6
use Fisharebest\Webtrees\Tree;
7
use Illuminate\Database\Query\Expression;
8
use Illuminate\Database\Capsule\Manager as DB;
9
10
use function strip_tags;
11
12
/**
13
 * Generate raw data for statistics
14
 */
15
class StatisticsService
16
{
17
    private Tree $tree;
18
19
    public function __construct(Tree $tree)
20
    {
21
        $this->tree = $tree;
22
    }
23
24
    public function countAllRecords(): int
25
    {
26
        return
27
            $this->countIndividuals() +
28
            $this->countFamilies() +
29
            $this->countMedia() +
30
            $this->countNotes() +
31
            $this->countRepositories() +
32
            $this->countSources();
33
    }
34
35
    /**
36
     * @param array<string> $events
37
     */
38
    public function countEvents(array $events): int
39
    {
40
        return DB::table('dates')
41
            ->where('d_file', '=', $this->tree->id())
42
            ->whereIn('d_fact', $events)
43
            ->count();
44
    }
45
46
    /**
47
     * @return array<int,array{0:string,1:int}>
48
     */
49
    public function countEventsByCentury(string $event): array
50
    {
51
        return DB::table('dates')
52
            ->select([new Expression('ROUND((d_year + 49) / 100, 0) AS century'), new Expression('COUNT(*) AS total')])
53
            ->where('d_file', '=', $this->tree->id())
54
            ->where('d_year', '<>', 0)
55
            ->where('d_fact', '=', $event)
56
            ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
57
            ->groupBy(['century'])
58
            ->orderBy('century')
59
            ->get()
60
            ->map(fn (object $row): array => [$this->centuryName((int) $row->century), (int) $row->total])
61
            ->all();
62
    }
63
64
    public function countFamilies(): int
65
    {
66
        return DB::table('families')
67
            ->where('f_file', '=', $this->tree->id())
68
            ->count();
69
    }
70
71
    public function countIndividuals(): int
72
    {
73
        return DB::table('individuals')
74
            ->where('i_file', '=', $this->tree->id())
75
            ->count();
76
    }
77
78
    public function countIndividualsBySex(string $sex): int
79
    {
80
        return DB::table('individuals')
81
            ->where('i_file', '=', $this->tree->id())
82
            ->where('i_sex', '=', $sex)
83
            ->count();
84
    }
85
86
    public function countMedia(): int
87
    {
88
        return DB::table('media')
89
            ->where('m_file', '=', $this->tree->id())
90
            ->count();
91
    }
92
93
    public function countNotes(): int
94
    {
95
        return DB::table('other')
96
            ->where('o_file', '=', $this->tree->id())
97
            ->where('o_type', '=', 'NOTE')
98
            ->count();
99
    }
100
101
    /**
102
     * @param array<string> $events
103
     */
104
    public function countOtherEvents(array $events): int
105
    {
106
        return DB::table('dates')
107
            ->where('d_file', '=', $this->tree->id())
108
            ->whereNotIn('d_fact', $events)
109
            ->count();
110
    }
111
112
    public function countRepositories(): int
113
    {
114
        return DB::table('other')
115
            ->where('o_file', '=', $this->tree->id())
116
            ->where('o_type', '=', 'REPO')
117
            ->count();
118
    }
119
120
    public function countSources(): int
121
    {
122
        return DB::table('sources')
123
            ->where('s_file', '=', $this->tree->id())
124
            ->count();
125
    }
126
127
    /**
128
     * Century name, English => 21st, Polish => XXI, etc.
129
     */
130
    private function centuryName(int $century): string
131
    {
132
        if ($century < 0) {
133
            return I18N::translate('%s BCE', $this->centuryName(-$century));
134
        }
135
136
        // The current chart engine (Google charts) can't handle <sup></sup> markup
137
        switch ($century) {
138
            case 21:
139
                return strip_tags(I18N::translateContext('CENTURY', '21st'));
140
            case 20:
141
                return strip_tags(I18N::translateContext('CENTURY', '20th'));
142
            case 19:
143
                return strip_tags(I18N::translateContext('CENTURY', '19th'));
144
            case 18:
145
                return strip_tags(I18N::translateContext('CENTURY', '18th'));
146
            case 17:
147
                return strip_tags(I18N::translateContext('CENTURY', '17th'));
148
            case 16:
149
                return strip_tags(I18N::translateContext('CENTURY', '16th'));
150
            case 15:
151
                return strip_tags(I18N::translateContext('CENTURY', '15th'));
152
            case 14:
153
                return strip_tags(I18N::translateContext('CENTURY', '14th'));
154
            case 13:
155
                return strip_tags(I18N::translateContext('CENTURY', '13th'));
156
            case 12:
157
                return strip_tags(I18N::translateContext('CENTURY', '12th'));
158
            case 11:
159
                return strip_tags(I18N::translateContext('CENTURY', '11th'));
160
            case 10:
161
                return strip_tags(I18N::translateContext('CENTURY', '10th'));
162
            case 9:
163
                return strip_tags(I18N::translateContext('CENTURY', '9th'));
164
            case 8:
165
                return strip_tags(I18N::translateContext('CENTURY', '8th'));
166
            case 7:
167
                return strip_tags(I18N::translateContext('CENTURY', '7th'));
168
            case 6:
169
                return strip_tags(I18N::translateContext('CENTURY', '6th'));
170
            case 5:
171
                return strip_tags(I18N::translateContext('CENTURY', '5th'));
172
            case 4:
173
                return strip_tags(I18N::translateContext('CENTURY', '4th'));
174
            case 3:
175
                return strip_tags(I18N::translateContext('CENTURY', '3rd'));
176
            case 2:
177
                return strip_tags(I18N::translateContext('CENTURY', '2nd'));
178
            case 1:
179
                return strip_tags(I18N::translateContext('CENTURY', '1st'));
180
            default:
181
                return ($century - 1) . '01-' . $century . '00';
182
        }
183
    }
184
}
185