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

AccountingService::addReportEntry()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 39
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 25
nc 12
nop 5
dl 0
loc 39
rs 6.7272
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\Service;
12
13
use LoginCidadao\OAuthBundle\Entity\Client;
14
use LoginCidadao\OAuthBundle\Entity\ClientRepository;
15
use PROCERGS\Generic\Traits\OptionalLoggerAwareTrait;
16
use PROCERGS\LoginCidadao\AccountingBundle\Entity\ProcergsLinkRepository;
17
use PROCERGS\LoginCidadao\AccountingBundle\Model\AccountingReport;
18
use PROCERGS\LoginCidadao\AccountingBundle\Model\GcsInterface;
19
use Psr\Log\LoggerAwareInterface;
20
21
class AccountingService implements LoggerAwareInterface
22
{
23
    use OptionalLoggerAwareTrait;
24
25
    /** @var SystemsRegistryService */
26
    private $systemsRegistry;
27
28
    /** @var ClientRepository */
29
    private $clientRepository;
30
31
    /** @var ProcergsLinkRepository */
32
    private $procergsLinkRepository;
33
34
    /**
35
     * AccountingService constructor.
36
     * @param SystemsRegistryService $systemsRegistry
37
     * @param ClientRepository $clientRepository
38
     * @param ProcergsLinkRepository $procergsLinkRepository
39
     */
40
    public function __construct(
41
        SystemsRegistryService $systemsRegistry,
42
        ClientRepository $clientRepository,
43
        ProcergsLinkRepository $procergsLinkRepository
44
    ) {
45
        $this->systemsRegistry = $systemsRegistry;
46
        $this->clientRepository = $clientRepository;
47
        $this->procergsLinkRepository = $procergsLinkRepository;
48
    }
49
50
    /**
51
     * @param \DateTime $start
52
     * @param \DateTime $end
53
     * @return AccountingReport
54
     */
55
    public function getAccounting(\DateTime $start, \DateTime $end)
56
    {
57
        $this->log('info', "Getting accounting between {$start->format('c')} and {$end->format('c')}");
58
        $start->setTime(0, 0, 0);
59
        $end->setTime(0, 0, 0);
60
61
        $data = $this->clientRepository->getAccessTokenAccounting($start, $end);
62
        $actionLog = $this->clientRepository->getActionLogAccounting($start, $end);
63
64
        $this->log('info', "Loaded accounting data");
65
66
        $clientIds = array_unique(array_merge(
67
            array_column($data, 'id'),
68
            array_column($actionLog, 'id')
69
        ));
70
71
        $clients = [];
72
        /** @var Client $client */
73
        foreach ($this->clientRepository->findBy(['id' => $clientIds]) as $client) {
74
            $clients[$client->getId()] = $client;
75
        }
76
77
        $this->log('info', "Loading linked clients...");
78
        $linked = $this->systemsRegistry->fetchLinked($clients, $this->procergsLinkRepository);
79
80
        $this->log('info', "Preparing AccountingReport object...");
81
        $report = new AccountingReport($this->systemsRegistry, $linked);
82
        foreach ($data as $usage) {
83
            /** @var \LoginCidadao\OAuthBundle\Entity\Client $client */
84
            $report->addEntry($clients[$usage['id']], $usage['access_tokens'], null);
85
        }
86
87
        foreach ($actionLog as $action) {
88
            /** @var \LoginCidadao\OAuthBundle\Entity\Client $client */
89
            $report->addEntry($clients[$action['id']], null, $action['api_usage']);
90
        }
91
        $this->log('info', "AccountingReport object ready.");
92
93
        return $report;
94
    }
95
96
    public function getGcsInterface($interfaceName, \DateTime $start, \DateTime $end)
97
    {
98
        $data = $this->getAccounting($start, $end)->getReport(['include_inactive' => false]);
99
100
        $this->log('info', "Preparing GCS Interface...");
101
        $gcsInterface = new GcsInterface($interfaceName, $start, ['ignore_externals' => true]);
102
103
        foreach ($data as $client) {
104
            $this->log('info',
105
                "Including {$client->getClient()->getPublicId()} into GCS Interface...",
106
                ['entry' => $client]
107
            );
108
            $gcsInterface->addClient($client);
109
        }
110
        $this->log('info', "GCS Interface object is ready.");
111
112
        $response = $gcsInterface->__toString();
113
        $this->log('info', "Resulting GCS Interface length: ".strlen($response), ['response' => $response]);
114
115
        return $response;
116
    }
117
}
118