File   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 403
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 23

Test Coverage

Coverage 29.7%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 23
dl 0
loc 403
ccs 52
cts 175
cp 0.297
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A getCart() 0 4 1
A setCart() 0 4 1
A getFooter() 0 4 1
A setFooter() 0 4 1
A getHeader() 0 4 1
A setHeader() 0 4 1
A createFilename() 0 16 1
A getTotalPrice() 0 10 2
A generateContent() 0 27 2
A save() 0 17 2
B read() 0 112 6
A createPurchaser() 0 16 1
A createParcels() 0 32 1
A createAuthorization() 0 7 1
1
<?php
2
namespace MrPrompt\Centercob\Shipment;
3
4
use DateTime;
5
use MrPrompt\ShipmentCommon\Base\Bank;
6
use MrPrompt\ShipmentCommon\Base\Cart;
7
use MrPrompt\ShipmentCommon\Base\Email;
8
use MrPrompt\ShipmentCommon\Base\Phone;
9
use MrPrompt\ShipmentCommon\Base\Charge;
10
use MrPrompt\ShipmentCommon\Base\Holder;
11
use MrPrompt\ShipmentCommon\Base\Parcel;
12
use MrPrompt\ShipmentCommon\Base\Seller;
13
use MrPrompt\ShipmentCommon\Util\Number;
14
use MrPrompt\ShipmentCommon\Base\Address;
15
use MrPrompt\ShipmentCommon\Base\Parcels;
16
use MrPrompt\ShipmentCommon\Base\Customer;
17
use MrPrompt\ShipmentCommon\Base\Document;
18
use MrPrompt\ShipmentCommon\Base\Sequence;
19
use MrPrompt\ShipmentCommon\Base\Purchaser;
20
use MrPrompt\ShipmentCommon\Base\CreditCard;
21
use MrPrompt\ShipmentCommon\Base\Occurrence;
22
use MrPrompt\ShipmentCommon\Base\BankAccount;
23
use MrPrompt\Centercob\Shipment\Partial\Detail;
24
use MrPrompt\Centercob\Shipment\Partial\Footer;
25
use MrPrompt\Centercob\Shipment\Partial\Header;
26
use MrPrompt\ShipmentCommon\Base\Authorization;
27
use MrPrompt\ShipmentCommon\Base\ConsumerUnity;
28
29
/**
30
 * Shipment file class
31
 *
32
 * @author Thiago Paes <[email protected]>
33
 */
