File::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 1
1
<?php
2
namespace MrPrompt\CaixaEconomicaFederal\Received;
3
4
use MrPrompt\ShipmentCommon\Base\Bank;
5
use MrPrompt\ShipmentCommon\Base\Cart;
6
use MrPrompt\ShipmentCommon\Base\Charge;
7
use MrPrompt\ShipmentCommon\Base\Customer;
8
use MrPrompt\ShipmentCommon\Base\Occurrence;
9
use MrPrompt\ShipmentCommon\Base\Sequence;
10
use MrPrompt\ShipmentCommon\Util\Number;
11
use MrPrompt\CaixaEconomicaFederal\Factory;
12
use MrPrompt\CaixaEconomicaFederal\Received\Partial\Footer;
13
use MrPrompt\CaixaEconomicaFederal\Received\Partial\Header;
14
use DateTime;
15
16
/**
17
 * Billet file class
18
 *
19
 * @author Thiago Paes <[email protected]>
20
 */
21
final class File
22
{
23
    /**
24
     * File name template
25
     *
26
     * @var string
27
     */
28
    const TEMPLATE_GENERATED = '{CLIENT}_{DDMMYYYY}_{SEQUENCE}.TXT.RET';
29
30
    /**
31
     * @var DateTime
32
     */
33
    protected $now;
34
35
    /**
36
     * @var Cart
37
     */
38
    protected $cart;
39
40
    /**
41
     * @var Sequence
42
     */
43
    protected $sequence;
44
45
    /**
46
     * @var Customer
47
     */
48
    protected $customer;
49
50
    /**
51
     * @var string
52
     */
53
    protected $storage;
54
55
    /**
56
     * @var string
57
     */
58
    protected $header;
59
60
    /**
61
     * @var string
62
     */
63
    protected $footer;
64
65
    /**
66
     * @param Customer $customer
67
     * @param Sequence $sequence
68
     * @param DateTime $today
69
     * @param string   $storageDir
70
     */
71 3
    public function __construct(Customer $customer, Sequence $sequence, DateTime $today, $storageDir = null)
72
    {
73 3
        $this->customer     = $customer;
74 3
        $this->sequence     = $sequence;
75 3
        $this->now          = $today;
76 3
        $this->storage      = $storageDir;
77 3
    }
78
79
    /**
80
     * Return the cart
81
     *
82
     * @return Cart
83
     */
84 1
    public function getCart()
85
    {
86 1
        return $this->cart;
87
    }
88
89
    /**
90
     * Set the for with the payments
91
     *
92
     * @param Cart $cart
93
     */
94 1
    public function setCart(Cart $cart)
95
    {
96 1
        $this->cart = $cart;
97 1
    }
98
99
    /**
100
     * Create the file name
101
     *
102
     * @return string
103
     */
104
    protected function createFilename()
105
    {
106
        $search = [
107
            '{CLIENT}',
108
            '{DDMMYYYY}',
109
            '{SEQUENCE}'
110
        ];
111
112
        $replace = [
113
            Number::zeroFill($this->customer->getCode(), 6, Number::FILL_LEFT),
114
            $this->now->format('dmY'),
115
            Number::zeroFill($this->sequence->getValue(), 5, Number::FILL_LEFT),
116
        ];
117
118
        return str_replace($search, $replace, self::TEMPLATE_GENERATED);
119
    }
120
121
    /**
122
     * Read a return file
123
     *
124
     * @param string $file
125
     * @return array [Header, Detail, Footer]
126
     * @throws \Exception
127
     */
128 1
    public function read($file = null)
129
    {
130 1
        $filename       = $file ? $file : $this->createFilename();
131 1
        $inputFile      = $this->storage . DIRECTORY_SEPARATOR . $filename;
132
133
        /* @var $cnab \Cnab\Factory */
134 1
        $cnab           = new \Cnab\Factory();
135 1
        $file           = $cnab->createRetorno($inputFile);
136 1
        $details        = $file->listDetalhes();
137
138
        /* @var $detail \Cnab\Retorno\Cnab240\Detalhe */
139 1
        foreach($details as $detail) {
140
            $result = [
141 1
                'cliente'           => $this->customer->getCode(),
142 1
                'cobranca'          => Charge::BILLET,
143 1
                'ocorrencia'        => Occurrence::INSERT,
144 1
                'identificador'     => $detail->getNumeroDocumento(),
145 1
                'autorizacao'       => $detail->getNossoNumero(),
146
                'parcelas'          => [
147
                    [
148 1
                        'vencimento' => ($detail->getDataVencimento() ? $detail->getDataVencimento()->format('dmY') : null),
149 1
                        'valor'      => $detail->getValorRecebido(),
150 1
                        'quantidade' => 1,  // quantidade de parcelas do bloqueto
151 1
                        'situacao'   => $detail->isBaixa()
152
                    ],
153
                ],
154
                'boleto'            => [
155 1
                    'documento'         => $detail->getNumeroDocumento(),
156
                    'banco'             => [
157 1
                        'codigo'        => Bank::CAIXA_ECONOMICA_FEDERAL,
158 1
                        'agencia'       => $detail->getAgencia(),
159 1
                        'digito'        => $detail->getAgenciaDv(),
160
                        'conta'         => [
161 1
                            'numero'    => $detail->getAgenciaCobradora(),
162 1
                            'digito'    => $detail->getAgenciaCobradoraDac(),
163 1
                            'operacao'  => 003,
164
                        ]
165
                    ],
166
                ],
167
            ];
168
169 1
            $this->cart->addItem( Factory::createDetailFromArray($result) );
170
        }
171
172 1
        $this->header = $this->getHeaderFromFile();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getHeaderFromFile() of type MrPrompt\CaixaEconomicaF...Received\Partial\Header is incompatible with the declared type string of property $header.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
173 1
        $this->footer = $this->getFooterFromFile();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getFooterFromFile() of type MrPrompt\CaixaEconomicaF...Received\Partial\Footer is incompatible with the declared type string of property $footer.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
174
175
        return [
176 1
            $this->header,
177 1
            $this->cart,
178 1
            $this->footer
179
        ];
180
    }
181
182
    /**
183
     * @return Header
184
     */
185
    private function getHeaderFromFile()
186
    {
187
        $header = new Header(
188
            $this->cart->offsetGet(0)->getCustomer(),
189
            $this->cart->offsetGet(0)->getSequence(),
190
            $this->now
191
        );
192
193
        return $header;
194
    }
195
196
    /**
197
     * @return Footer
198
     */
199
    private function getFooterFromFile()
200
    {
201
        $sum = 0;
202
203
        foreach ($this->cart as $item) {
204
            foreach ($item->getParcels() as $item) {
0 ignored issues
show
Comprehensibility Bug introduced by
$item is overwriting a variable from outer foreach loop.
Loading history...
205
                $sum += $item->getPrice();
206
            }
207
        }
208
209
        $footer = new Footer(
210
            $sum,
211
            $this->cart->count(),
212
            $this->cart->offsetGet(0)->getSequence()
213
        );
214
215
        return $footer;
216
    }
217
}
218