Failed Conditions
Push — issue#752 ( ae1705...8960ee )
by Guilherme
08:24
created

AccountingReport::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace PROCERGS\LoginCidadao\AccountingBundle\Model;
12
13
use LoginCidadao\OAuthBundle\Model\ClientInterface;
14
use PROCERGS\LoginCidadao\AccountingBundle\Entity\ProcergsLink;
15
use PROCERGS\LoginCidadao\AccountingBundle\Service\SystemsRegistryService;
16
17
class AccountingReport
18
{
19
    const SORT_ORDER_ASC = 'asc';
20
    const SORT_ORDER_DESC = 'desc';
21
22
    /** @var SystemsRegistryService */
23
    private $systemsRegistry;
24
25
    /** @var ProcergsLink[] */
26
    private $linked;
27
28
    /** @var AccountingReportEntry[] */
29
    private $report = [];
30
31
    /**
32
     * AccountingReport constructor.
33
     * @param SystemsRegistryService $systemsRegistry
34
     * @param array $linked
35
     */
36
    public function __construct(SystemsRegistryService $systemsRegistry, array $linked = [])
37
    {
38
        $this->systemsRegistry = $systemsRegistry;
39
        $this->linked = $linked;
40
    }
41
42
    public function addEntry(ClientInterface $client, $accessTokens = null, $apiUsage = null, $lazyLoad = false)
43
    {
44
        $this->createEntryIfNeeded($client, !$lazyLoad);
45
        $entry = $this->getEntry($client);
46
47
        if ($accessTokens) {
48
            $entry->setAccessTokens($accessTokens);
49
        }
50
        if ($apiUsage) {
51
            $entry->setApiUsage($apiUsage);
52
        }
53
    }
54
55
    public function getReport(array $options = [])
56
    {
57
        $report = $this->report;
58
59
        $options = array_merge([
60
            'include_inactive' => true,
61
            'sort' => null,
62
        ], $options);
63
        if (!$options['include_inactive']) {
64
            $report = array_filter($this->report, function (AccountingReportEntry $entry) {
65
                return $entry->getTotalUsage() > 0;
66
            });
67
        }
68
69
        if ($options['sort'] !== null) {
70
            $report = $this->sortReport($report, $options['sort']);
71
        }
72
73
        $systemsRegistry = $this->systemsRegistry;
74
        $report = array_map(function (AccountingReportEntry $entry) use ($systemsRegistry) {
75
            if (false === $entry->isQueriedSystemsRegistry()) {
76
                $client = $entry->getClient();
77
                $entry->setProcergsInitials($systemsRegistry->getSystemInitials($client));
78
                $entry->setProcergsOwner($systemsRegistry->getSystemOwners($client));
79
                $entry->setQueriedSystemsRegistry(true);
80
            }
81
82
            return $entry;
83
        }, $report);
84
85
        return $report;
86
    }
87
88
    private function createEntryIfNeeded(ClientInterface $client, $querySystemsRegistry = true)
89
    {
90
        $clientId = $client->getId();
91
        if (array_key_exists($clientId, $this->report)) {
92
            return;
93
        }
94
95
        $initials = null;
96
        $owners = null;
97
        if ($querySystemsRegistry) {
98
            $initials = $this->systemsRegistry->getSystemInitials($client);
99
            $owners = $this->systemsRegistry->getSystemOwners($client);
100
        }
101
102
        $this->report[$clientId] = (new AccountingReportEntry())
103
            ->setClient($client)
104
            ->setProcergsInitials($initials)
105
            ->setProcergsOwner($owners)
106
            ->setSystemType($this->getSystemType($clientId))
107
            ->setAccessTokens(0)
108
            ->setApiUsage(0)
109
            ->setQueriedSystemsRegistry($querySystemsRegistry);
110
    }
111
112
    private function getEntry(ClientInterface $client)
113
    {
114
        return $this->report[$client->getId()];
115
    }
116
117
    private function getSystemType($clientId)
118
    {
119
        if (array_key_exists($clientId, $this->linked)) {
120
            return $this->linked[$clientId]->getSystemType();
121
        }
122
123
        // If there is no known link we assume it's an Internal system
124
        // If this assumption is false then an alarm will go off to alert the accounting team to fix it
125
        return ProcergsLink::TYPE_INTERNAL;
126
    }
127
128
    private function sortReport($report, $order)
129
    {
130
        $order = strtolower(trim($order));
131
        switch ($order) {
132
            case self::SORT_ORDER_ASC:
133
                $order = -1;
134
                break;
135
            case self::SORT_ORDER_DESC:
136
                $order = 1;
137
                break;
138
            default:
139
                throw new \InvalidArgumentException("Invalid sorting order '{$order}'");
140
        }
141
142
        uasort($report, function (AccountingReportEntry $a, AccountingReportEntry $b) use ($order) {
143
            if ($a->getTotalUsage() === $b->getTotalUsage()) {
144
                return 0;
145
            }
146
147
            return ($a->getTotalUsage() < $b->getTotalUsage()) ? $order : $order * -1;
148
        });
149
150
        return $report;
151
    }
152
}
153