34
class File
35
{
36
    /**
37
     * File name template
38
     *
39
     * @var string
40
     */
41
    const TEMPLATE_GENERATED = '{CLIENT}_{DDMMYYYY}_{SEQUENCE}.TXT';
42
43
    /**
44
     * File name template
45
     *
46
     * @var string
47
     */
48
    const TEMPLATE_PROCESSED = '{CLIENT}_{DDMMYYYY}_{SEQUENCE}.TXT.RET';
49
50
    /**
51
     * @var DateTime
52
     */
53
    private $now;
54
55
    /**
56
     * @var string
57
     */
58
    private $content;
59
60
    /**
61
     * @var Header
62
     */
63
    private $header;
64
65
    /**
66
     * @var Cart
67
     */
68
    private $cart;
69
70
    /**
71
     * @var Footer
72
     */
73
    private $footer;
74
75
    /**
76
     * @var Sequence
77
     */
78
    private $sequence;
79
80
    /**
81
     * @var Customer
82
     */
83
    private $customer;
84
85
    /**
86
     * @var string
87
     */
88
    private $storage;
89
90
    /**
91
     * @var string
92
     */
93
    private $template;
94
95
    /**
96
     * @param Customer $customer
97
     * @param Sequence $sequence
98
     * @param DateTime $today
99
     * @param string   $storageDir
100
     */
101 8
    public function __construct(
102
        Customer $customer,
103
        Sequence $sequence,
104
        DateTime $today,
105
        $storageDir = null
106
    ) {
107 8
        $this->customer     = $customer;
108 8
        $this->now          = $today;
109 8
        $this->sequence     = $sequence;
110 8
        $this->storage      = $storageDir;
111 8
        $this->content      = null;
112 8
    }
113
114
    /**
115
     * @return Cart
116
     */
117 1
    public function getCart()
118
    {
119 1
        return $this->cart;
120
    }
121
122
    /**
123
     * @param Cart $cart
124
     */
125 1
    public function setCart(Cart $cart)
126
    {
127 1
        $this->cart = $cart;
128 1
    }
129
130
    /**
131
     * @return Footer
132
     */
133 1
    public function getFooter()
134
    {
135 1
        return $this->footer;
136
    }
137
138
    /**
139
     * @param Footer $footer
140
     */
141 1
    public function setFooter(Footer $footer)
142
    {
143 1
        $this->footer = $footer;
144 1
    }
145
146
    /**
147
     * @return Header
148
     */
149 1
    public function getHeader()
150
    {
151 1
        return $this->header;
152
    }
153
154
    /**
155
     * @param Header $header
156
     */
157 1
    public function setHeader(Header $header)
158
    {
159 1
        $this->header = $header;
160 1
    }
161
162
    /**
163
     * Create the file name
164
     *
165
     * @return string
166
     */
167
    private function createFilename($type = self::TEMPLATE_GENERATED)
168
    {
169
        $search = [
170
            '{CLIENT}',
171
            '{DDMMYYYY}',
172
            '{SEQUENCE}'
173
        ];
174
175
        $replace = [
176
            Number::zeroFill($this->customer->getCode(), 6, Number::FILL_LEFT),
177
            $this->now->format('dmY'),
178
            Number::zeroFill($this->sequence->getValue(), 5, Number::FILL_LEFT),
179
        ];
180
181
        return str_replace($search, $replace, $type);
182
    }
183
184
    /**
185
     * @param $parcels
186
     * @return int
187
     */
188
    private function getTotalPrice($parcels)
189
    {
190
        $price = 0;
191
192
        foreach ($parcels as $parcel) {
193
            $price += $parcel->getPrice();
194
        }
195
196
        return $price;
197
    }
198
199
    /**
200
     * @return string
201
     */
202
    private function generateContent()
203
    {
204
        $totalPrice     = 0;
205
        $counter        = 2;
206
207
        $this->header = new Header($this->customer, $this->sequence, $this->now);
208
        $this->content  = $this->header->render() . PHP_EOL;
209
210
        /* @var $detail \Centercob\Gateway\Shipment\Partial\Detail */
211
        foreach ($this->cart as $detail) {
212
            $detail->getSequence()->setValue($counter);
213
214
            $this->content .= $detail->render() . PHP_EOL;
215
216
            $totalPrice += $this->getTotalPrice( $detail->getParcels() );
217
            $counter++;
218
        }
219
220
        $sequence = clone $this->sequence;
221
        $sequence->setValue($counter);
222
223
        $this->footer = new Footer(($counter - 1), $totalPrice, $sequence);
224
225
        $this->content .= $this->footer->render();
226
227
        return $this->content;
228
    }
229
230
    /**
231
     * @return string
232
     */
233 1
    public function save()
234
    {
235 1
        if (null === $this->content) {
236 1
            $this->content = $this->generateContent();
237
        }
238
239 1
        $this->template = self::TEMPLATE_GENERATED;
240
241 1
        $this->header->setSequence($this->sequence);
242
243 1
        $filename   = $this->createFilename($this->template);
244 1
        $outputFile = $this->storage . DIRECTORY_SEPARATOR . $filename;
245
246 1
        file_put_contents($outputFile, $this->content);
247
248 1
        return $outputFile;
249
    }
250
251
    /**
252
     * @return string
253
     */
254 1
    public function read()
255
    {
256 1
        $this->template = self::TEMPLATE_GENERATED;
257
258 1
        $file = $this->storage . DIRECTORY_SEPARATOR . $this->createFilename($this->template);
259
260 1
        $this->content  = file_get_contents($file);
261
262 1
        $details        = explode(PHP_EOL, $this->content);
263 1
        $headerLine     = array_shift($details);
264 1
        $footerLine     = array_pop($details);
265
266 1
        if (null == $footerLine) {
267
            $footerLine = array_pop($details);
268
        }
269
270 1
        $this->header   = new Header(
271 1
            $this->customer,
272 1
            new Sequence(substr($headerLine, 1000, 6)),
273 1
            $this->now
274
        );
275
276 1
        $this->footer   = new Footer(
277 1
            substr($footerLine, 1, 6),
278 1
            substr($footerLine, 7, 10),
279 1
            (new Sequence(substr($footerLine, 1000, 6)))
280
        );
281
282 1
        $this->cart = new Cart();
283
284
        /* @var $detail \Centercob\Gateway\Received\Partial\Detail */
285 1
        foreach ($details as $row) {
286
            $charge         = new Charge();
287
            $card           = new CreditCard();
288
            $account        = new BankAccount(new Bank(), new Holder());
289
            $unity          = new ConsumerUnity();
290
            $seller         = new Seller();
291
292
            $parcels        = $this->createParcels($row);
293
            $authorization  = $this->createAuthorization($row);
294
            $sequence       = new Sequence(substr($row, 1000, 6));
295
296
            // extracting object from line
297
            $unity->setNumber(substr($row, 9, 25));
298
            $unity->setRead(DateTime::createFromFormat('dmY', substr($row, 141, 8)));
0 ignored issues
show
Security Bug introduced by
It seems like \DateTime::createFromFor..., substr($row, 141, 8)) targeting DateTime::createFromFormat() can also be of type false; however, MrPrompt\ShipmentCommon\...onsumerUnity::setRead() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?
Loading history...
299
            $unity->setMaturity(DateTime::createFromFormat('dmY', substr($row, 149, 8)));
0 ignored issues
show
Security Bug introduced by
It seems like \DateTime::createFromFor..., substr($row, 149, 8)) targeting DateTime::createFromFormat() can also be of type false; however, MrPrompt\ShipmentCommon\...merUnity::setMaturity() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?
Loading history...
300
301
            $occurrence = new Occurrence();
302
            $occurrence->setType(substr($row, 44, 1));
303
            $occurrence->setReturn(substr($row, 783, 2));
304
            $occurrence->setDescription(substr($row, 785, 100));
305
306
            $charge->setCharging(substr($row, 8, 1));
307
            $charge->setOccurrence($occurrence);
308
309
            $address = new Address();
310
            $address->setCity(substr($row, 69, 50));
311
            $address->setState(substr($row, 119, 2));
312
            $address->setPostalCode(substr($row, 121, 8));
313
            $address->setAddress(substr($row, 461, 50));
314
            $address->setNumber(substr($row, 511, 6));
315
            $address->setDistrict(substr($row, 517, 30));
316
            $address->setComplement(substr($row, 547, 30));
317
318
            $seller->setCode(substr($row, 135, 6));
319
320
            $document = new Document();
321
            $document->setNumber(substr($row, 368, 15));
322
323
            $account->getHolder()->setName(substr($row, 158, 70));
324
            $account->getHolder()->setDocument($document);
325
326
            switch ($charge->getCharging()) {
327
                case Charge::CREDIT_CARD:
328
                    $card->setFlag(substr(substr($row, 129, 6), -2));
329
                    break;
330
331
                case Charge::DEBIT:
332
                    $account->getBank()->setCode(substr(substr($row, 129, 6), -2));
333
                    break;
334
335
                case Charge::ENERGY:
336
                    $unity->setCode(substr(substr($row, 129, 6), -2));
337
                    break;
338
            }
339
340
            $purchaser = $this->createPurchaser($row);
341
            $purchaser->setAddress($address);
342
            $purchaser->setDocument($document);
343
344
            $detail = new Detail(
345
                $this->customer,
346
                $charge,
347
                $seller,
348
                $purchaser,
349
                $parcels,
350
                $authorization,
351
                $card,
352
                $account,
353
                $unity,
354
                $sequence
355
            );
356
357
            $this->cart->append($detail);
358
        }
