Completed
Pull Request — master (#1)
by Thiago
02:40
created

Detail::render()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 159

Duplication

Lines 21
Ratio 13.21 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 21
loc 159
ccs 0
cts 73
cp 0
rs 7.3777
c 0
b 0
f 0
cc 6
nc 8
nop 0
crap 42

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace MrPrompt\Centercob\Shipment\Partial;
3
4
use MrPrompt\ShipmentCommon\Base\Charge;
5
use MrPrompt\ShipmentCommon\Base\Holder;
6
use MrPrompt\ShipmentCommon\Base\Seller;
7
use MrPrompt\ShipmentCommon\Base\Parcels;
8
use MrPrompt\ShipmentCommon\Type\Numeric;
9
use MrPrompt\ShipmentCommon\Base\Customer;
10
use MrPrompt\ShipmentCommon\Base\Sequence;
11
use MrPrompt\ShipmentCommon\Base\Purchaser;
12
use MrPrompt\ShipmentCommon\Base\CreditCard;
13
use MrPrompt\ShipmentCommon\Base\Dealership;
14
use MrPrompt\ShipmentCommon\Base\BankAccount;
15
use MrPrompt\ShipmentCommon\Type\Alphanumeric;
16
use MrPrompt\ShipmentCommon\Base\Authorization;
17
use MrPrompt\ShipmentCommon\Base\ConsumerUnity;
18
19
/**
20
 * File detail
21
 *
22
 * @author Thiago Paes <[email protected]>
23
 */
24
class Detail extends \stdClass
25
{
26
    /**
27
     * Type of register
28
     *
29
     * @var string
30
     */
31
    const TYPE = 'D';
32
33
    /**
34
     * Shipment code
35
     *
36
     * @var int
37
     */
38
    const SHIPPING = 2;
39
40
    /**
41
     * Date Format
42
     *
43
     * @var string
44
     */
45
    const DATE_FORMAT = 'dmY';
46
47
    /**
48
     * Customer
49
     *
50
     * @var Customer
51
     */
52
    private $customer;
53
54
    /**
55
     * Charge
56
     *
57
     * @var charge
58
     */
59
    private $charge;
60
61
    /**
62
     * Consumer Unity
63
     *
64
     * @var ConsumerUnity
65
     */
66
    private $consumerUnity;
67
68
    /**
69
     * Dealership
70
     *
71
     * @var Dealership
72
     */
73
    private $dealership;
74
75
    /**
76
     * Seller
77
     *
78
     * @var Seller
79
     */
80
    private $seller;
81
82
    /**
83
     * Purchaser
84
     *
85
     * @var Purchaser
86
     */
87
    private $purchaser;
88
89
    /**
90
     * Bank account
91
     *
92
     * @var BankAccount
93
     */
94
    private $bankAccount;
95
96
    /**
97
     * Parcels
98
     *
99
     * @var Parcels
100
     */
101
    private $parcels;
102
103
    /**
104
     * Authorization
105
     *
106
     * @var Authorization
107
     */
108
    private $authorization;
109
110
    /**
111
     * Credit Card
112
     *
113
     * @var CreditCard
114
     */
115
    private $creditCard;
116
117
    /**
118
     * Sequence
119
     *
120
     * @var Sequence
121
     */
122
    private $sequence;
123
124
    /**
125
     * Holder
126
     *
127
     * @var Holder
128
     */
129
    private $holder;
130
131
    /**
132
     * Constructor
133
     *
134
     * @param Customer $customer
135
     * @param Charge $charge
136
     * @param Seller $seller
137
     * @param Purchaser $purchaser
138
     * @param Parcels $parcels
139
     * @param Authorization $authorization
140
     * @param CreditCard $creditCard
141
     * @param BankAccount $bankAccount
142
     * @param ConsumerUnity $consumerUnity
143
     * @param Sequence $sequence
144
     */
145 20
    public function __construct(
146
        Customer $customer,
147
        Charge $charge,
148
        Seller $seller,
149
        Purchaser $purchaser,
150
        Parcels $parcels,
151
        Authorization $authorization,
152
        CreditCard $creditCard,
153
        BankAccount $bankAccount,
154
        ConsumerUnity $consumerUnity,
155
        Sequence $sequence
156
    ) {
157 20
        $this->customer         = $customer;
158 20
        $this->charge           = $charge;
0 ignored issues
show
Documentation Bug introduced by
It seems like $charge of type object<MrPrompt\ShipmentCommon\Base\Charge> is incompatible with the declared type object<MrPrompt\Centerco...hipment\Partial\charge> of property $charge.

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...
159 20
        $this->consumerUnity    = $consumerUnity;
160 20
        $this->seller           = $seller;
161 20
        $this->purchaser        = $purchaser;
162 20
        $this->bankAccount      = $bankAccount;
163 20
        $this->parcels          = $parcels;
164 20
        $this->authorization    = $authorization;
165 20
        $this->creditCard       = $creditCard;
166 20
        $this->sequence         = $sequence;
167 20
        $this->dealership       = new Dealership();
168 20
        $this->holder           = new Holder();
169
170 20
        switch ($this->charge->getCharging()) {
171 20
            case Charge::CREDIT_CARD:
172
                $this->dealership->setCode($this->creditCard->getFlag());
173
                break;
174
175 20
            case Charge::DEBIT:
176 20
                $this->dealership->setCode($this->bankAccount->getBank()->getCode());
177 20
                break;
178
179
            case Charge::ENERGY:
180
                $this->dealership->setCode($this->consumerUnity->getCode());
181
                break;
182
183
            case Charge::BILLET:
184
                $this->dealership->setCode($this->bankAccount->getBank()->getCode());
185
                break;
186
        }
187 20
    }
188
189
    /**
190
     * @return Customer
191
     */
192 1
    public function getCustomer()
193
    {
194 1
        return $this->customer;
195
    }
196
197
    /**
198
     * @param Customer $customer
199
     */
200 1
    public function setCustomer(Customer $customer)
201
    {
202 1
        $this->customer = $customer;
203 1
    }
204
205
    /**
206
     * @return Charge
207
     */
208 1
    public function getCharge()
209
    {
210 1
        return $this->charge;
211
    }
212
213
    /**
214
     * @param Charge $charge
215
     */
216 1
    public function setCharge(Charge $charge)
217
    {
218 1
        $this->charge = $charge;
0 ignored issues
show
Documentation Bug introduced by
It seems like $charge of type object<MrPrompt\ShipmentCommon\Base\Charge> is incompatible with the declared type object<MrPrompt\Centerco...hipment\Partial\charge> of property $charge.

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...
219 1
    }
220
221
    /**
222
     * @return ConsumerUnity
223
     */
224 1
    public function getConsumerUnity()
225
    {
226 1
        return $this->consumerUnity;
227
    }
228
229
    /**
230
     * @param ConsumerUnity $consumerUnity
231
     */
232 1
    public function setConsumerUnity(ConsumerUnity $consumerUnity)
233
    {
234 1
        $this->consumerUnity = $consumerUnity;
235 1
    }
236
237
    /**
238
     * @return Seller
239
     */
240 1
    public function getSeller()
241
    {
242 1
        return $this->seller;
243
    }
244
245
    /**
246
     * @param Seller $seller
247
     */
248 1
    public function setSeller(Seller $seller)
249
    {
250 1
        $this->seller = $seller;
251 1
    }
252
253
    /**
254
     * @return Purchaser
255
     */
256 1
    public function getPurchaser()
257
    {
258 1
        return $this->purchaser;
259
    }
260
261
    /**
262
     * @param Purchaser $purchaser
263
     */
264 1
    public function setPurchaser(Purchaser $purchaser)
265
    {
266 1
        $this->purchaser = $purchaser;
267 1
    }
268
269
    /**
270
     * @return BankAccount
271
     */
272 1
    public function getBankAccount()
273
    {
274 1
        return $this->bankAccount;
275
    }
276
277
    /**
278
     * @param BankAccount $bankAccount
279
     */
280 1
    public function setBankAccount(BankAccount $bankAccount)
281
    {
282 1
        $this->bankAccount = $bankAccount;
283 1
    }
284
285
    /**
286
     * @return Parcels
287
     */
288 1
    public function getParcels()
289
    {
290 1
        return $this->parcels;
291
    }
292
293
    /**
294
     * @param Parcels $parcels
295
     */
296 1
    public function setParcels(Parcels $parcels)
297
    {
298 1
        $this->parcels = $parcels;
299 1
    }
300
301
    /**
302
     * @return Authorization
303
     */
304 1
    public function getAuthorization()
305
    {
306 1
        return $this->authorization;
307
    }
308
309
    /**
310
     * @param Authorization $authorization
311
     */
312 1
    public function setAuthorization(Authorization $authorization)
313
    {
314 1
        $this->authorization = $authorization;
315 1
    }
316
317
    /**
318
     * @return CreditCard
319
     */
320 1
    public function getCreditCard()
321
    {
322 1
        return $this->creditCard;
323
    }
324
325
    /**
326
     * @param CreditCard $creditCard
327
     */
328 1
    public function setCreditCard(CreditCard $creditCard)
329
    {
330 1
        $this->creditCard = $creditCard;
331 1
    }
332
333
    /**
334
     * @return Sequence
335
     */
336 1
    public function getSequence()
337
    {
338 1
        return $this->sequence;
339
    }
340
341
    /**
342
     * @param Sequence $sequence
343
     */
344 1
    public function setSequence(Sequence $sequence)
345
    {
346 1
        $this->sequence = $sequence;
347 1
    }
348
349
    /**
350
     * Render detail registry line
351
     *
352
     * @return string
353
     */
354
    public function render()
355
    {
356
        // Register code
357
        $result  = str_pad(self::TYPE, 1, Alphanumeric::FILL, Alphanumeric::ALIGN);
358
359
        // Shipping code
360
        $result .= str_pad(self::SHIPPING, 1, Numeric::FILL, Numeric::ALIGN);
361
362
        // Customer code
363
        $result .= substr(str_pad($this->customer->getCode(), 6, Numeric::FILL, Numeric::ALIGN), 0, 6);
364
365
        // Charging type
366
        $result .= substr(str_pad($this->charge->getCharging(), 1, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 1);
367
368
        // Consumer Unity from client
369
        $result .= substr(str_pad($this->consumerUnity->getNumber(), 25, Numeric::FILL, Numeric::ALIGN), 0, 25);
370
371
        // white space
372
        $result .= str_pad('', 10, Numeric::FILL, Numeric::ALIGN);
373
374
        // Occurrence type
375
        $result .= substr(str_pad($this->charge->getOccurrence()->getType(), 1, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 1);
376
377
        // white space
378
        $result .= str_pad('', 24, Alphanumeric::FILL, Alphanumeric::ALIGN);
379
380
        // City, state and Postal Code
381
        $result .= substr(str_pad($this->purchaser->getAddress()->getCity(), 50, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 50);
382
        $result .= substr(str_pad($this->purchaser->getAddress()->getState(), 2, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 2);
383
        $result .= substr(str_pad($this->purchaser->getAddress()->getPostalCode(), 8, Numeric::FILL, Numeric::ALIGN), 0, 8);
384
385
        // Dealership code
386
        $result .= substr(str_pad($this->dealership->getCode(), 6, Numeric::FILL, Numeric::ALIGN), 0, 6);
387
388
        // Seller code
389
        $result .= substr(str_pad($this->seller->getCode(), 6, Numeric::FILL, Numeric::ALIGN), 0, 6);
390
391
        // energy charge type
392
        $result .= substr($this->consumerUnity->getRead()->format(self::DATE_FORMAT), 0, 8);
393
        $result .= substr($this->consumerUnity->getMaturity()->format(self::DATE_FORMAT), 0, 8);
394
395
        // type person
396
        $result .= substr(str_pad($this->seller->getPerson(), 1, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 1);
397
398
        // person name
399
        $result .= substr(str_pad($this->purchaser->getName(), 70, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 70);
400
401
        // fantasy name
402
        $result .= substr(str_pad($this->purchaser->getFantasyName(), 70, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 70);
403
404
        // social reason
405
        $result .= substr(str_pad($this->purchaser->getSocialReason(), 70, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 70);
406
407
        // document number
408
        $result .= substr(str_pad($this->purchaser->getDocument()->getNumber(), 15, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 15);
409
        $result .= substr(str_pad($this->purchaser->getStateRegistration(), 20, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 20);
410
411
        // purchaser birth day
412
        $result .= $this->purchaser->getBirth()->format(self::DATE_FORMAT);
413
414
        $result .= substr(str_pad($this->purchaser->getEmail()->getAddress(), 50, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 50);
415
        $result .= substr(str_pad($this->purchaser->getAddress()->getAddress(), 50, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 50);
416
        $result .= substr(str_pad($this->purchaser->getAddress()->getNumber(), 6, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 6);
417
        $result .= substr(str_pad($this->purchaser->getAddress()->getDistrict(), 30, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 30);
418
        $result .= substr(str_pad($this->purchaser->getAddress()->getComplement(), 30, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 30);
419
        $result .= substr(str_pad($this->purchaser->getHomePhone()->getNumber(), 11, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 11);
420
        $result .= substr(str_pad($this->purchaser->getHomePhoneSecondary()->getNumber(), 11, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 11);
421
        $result .= substr(str_pad($this->purchaser->getCellPhone()->getNumber(), 11, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 11);
422
423
        // whitespace
424
        $result .= str_pad('', 7, Alphanumeric::FILL, Alphanumeric::ALIGN);
425
426
        // fathers
427
        $result .= substr(str_pad($this->purchaser->getFatherName(), 50, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 50);
428
        $result .= substr(str_pad($this->purchaser->getMotherName(), 50, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 50);
429
430
        // maturity date from first parcel
431
        $result .= substr($this->parcels->offsetGet(0)->getMaturity()->format(self::DATE_FORMAT), 0, 8);
432
433
        // first parcel
434
        $result .= substr(str_pad($this->parcels->offsetGet(0)->getPrice(), 10, Numeric::FILL, Numeric::ALIGN), 0, 10);
435
        $result .= substr(str_pad($this->parcels->offsetGet(0)->getQuantity(), 2, Numeric::FILL, Numeric::ALIGN), 0, 2);
436
437
        // second parcel
438 View Code Duplication
        if ($this->parcels->offsetExists(1)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
439
            $result .= substr(str_pad($this->parcels->offsetGet(1)->getPrice(), 10, Numeric::FILL, Numeric::ALIGN), 0, 10);
440
            $result .= substr(str_pad($this->parcels->offsetGet(1)->getQuantity(), 2, Numeric::FILL, Numeric::ALIGN), 0, 2);
441
        } else {
442
            $result .= substr(str_pad(0, 10, Numeric::FILL, Numeric::ALIGN), 0, 10);
443
            $result .= substr(str_pad(0, 2, Numeric::FILL, Numeric::ALIGN), 0, 2);
444
        }
445
446
        // third parcel
447 View Code Duplication
        if ($this->parcels->offsetExists(2)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
448
            $result .= substr(str_pad($this->parcels->offsetGet(2)->getPrice(), 10, Numeric::FILL, Numeric::ALIGN), 0, 10);
449
            $result .= substr(str_pad($this->parcels->offsetGet(2)->getQuantity(), 2, Numeric::FILL, Numeric::ALIGN), 0, 2);
450
        } else {
451
            $result .= substr(str_pad(0, 10, Numeric::FILL, Numeric::ALIGN), 0, 10);
452
            $result .= substr(str_pad(0, 2, Numeric::FILL, Numeric::ALIGN), 0, 2);
453
        }
454
455
        // fourth parcel
456 View Code Duplication
        if ($this->parcels->offsetExists(3)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
457
            $result .= substr(str_pad($this->parcels->offsetGet(3)->getPrice(), 10, Numeric::FILL, Numeric::ALIGN), 0, 10);
458
            $result .= substr(str_pad($this->parcels->offsetGet(3)->getQuantity(), 2, Numeric::FILL, Numeric::ALIGN), 0, 2);
459
        } else {
460
            $result .= substr(str_pad(0, 10, Numeric::FILL, Numeric::ALIGN), 0, 10);
461
            $result .= substr(str_pad(0, 2, Numeric::FILL, Numeric::ALIGN), 0, 2);
462
        }
463
464
        // authorization number
465
        $result .= substr(str_pad($this->authorization->getNumber(), 10, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 10);
466
467
        // occurrence code return
468
        $result .= substr(str_pad($this->charge->getOccurrence()->getReturn(), 2, Numeric::FILL, Numeric::ALIGN), 0, 2);
469
470
        // occurrence description
471
        $result .= substr(str_pad($this->charge->getOccurrence()->getDescription(), 100, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 100);
472
473
        // customer identity number
474
        $result .= substr(str_pad($this->customer->getIdentityNumber(), 25, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 25);
475
476
        // agency (only to automatic debit)
477
        $result .= substr(str_pad($this->bankAccount->getBank()->getAgency(), 8, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 8);
478
        $result .= substr(str_pad($this->bankAccount->getBank()->getDigit(), 3, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 3);
479
480
        // account (only to automatic debit)
481
        $result .= substr(str_pad($this->bankAccount->getNumber(), 12, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 12);
482
        $result .= substr(str_pad($this->bankAccount->getDigit(), 3, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 3);
483
        $result .= substr(str_pad($this->bankAccount->getOperation(), 4, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 4);
484
        $result .= substr(str_pad(($this->bankAccount->getSecurity() ? 'S' : 'N'), 1, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 1);
485
486
        // credit card (only for credit card payments, of course)
487
        $result .= substr(str_pad($this->creditCard->getNumber(), 16, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 16);
488
        $result .= str_pad('', 3, Alphanumeric::FILL, Alphanumeric::ALIGN);
489
        $result .= substr($this->creditCard->getValidate()->format('mY'), 0, 6);
490
        $result .= substr(str_pad($this->creditCard->getSecurityNumber(), 5, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 5);
491
492
        // Helpful maturity
493
        $result .= substr(str_pad(($this->customer->getHelpfulMaturity() ? 'S' : 'N'), 1, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 1);
494
495
        // Working days
496
        $result .= substr(str_pad($this->customer->getWorkingDays(), 2, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 2);
497
498
        // type person of purchaser
499
        $result .= substr(str_pad($this->purchaser->getPerson(), 1, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 1);
500
501
        // document from purchaser
502
        $result .= substr(str_pad($this->purchaser->getDocument()->getNumber(), 20, Alphanumeric::FILL, Alphanumeric::ALIGN), 0, 20);
503
504
        // white spaces
505
        $result .= str_pad('', 5, Alphanumeric::FILL, Alphanumeric::ALIGN);
506
507
        // sequence number from line
508
        $result .= substr(str_pad($this->sequence->getValue(), 6, Numeric::FILL, Numeric::ALIGN), 0, 6);
509
510
        // resulting....
511
        return $result;
512
    }
513
}
514