Completed
Push — master ( 707e27...2c3f92 )
by
unknown
02:11
created

Orders   B

Complexity

Total Complexity 46

Size/Duplication

Total Lines 545
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 15
Bugs 1 Features 1
Metric Value
wmc 46
c 15
b 1
f 1
lcom 1
cbo 4
dl 0
loc 545
rs 8.3999

38 Methods

Rating   Name   Duplication   Size   Complexity  
A structure() 0 13 2
A create() 0 4 1
A get() 0 4 1
A getId() 0 4 1
A getOwnId() 0 4 1
A getAmountTotal() 0 4 1
A getAmountFees() 0 4 1
A getAmountRefunds() 0 4 1
A getAmountLiquid() 0 4 1
A getAmountOtherReceivers() 0 4 1
A getCurrenty() 0 4 1
A getSubtotalShipping() 0 6 1
A getSubtotalAddition() 0 6 1
A getSubtotalDiscount() 0 6 1
A getSubtotalItems() 0 6 1
A getItemIterator() 0 4 1
A getCustomer() 0 4 1
A getPaymentIterator() 0 4 1
A getReceiverIterator() 0 4 1
A getEventIterator() 0 4 1
A getRefundIterator() 0 4 1
A getStatus() 0 4 1
A getCreatedAt() 0 4 1
A getUpdatedAt() 0 4 1
A payments() 0 7 1
A refunds() 0 7 1
A setCustomer() 0 6 1
A addItem() 0 20 4
A addReceiver() 0 15 2
A initialize() 0 10 1
A initializeSubtotals() 0 6 2
B populate() 0 28 1
A setAddition() 0 9 2
A setCustomerId() 0 9 2
A setDiscount() 0 6 1
A setDiscont() 0 6 1
A setOwnId() 0 6 1
A setShippingAmount() 0 6 1

How to fix   Complexity   

Complex Class

Complex classes like Orders often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Orders, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Moip\Resource;
4
5
use ArrayIterator;
6
use stdClass;
7
8
/**
9
 * Class Orders.
10
 */