359
360
        return [
361 1
            $this->header,
362 1
            $this->cart,
363 1
            $this->footer
364
        ];
365
    }
366
367
    /**
368
     * @param string $row
369
     * @return Purchaser
370
     */
371
    private function createPurchaser($row)
372
    {
373
        $purchaser      = new Purchaser();
374
        $purchaser->setPerson(substr($row, 157, 1));
375
        $purchaser->setName(substr($row, 158, 70));
376
        $purchaser->setFantasyName(substr($row, 228, 70));
377
        $purchaser->setSocialReason(substr($row, 298, 70));
378
        $purchaser->setBirth(DateTime::createFromFormat('dmY', substr($row, 403, 8)));
0 ignored issues
show
Security Bug introduced by
It seems like \DateTime::createFromFor..., substr($row, 403, 8)) targeting DateTime::createFromFormat() can also be of type false; however, MrPrompt\ShipmentCommon\Base\Person::setBirth() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?
Loading history...
379
        $purchaser->setEmail(new Email(substr($row, 411, 50)));
380
        $purchaser->setHomePhone(new Phone(substr($row, 577, 11), Phone::TELEPHONE));
381
        $purchaser->setCellPhone(new Phone(substr($row, 599, 11), Phone::CELLPHONE));
382
        $purchaser->setFatherName(substr($row, 617, 50));
