1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of byrokrat\giroapp. |
4
|
|
|
* |
5
|
|
|
* byrokrat\giroapp is free software: you can redistribute it and/or |
6
|
|
|
* modify it under the terms of the GNU General Public License as published |
7
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* byrokrat\giroapp 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
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with byrokrat\giroapp. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
* |
18
|
|
|
* Copyright 2016-20 Hannes Forsgård |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
declare(strict_types = 1); |
22
|
|
|
|
23
|
|
|
namespace byrokrat\giroapp\Status; |
24
|
|
|
|
25
|
|
|
use byrokrat\giroapp\DependencyInjection; |
26
|
|
|
use byrokrat\giroapp\Domain\State; |
27
|
|
|
|
28
|
|
|
final class StandardStatisticsConfigurator |
29
|
|
|
{ |
30
|
|
|
use DependencyInjection\DonorQueryProperty; |
31
|
|
|
|
32
|
|
|
private const STAT_NAMES = [ |
33
|
|
|
'donor-count' => 'Donors', |
34
|
|
|
'monthly-amount' => 'Monthly amount', |
35
|
|
|
'exportable-count' => 'Exportables', |
36
|
|
|
'waiting-count' => 'Awaiting response', |
37
|
|
|
'error-count' => 'Errors', |
38
|
|
|
'revoked-count' => 'Revoked', |
39
|
|
|
'paused-count' => 'Paused', |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** @var array<string, int> */ |
43
|
|
|
private $stats; |
44
|
|
|
|
45
|
|
|
public function loadStatistics(StatisticsManager $statisticsManager): void |
46
|
|
|
{ |
47
|
|
|
foreach (self::STAT_NAMES as $name => $desc) { |
48
|
|
|
$statisticsManager->addStatistic( |
49
|
|
|
new LazyStatistic( |
50
|
|
|
$name, |
51
|
|
|
$desc, |
52
|
|
|
function () use ($name) { |
53
|
|
|
return $this->getStat($name); |
54
|
|
|
} |
55
|
|
|
) |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function getStat(string $name): int |
61
|
|
|
{ |
62
|
|
|
if (!isset($this->stats)) { |
63
|
|
|
$this->buildStats(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (!isset($this->stats[$name])) { |
67
|
|
|
throw new \LogicException("Invalid internal stat $name"); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $this->stats[$name]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function buildStats(): void |
74
|
|
|
{ |
75
|
|
|
$this->stats = [ |
76
|
|
|
'donor-count' => 0, |
77
|
|
|
'monthly-amount' => 0, |
78
|
|
|
'exportable-count' => 0, |
79
|
|
|
'waiting-count' => 0, |
80
|
|
|
'error-count' => 0, |
81
|
|
|
'revoked-count' => 0, |
82
|
|
|
'paused-count' => 0, |
83
|
|
|
]; |
84
|
|
|
|
85
|
|
|
foreach ($this->donorQuery->findAll() as $donor) { |
86
|
|
|
if ($donor->getState() instanceof State\Active) { |
87
|
|
|
$this->stats['donor-count']++; |
88
|
|
|
$this->stats['monthly-amount'] += intval($donor->getDonationAmount()->getAmount()) / 100; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if ($donor->getState() instanceof State\ExportableStateInterface) { |
92
|
|
|
$this->stats['exportable-count']++; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ($donor->getState() instanceof State\AwaitingResponseStateInterface) { |
96
|
|
|
$this->stats['waiting-count']++; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ($donor->getState() instanceof State\Error) { |
100
|
|
|
$this->stats['error-count']++; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if ($donor->getState() instanceof State\Revoked) { |
104
|
|
|
$this->stats['revoked-count']++; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if ($donor->getState() instanceof State\Paused) { |
108
|
|
|
$this->stats['paused-count']++; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|