11
class Orders extends MoipResource
12
{
13
    /**
14
     * @const string
15
     */
16
    const PATH = 'orders';
17
18
    /**
19
     * Defines what kind of payee as pripmary.
20
     *
21
     * @const string
22
     */
23
    const RECEIVER_TYPE_PRIMARY = 'PRIMARY';
24
25
    /**
26
     * Defines what kind of payee as secundary.
27
     *
28
     * @const string
29
     */
30
    const RECEIVER_TYPE_SECONDARY = 'SECONDARY';
31
32
    /**
33
     * Currency used in the application.
34
     *
35
     * @const string
36
     */
37
    const AMOUNT_CURRENCY = 'BRL';
38
39
    /**
40
     * @var \Moip\Resource\Orders
41
     **/
42
    private $orders;
43
44
    /**
45
     * Adds a new item to order.
46
     *
47
     * @param string $product  Name of the product.
48
     * @param int    $quantity Product Quantity.
49
     * @param string $detail   Additional product description.
50
     * @param int    $price    Initial value of the item.
51
     *
52
     * @return $this
53
     */
54
    public function addItem($product, $quantity, $detail, $price)
55
    {
56
        if (!is_int($price)) {
57
            throw new \UnexpectedValueException('Informe o valor do item como inteiro');
58
        }
59
60
        if (!is_int($quantity) || $quantity < 1) {
61
            throw new \UnexpectedValueException('A quantidade do item deve ser um valor inteiro maior que 0');
62
        }
63
64
        $item = new stdClass();
65
        $item->product = $product;
66
        $item->quantity = $quantity;
67
        $item->detail = $detail;
68
        $item->price = $price;
69
70
        $this->data->items[] = $item;
71
72
        return $this;
73
    }
74
75
    /**
76
     *  Adds a new receiver to order.
77
     *
78
     * @param string $moipAccount Id MoIP MoIP account that will receive payment values.
79
     * @param string $type        Define qual o tipo de recebedor do pagamento, valores possíveis: PRIMARY, SECONDARY.
80
     * @param int    $fixed       Value that the receiver will receive.
81
     *
82
     * @return $this
83
     */
84
    public function addReceiver($moipAccount, $type, $fixed)
85
    {
86
        $receiver = new stdClass();
87
        $receiver->moipAccount = new stdClass();
88
        $receiver->moipAccount->id = $moipAccount;
89
        if (!empty($fixed)) {
90
            $receiver->amount = new stdClass();
91
            $receiver->amount->fixed = $fixed;
92
        }
93
        $receiver->type = $type;
94
95
        $this->data->receivers[] = $receiver;
96
97
        return $this;
98
    }
99
100
    /**
101
     * Initialize necessary used in some functions.
102
     */
103
    protected function initialize()
104
    {
105
        $this->data = new stdClass();
106
        $this->data->ownId = null;
107
        $this->data->amount = new stdClass();
108
        $this->data->amount->currency = self::AMOUNT_CURRENCY;
109
        $this->data->amount->subtotals = new stdClass();
110
        $this->data->items = [];
111
        $this->data->receivers = [];
112
    }
113
114
    /**
115
     * Initialize necessary used in some functions.
116
     */
117
    private function initializeSubtotals()
118
    {
119
        if (!isset($this->data->subtotals)) {
120
            $this->data->subtotals = new stdClass();
121
        }
122
    }
123
124
    /**
125
     * Mount the structure of order.
126
     *
127
     * @param \stdClass $response
128
     *
129
     * @return Orders Response order.
130
     */
131
    protected function populate(stdClass $response)
132
    {
133
        $this->orders = clone $this;
134
        $this->orders->data->id = $response->id;
135
        $this->orders->data->ownId = $response->ownId;
136
        $this->orders->data->amount->total = $response->amount->total;
137
        $this->orders->data->amount->fees = $response->amount->fees;
138
        $this->orders->data->amount->refunds = $response->amount->refunds;
139
        $this->orders->data->amount->liquid = $response->amount->liquid;
140
        $this->orders->data->amount->otherReceivers = $response->amount->otherReceivers;
141
        $this->orders->data->amount->subtotals = $response->amount->subtotals;
142
143
        $customer = new Customer($this->moip);
144
        $this->orders->data->customer = $customer->populate($response->customer);
145
146
        $this->orders->data->payments = $this->structure($response, Payment::PATH, Payment::class);
147
        $this->orders->data->refunds = $this->structure($response, Refund::PATH, Refund::class);
148
        $this->orders->data->entries = $this->structure($response, Entry::PATH, Entry::class);
149
        $this->orders->data->events = $this->structure($response, Event::PATH, Event::class);
150
151
        $this->orders->data->items = $response->items;
152
        $this->orders->data->receivers = $response->receivers;
153
        $this->orders->data->createdAt = $response->createdAt;
154
        $this->orders->data->status = $response->status;
155
        $this->orders->data->_links = $response->_links;
156
157
        return $this->orders;
158
    }
159
160
    /**
161
     * Structure resource.
162
     *
163
     * @param stdClass                                                                               $response
164
     * @param string                                                                                 $resource
165
     * @param \Moip\Resource\Payment|\Moip\Resource\Refund|\Moip\Resource\Entry|\Moip\Resource\Event $class
166
     *
167
     * @return array
168
     */
169
    private function structure(stdClass $response, $resource, $class)
170
    {
171
        $structures = [];
172
173
        foreach ($response->$resource as $responseResource) {
174
            $structure = new $class($this->orders->moip);
175
            $structure->populate($responseResource);
176
177
            $structures[] = $structure;
178
        }
179
180
        return $structures;
181
    }
182
183
    /**
184
     * Create a new order in MoIP.
185
     *
186
     * @return \Moip\Resource\Orders|stdClass
187
     */
188
    public function create()
189
    {
190
        return $this->createResource(sprintf('/%s/%s', MoipResource::VERSION, self::PATH));
191
    }
192
193
    /**
194
     * Get an order in MoIP.
195
     *
196
     * @param string $id_moip Id MoIP order id
197
     *
198
     * @return stdClass
199
     */
200
    public function get($id_moip)
201
    {
202
        return $this->getByPath(sprintf('/%s/%s/%s', MoipResource::VERSION, self::PATH, $id_moip));
203
    }
204
205
    /**
206
     * Get MoIP order id.
207
     *
208
     * @return string
209
     */
210
    public function getId()
211
    {
212
        return $this->getIfSet('id');
213
    }
214
215
    /**
216
     * Get own request id. external reference.
217
     *
218
     * @return string
219
     */
220
    public function getOwnId()
221
    {
222
        return $this->getIfSet('ownId');
223
    }
224
225
    /**
226
     * Get total value of order.
227
     *
228
     * @return int|float
229
     */
230
    public function getAmountTotal()
231
    {
232
        return $this->getIfSet('total', $this->data->amount);
233
    }
234
235
    /**
236
     * Get total value of MoIP rate.
237
     *
238
     * @return int|float
239
     */
240
    public function getAmountFees()
241
    {
242
        return $this->getIfSet('fees', $this->data->amount);
243
    }
244
245
    /**
246
     * Get total amount of refunds.
247
     *
248
     * @return int|float
249
     */
250
    public function getAmountRefunds()
251
    {
252
        return $this->getIfSet('refunds', $this->data->amount);
253
    }
254
255
    /**
256
     * Get net total value.
257
     *
258
     * @return int|float
259
     */
260
    public function getAmountLiquid()
261
    {
262
        return $this->getIfSet('liquid', $this->data->amount);
263
    }
264
265
    /**
266
     * Get sum of amounts received by other recipients. Used in Marketplaces.
267
     *
268
     * @return int|float
269
     */
270
    public function getAmountOtherReceivers()
271
    {
272
        return $this->getIfSet('otherReceivers', $this->data->amount);
273
    }
274
275
    /**
276
     * Get currency used in the application. Possible values: BRL.
277
     *
278
     * @return string
279
     */
280
    public function getCurrenty()
281
    {
282
        return $this->getIfSet('currency', $this->data->amount);
283
    }
284
285
    /**
286
     * Get greight value of the item will be added to the value of the items.
287
     *
288
     * @return int|float
289
     */
290
    public function getSubtotalShipping()
291
    {
292
        $this->initializeSubtotals();
293
294
        return $this->getIfSet('shipping', $this->data->amount->subtotals);
295
    }
296
297
    /**
298
     * Get Additional value to the item will be added to the value of the items.
299
     *
300
     * @return int|float
301
     */
302
    public function getSubtotalAddition()
303
    {
304
        $this->initializeSubtotals();
305
306
        return $this->getIfSet('addition', $this->data->amount->subtotals);
307
    }
308
309
    /**
310
     * Get discounted value of the item will be subtracted from the total value of the items.
311
     *
312
     * @return int|float
313
     */
314
    public function getSubtotalDiscount()
315
    {
316
        $this->initializeSubtotals();
317
318
        return $this->getIfSet('discount', $this->data->amount->subtotals);
319
    }
320
321
    /**
322
     * Get summing the values of all items.
323
     *
324
     * @return int|float
325
     */
326
    public function getSubtotalItems()
327
    {
328
        $this->initializeSubtotals();
329
330
        return $this->getIfSet('items', $this->data->amount->subtotals);
331
    }
332
333
    /**
334
     * Ger structure item information request.
335
     *
336
     * @return \ArrayIterator
337
     */
338
    public function getItemIterator()
339
    {
340
        return new ArrayIterator($this->data->items);
341
    }
342
343
    /**
344
     * Get Customer associated with the request.
345
     *
346
     * @return \Moip\Resource\Customer
347
     */
348
    public function getCustomer()
349
    {
350
        return $this->data->customer;
351
    }
352
353
    /**
354
     * Get payments associated with the request.
355
     *
356
     * @return ArrayIterator
357
     */
358
    public function getPaymentIterator()
359
    {
360
        return new ArrayIterator($this->data->payments);
361
    }
362
363
    /**
364
     * Get recipient structure of payments.
365
     *
366
     * @return ArrayIterator
367
     */
368
    public function getReceiverIterator()
369
    {
370
        return new ArrayIterator($this->data->receivers);
371
    }
372
373
    /**
374
     * Get releases associated with the request.
375
     *
376
     * @return ArrayIterator
377
     */
378
    public function getEventIterator()
379
    {
380
        return new ArrayIterator($this->data->events);
381
    }
382
383
    /**
384
     * Get repayments associated with the request.
385
     *
386
     * @return ArrayIterator
387
     */
388
    public function getRefundIterator()
389
    {
390
        return new ArrayIterator($this->data->refunds);
391
    }
392
393
    /**
394
     * Get order status.
395
     * Possible values: CREATED, WAITING, PAID, NOT_PAID, REVERTED.
396
     *
397
     * @return string
398
     */
399
    public function getStatus()
400
    {
401
        return $this->getIfSet('status');
402
    }
403
404
    /**
405
     * Get date of resource creation.
406
     *
407
     * @return \DateTime
408
     */
409
    public function getCreatedAt()
410
    {
411
        return $this->getIfSetDateTime('createdAt');
412
    }
413
414
    /**
415
     * Get updated resource.
416
     *
417
     * @return \DateTime
418
     */
419
    public function getUpdatedAt()
420
    {
421
        return $this->getIfSetDateTime('updatedAt');
422
    }
423
424
    /**
425
     * Structure of payment.
426
     *
427
     * @return \Moip\Resource\Payment
428
     */
429
    public function payments()
430
    {
431
        $payment = new Payment($this->moip);
432
        $payment->setOrder($this);
433
434
        return $payment;
435
    }
436
437
    /**
438
     * Structure of refund.
439
     *
440
     * @return \Moip\Resource\Refund
441
     */
442
    public function refunds()
443
    {
444
        $refund = new Refund($this->moip);
445
        $refund->setOrder($this);
446
447
        return $refund;
448
    }
449
450
    /**
451
     * Set additional value to the item will be added to the value of the items.
452
     *
453
     * @param int|float $value additional value to the item.
454
     *
455
     * @return $this
456
     */
457
    public function setAddition($value)
458
    {
459
        if (!isset($this->data->amount->subtotals)) {
460
            $this->data->amount->subtotals = new stdClass();
461
        }
462
        $this->data->amount->subtotals->addition = (float) $value;
463
464
        return $this;
465
    }
466
467
    /**
468
     * Set customer associated with the order.
469
     *
470
     * @param \Moip\Resource\Customer $customer customer associated with the request.
471
     *
472
     * @return $this
473
     */
474
    public function setCustomer(Customer $customer)
475
    {
476
        $this->data->customer = $customer;
477
478
        return $this;
479
    }
480
481
    /**
482
     * Set customer id associated with the order.
483
     *
484
     * @param string $id Customer's id.
485
     *
486
     * @return $this
487
     */
488
    public function setCustomerId($id)
489
    {
490
        if (!isset($this->data->customer)) {
491
            $this->data->customer = new stdClass();
492
        }
493
        $this->data->customer->id = $id;
494
495
        return $this;
496
    }
497
498
    /**
499
     * Set discounted value of the item will be subtracted from the total value of the items.
500
     *
501
     * @param int|float $value discounted value.
502
     *
503
     * @return $this
504
     */
505
    public function setDiscount($value)
506
    {
507
        $this->data->amount->subtotals->discount = (float) $value;
508
509
        return $this;
510
    }
511
512
    /**
513
     * Set discounted value of the item will be subtracted from the total value of the items.
514
     *
515
     * @deprecated
516
     *
517
     * @param int|float $value discounted value.
518
     *
519
     * @return $this
520
     */
521
    public function setDiscont($value)
522
    {
523
        $this->setDiscount($value);
524
525
        return $this;
526
    }
527
528
    /**
529
     * Set own request id. external reference.
530
     *
531
     * @param string $ownId external reference.
532
     *
533
     * @return $this
534
     */
535
    public function setOwnId($ownId)
536
    {
537
        $this->data->ownId = $ownId;
538
539
        return $this;
540
    }
541
542
    /**
543
     * Set shipping Amount.
544
     *
545
     * @param float $value shipping Amount.
546
     *
547
     * @return $this
548
     */
549
    public function setShippingAmount($value)
550
    {
551
        $this->data->amount->subtotals->shipping = (float) $value;
552
553
        return $this;
554
    }
555
}
556