383
        $purchaser->setMotherName(substr($row, 667, 50));
384
385
        return $purchaser;
386
    }
387
388
    /**
389
     * @param $row
390
     * @return Parcels
391
     */
392
    private function createParcels($row)
393
    {
394
395
        $parcelOne = new Parcel();
396
        $parcelOne->setMaturity(DateTime::createFromFormat('dmY', substr($row, 717, 8)));
0 ignored issues
show
Security Bug introduced by
It seems like \DateTime::createFromFor..., substr($row, 717, 8)) targeting DateTime::createFromFormat() can also be of type false; however, MrPrompt\ShipmentCommon\Base\Parcel::setMaturity() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?
Loading history...
397
        $parcelOne->setPrice(substr($row, 725, 10));
398
        $parcelOne->setQuantity(substr($row, 735, 2));
399
        $parcelOne->setKey(0);
400
401
        $parcelTwo = new Parcel();
402
        $parcelTwo->setPrice(substr($row, 737, 10));
403
        $parcelTwo->setQuantity(substr($row, 747, 2));
404
        $parcelTwo->setKey(1);
405
406
        $parcelThree = new Parcel();
407
        $parcelThree->setPrice(substr($row, 749, 10));
408
        $parcelThree->setQuantity(substr($row, 759, 2));
409
        $parcelThree->setKey(2);
410
411
        $parcelFour = new Parcel();
412
        $parcelFour->setPrice(substr($row, 761, 10));
413
        $parcelFour->setQuantity(substr($row, 771, 2));
414
        $parcelFour->setKey(3);
415
416
        $parcels = new Parcels(4);
417
        $parcels->addParcel($parcelOne);
418
        $parcels->addParcel($parcelTwo);
419
        $parcels->addParcel($parcelThree);
420
        $parcels->addParcel($parcelFour);
421
422
        return $parcels;
423
    }
424
425
    /**
426
     * @param $row
427
     * @return Authorization
428
     */
429
    private function createAuthorization($row)
430
    {
431
        $authorization  = new Authorization();
432
        $authorization->setNumber(substr($row, 773, 10));
433
434
        return $authorization;
435
    }
436
}
437