ECF   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 1
dl 0
loc 145
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A add() 0 10 3
A get() 0 13 3
B totalize() 0 37 6
B adjustI030J900() 0 40 5
1
<?php
2
3
namespace NFePHP\ECF;
4
5
use NFePHP\ECF\Common\BlockInterface;
6
7
class ECF
8
{
9
    // protected $possibles = [
10
    //     'block0' => ['class' => Blocks\Block0::class, 'order' => 1], //Abertura, Identificação e Referências
11
    //     'blockc' => ['class' => Blocks\BlockC::class, 'order' => 2], //Informações Recuperadas da ECF Anterior
12
    //     'blocki' => ['class' => Blocks\BlockI::class, 'order' => 3], //Lançamentos Contábeis
13
    //     'blockj' => ['class' => Blocks\BlockJ::class, 'order' => 4], //Demonstrações Contábeis
14
    //     'blockk' => ['class' => Blocks\BlockK::class, 'order' => 5], //Conglomerados Econômicos
15
    // ];
16
17
    protected $possibles = [
18
        'block0' => ['class' => Blocks\Block0::class, 'order' => 1], // Abertura, Identificação e Referências
19
        'blockq' => ['class' => Blocks\BlockQ::class, 'order' => 2], // Livro Caixa
20
    ];
21
22
    protected $qtd_lin = 0;
23
24
    public function __construct()
25
    {
26
        //todo
27
    }
28
29
    /**
30
     * Add
31
     * @param BlockInterface $block
32
     */
33
    public function add(BlockInterface $block = null)
34
    {
35
        if (empty($block)) {
36
            return;
37
        }
38
        $name = strtolower((new \ReflectionClass($block))->getShortName());
39
        if (key_exists($name, $this->possibles)) {
40
            $this->{$name} = $block->get();
41
        }
42
    }
43
44
    /**
45
     * Create a ECF string
46
     */
47
    public function get()
48
    {
49
        $ecf = '';
50
        $keys = array_keys($this->possibles);
51
        foreach ($keys as $key) {
52
            if (isset($this->$key)) {
53
                $ecf .= $this->$key;
54
            }
55
        }
56
        $ecf .= $this->totalize($ecf);
57
        $ecf = $this->adjustI030J900($ecf);
58
        return $ecf;
59
    }
60
61
    /**
62
     * Totals blocks contents
63
     * @param string $ecf
64
     * @return string
65
     */
66
    protected function totalize($ecf)
67
    {
68
        $tot = '';
69
        $keys = [];
70
        $aecf = explode("\n", $ecf);
71
        foreach ($aecf as $element) {
72
            $param = explode("|", $element);
73
            if (!empty($param[1])) {
74
                $key = $param[1];
75
                if (!empty($keys[$key])) {
76
                    $keys[$key] += 1;
77
                } else {
78
                    $keys[$key] = 1;
79
                }
80
            }
81
        }
82
        //Inicializa o bloco 9
83
        $tot .= "|9001|0|\n";
84
        $n = 0;
85
        foreach ($keys as $key => $value) {
86
            if (!empty($key)) {
87
                $tot .= "|9900|$key|$value|\n";
88
                $n++;
89
            }
90
        }
91
        $n++;
92
        $tot .= "|9900|9001|1|\n";
93
        $tot .= "|9900|9900|". ($n+3)."|\n";
94
        $tot .= "|9900|9990|1|\n";
95
        $tot .= "|9900|9999|1|\n";
96
        $tot .= "|9990|". ($n+6) ."|\n";
97
        $ecf .= $tot;
98
        $n = count(explode("\n", $ecf));
99
        $tot .= "|9999|$n|\n";
100
        $this->qtd_lin = $n;
101
        return $tot;
102
    }
103
104
    /**
105
     * Ajusta os campos qtd_lin dos elementos I030 e J900
106
     *
107
     * @param string $ecf
108
     *
109
     * @return string
110
     */
111
    protected function adjustI030J900($ecf)
112
    {
113
        $aecf = explode("\n", $ecf);
114
        $i = 0;
115
        foreach ($aecf as $element) {
116
            $param = explode("|", $element);
117
            if (!empty($param[1])) {
118
                if ($param[1] == 'I030') {
119
                    $i030 = "|I030"
120
                    . "|{$param[2]}"
121
                    . "|{$param[3]}"
122
                    . "|{$param[4]}"
123
                    . "|{$this->qtd_lin}"
124
                    . "|{$param[6]}"
125
                    . "|{$param[7]}"
126
                    . "|{$param[8]}"
127
                    . "|{$param[9]}"
128
                    . "|{$param[10]}"
129
                    . "|{$param[11]}"
130
                    . "|{$param[12]}|";
131
                    $aecf[$i] = $i030;
132
                }
133
                if ($param[1] == 'J900') {
134
                    $j900 = "|J900"
135
                        . "|{$param[2]}"
136
                        . "|{$param[3]}"
137
                        . "|{$param[4]}"
138
                        . "|{$param[5]}"
139
                        . "|{$this->qtd_lin}"
140
                        . "|{$param[7]}"
141
                        . "|{$param[8]}|";
142
                    $aecf[$i] = $j900;
143
                    //ultimo campo a ser ajustado pode parar o loop
144
                    break;
145
                }
146
                $i++;
147
            }
148
        }
149
        return implode("\n", $aecf);
150
    }
151
}
152