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

GcsInterface::getStaticOwnerAndInitialsBody()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 3
dl 0
loc 9
rs 9.6666
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 PROCERGS\LoginCidadao\AccountingBundle\Entity\ProcergsLink;
14
15
class GcsInterface
16
{
17
    /** @var string */
18
    private $interfaceName;
19
20
    /** @var \DateTime */
21
    private $start;
22
23
    /** @var array */
24
    private $config;
25
26
    private $procergsSystems = [];
27
    private $externalSystems = [];
28
    private $invalidSystems = [];
29
30
    /**
31
     * GcsInterface constructor.
32
     * @param string $interfaceName
33
     * @param \DateTime $start
34
     * @param array $config
35
     */
36
    public function __construct($interfaceName, \DateTime $start, array $config = [])
37
    {
38
        $this->interfaceName = $interfaceName;
39
        $this->start = $start;
40
41
        $this->config = array_merge([
42
            'ignore_externals' => false,
43
            'external_label' => 'EXTERNAL',
44
        ], $config);
45
    }
46
47
    public function addClient(AccountingReportEntry $reportEntry)
48
    {
49
        switch ($reportEntry->getSystemType()) {
50
            case ProcergsLink::TYPE_EXTERNAL:
51
                $this->externalSystems[] = $reportEntry->getTotalUsage();
52
                continue;
53
            case ProcergsLink::TYPE_INTERNAL:
54
                $this->countProcergsSystem($reportEntry);
55
                continue;
56
            default:
57
                $this->registerInvalidEntry($reportEntry);
58
                continue;
59
        }
60
    }
61
62
    public function getHeader()
63
    {
64
        return implode(';', [
65
            '1',
66
            $this->interfaceName,
67
            $this->start->format('mY'),
68
            (new \DateTime())->format('dmY'),
69
        ]);
70
    }
71
72
    public function getBody()
73
    {
74
        $body = array_merge(
75
            $this->getProcergsSystemsBody(),
76
            $this->getExternalSystemsBody(),
77
            $this->getInvalidSystemsBody()
78
        );
79
80
        return implode("\n", $body);
81
    }
82
83
    public function getTail()
84
    {
85
        $body = array_merge(
86
            $this->getProcergsSystemsBody(),
87
            $this->getExternalSystemsBody(),
88
            $this->getInvalidSystemsBody()
89
        );
90
91
        return '9;'.count($body);
92
    }
93
94
    private function getProcergsSystemsBody()
95
    {
96
        $body = [];
97
        foreach ($this->procergsSystems as $initials => $sysInfo) {
98
            $body[] = implode(';', ['2', $sysInfo['owner'], $initials, $sysInfo['usage']]);
99
        }
100
101
        return $body;
102
    }
103
104
    private function getExternalSystemsBody()
105
    {
106
        if ($this->config['ignore_externals']) {
107
            return [];
108
        }
109
110
        return $this->getStaticOwnerAndInitialsBody($this->externalSystems, $this->config['external_label'],
111
            $this->config['external_label']);
112
    }
113
114
    private function getInvalidSystemsBody()
115
    {
116
        return $this->getStaticOwnerAndInitialsBody($this->invalidSystems);
117
    }
118
119
    private function getStaticOwnerAndInitialsBody($entries, $owner = '', $initials = '')
120
    {
121
        $body = [];
122
        foreach ($entries as $usage) {
123
            $body[] = implode(';', ['2', $owner, $initials, $usage]);
124
        }
125
126
        return $body;
127
    }
128
129
    private function countProcergsSystem(AccountingReportEntry $reportEntry)
130
    {
131
        $procergsInitials = $reportEntry->getProcergsInitials();
132
        $totalUsage = $reportEntry->getTotalUsage();
133
134
        if (count($procergsInitials) !== 1) {
135
            $this->registerInvalidEntry($reportEntry);
136
137
            return;
138
        }
139
140
        $owners = implode(' ', $reportEntry->getProcergsOwner());
141
        foreach ($procergsInitials as $initials) {
142
            $this->procergsSystems[$initials]['owner'] = $owners;
143
            if (array_key_exists('usage', $this->procergsSystems[$initials])) {
144
                $this->procergsSystems[$initials]['usage'] += $totalUsage;
145
            } else {
146
                $this->procergsSystems[$initials]['usage'] = $totalUsage;
147
            }
148
        }
149
    }
150
151
    private function registerInvalidEntry(AccountingReportEntry $reportEntry)
152
    {
153
        $this->invalidSystems[] = $reportEntry->getTotalUsage();
154
    }
155
156
    public function __toString()
157
    {
158
        $header = $this->getHeader();
159
        $body = $this->getBody();
160
        $tail = $this->getTail();
161
162
        return $header.PHP_EOL.
163
            $body.PHP_EOL.
164
            $tail;
165
    }
166
}
167