StandardStatisticsConfigurator::loadStatistics()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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