Completed
Push — master ( 24f6ce...c3efa2 )
by Guilherme
05:36
created

AccountingReport::sortReport()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 16
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 24
rs 8.5125
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)
43
    {
44
        $this->createEntryIfNeeded($client);
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
        return $report;
74
    }
75
76
    private function createEntryIfNeeded(ClientInterface $client)
77
    {
78
        $clientId = $client->getId();
79
        if (array_key_exists($clientId, $this->report)) {
80
            return;
81
        }
82
83
        $initials = $this->systemsRegistry->getSystemInitials($client);
84
        $owners = $this->systemsRegistry->getSystemOwners($client);
85
86
        $this->report[$clientId] = (new AccountingReportEntry())
87
            ->setClient($client)
88
            ->setProcergsInitials($initials)
89
            ->setProcergsOwner($owners)
90
            ->setSystemType($this->getSystemType($clientId))
91
            ->setAccessTokens(0)
92
            ->setApiUsage(0);
93
    }
94
95
    private function getEntry(ClientInterface $client)
96
    {
97
        return $this->report[$client->getId()];
98
    }
99
100
    private function getSystemType($clientId)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
101
    {
102
        if (array_key_exists($clientId, $this->linked)) {
103
            return $this->linked[$clientId]->getSystemType();
104
        }
105
106
        // If there is no known link we assume it's an Internal system
107
        // If this assumption is false then an alarm will go off to alert the accounting team to fix it
108
        return ProcergsLink::TYPE_INTERNAL;
109
    }
110
111
    private function sortReport($report, $order)
112
    {
113
        $order = strtolower(trim($order));
114
        switch ($order) {
115
            case self::SORT_ORDER_ASC:
116
                $order = -1;
117
                break;
118
            case self::SORT_ORDER_DESC:
119
                $order = 1;
120
                break;
121
            default:
122
                throw new \InvalidArgumentException("Invalid sorting order '{$order}'");
123
        }
124
125
        uasort($report, function (AccountingReportEntry $a, AccountingReportEntry $b) use ($order) {
126
            if ($a->getTotalUsage() === $b->getTotalUsage()) {
127
                return 0;
128
            }
129
130
            return ($a->getTotalUsage() < $b->getTotalUsage()) ? $order : $order * -1;
131
        });
132
133
        return $report;
134
    }
135
}
136