Completed
Push — master ( 887aec...707e27 )
by
unknown
13s
created

Orders::getEventIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
        $customer->populate($response->customer);
145
        $this->orders->data->customer = $customer;
146
147
        $this->orders->data->payments = $this->structure($response, Payment::PATH, Payment::class);
148
        $this->orders->data->refunds = $this->structure($response, Refund::PATH, Refund::class);
149
        $this->orders->data->entries = $this->structure($response, Entry::PATH, Entry::class);
150
        $this->orders->data->events = $this->structure($response, Event::PATH, Event::class);
151
152
        $this->orders->data->items = $response->items;
153
        $this->orders->data->receivers = $response->receivers;
154
        $this->orders->data->createdAt = $response->createdAt;
155
        $this->orders->data->status = $response->status;
156
        $this->orders->data->_links = $response->_links;
157
158
        return $this->orders;
159
    }
160
161
    /**
162
     * Structure resource.
163
     *
164
     * @param stdClass                                                                               $response
165
     * @param string                                                                                 $resource
166
     * @param \Moip\Resource\Payment|\Moip\Resource\Refund|\Moip\Resource\Entry|\Moip\Resource\Event $class
167
     *
168
     * @return array
169
     */
170
    private function structure(stdClass $response, $resource, $class)
171
    {
172
        $structures = [];
173
174
        foreach ($response->$resource as $responseResource) {
175
            $structure = new $class($this->orders->moip);
176
            $structure->populate($responseResource);
177
178
            $structures[] = $structure;
179
        }
180
181
        return $structures;
182
    }
183
184
    /**
185
     * Create a new order in MoIP.
186
     *
187
     * @return \Moip\Resource\Orders|stdClass
188
     */
189
    public function create()
190
    {
191
        return $this->createResource(sprintf('/%s/%s', MoipResource::VERSION, self::PATH));
192
    }
193
194
    /**
195
     * Get an order in MoIP.
196
     *
197
     * @param string $id_moip Id MoIP order id
198
     *
199
     * @return stdClass
200
     */
201
    public function get($id_moip)
202
    {
203
        return $this->getByPath(sprintf('/%s/%s/%s', MoipResource::VERSION, self::PATH, $id_moip));
204
    }
205
206
    /**
207
     * Get MoIP order id.
208
     *
209
     * @return string
210
     */
211
    public function getId()
212
    {
213
        return $this->getIfSet('id');
214
    }
215
216
    /**
217
     * Get own request id. external reference.
218
     *
219
     * @return string
220
     */
221
    public function getOwnId()
222
    {
223
        return $this->getIfSet('ownId');
224
    }
225
226
    /**
227
     * Get total value of order.
228
     *
229
     * @return int|float
230
     */
231
    public function getAmountTotal()
232
    {
233
        return $this->getIfSet('total', $this->data->amount);
234
    }
235
236
    /**
237
     * Get total value of MoIP rate.
238
     *
239
     * @return int|float
240
     */
241
    public function getAmountFees()
242
    {
243
        return $this->getIfSet('fees', $this->data->amount);
244
    }
245
246
    /**
247
     * Get total amount of refunds.
248
     *
249
     * @return int|float
250
     */
251
    public function getAmountRefunds()
252
    {
253
        return $this->getIfSet('refunds', $this->data->amount);
254
    }
255
256
    /**
257
     * Get net total value.
258
     *
259
     * @return int|float
260
     */
261
    public function getAmountLiquid()
262
    {
263
        return $this->getIfSet('liquid', $this->data->amount);
264
    }
265
266
    /**
267
     * Get sum of amounts received by other recipients. Used in Marketplaces.
268
     *
269
     * @return int|float
270
     */
271
    public function getAmountOtherReceivers()
272
    {
273
        return $this->getIfSet('otherReceivers', $this->data->amount);
274
    }
275
276
    /**
277
     * Get currency used in the application. Possible values: BRL.
278
     *
279
     * @return string
280
     */
281
    public function getCurrenty()
282
    {
283
        return $this->getIfSet('currency', $this->data->amount);
284
    }
285
286
    /**
287
     * Get greight value of the item will be added to the value of the items.
288
     *
289
     * @return int|float
290
     */
291
    public function getSubtotalShipping()
292
    {
293
        $this->initializeSubtotals();
294
295
        return $this->getIfSet('shipping', $this->data->amount->subtotals);
296
    }
297
298
    /**
299
     * Get Additional value to the item will be added to the value of the items.
300
     *
301
     * @return int|float
302
     */
303
    public function getSubtotalAddition()
304
    {
305
        $this->initializeSubtotals();
306
307
        return $this->getIfSet('addition', $this->data->amount->subtotals);
308
    }
309
310
    /**
311
     * Get discounted value of the item will be subtracted from the total value of the items.
312
     *
313
     * @return int|float
314
     */
315
    public function getSubtotalDiscount()
316
    {
317
        $this->initializeSubtotals();
318
319
        return $this->getIfSet('discount', $this->data->amount->subtotals);
320
    }
321
322
    /**
323
     * Get summing the values of all items.
324
     *
325
     * @return int|float
326
     */
327
    public function getSubtotalItems()
328
    {
329
        $this->initializeSubtotals();
330
331
        return $this->getIfSet('items', $this->data->amount->subtotals);
332
    }
333
334
    /**
335
     * Ger structure item information request.
336
     *
337
     * @return \ArrayIterator
338
     */
339
    public function getItemIterator()
340
    {
341
        return new ArrayIterator($this->data->items);
342
    }
343
344
    /**
345
     * Get Customer associated with the request.
346
     *
347
     * @return \Moip\Resource\Customer
348
     */
349
    public function getCustomer()
350
    {
351
        return $this->data->customer;
352
    }
353
354
    /**
355
     * Get payments associated with the request.
356
     *
357
     * @return ArrayIterator
358
     */
359
    public function getPaymentIterator()
360
    {
361
        return new ArrayIterator($this->data->payments);
362
    }
363
364
    /**
365
     * Get recipient structure of payments.
366
     *
367
     * @return ArrayIterator
368
     */
369
    public function getReceiverIterator()
370
    {
371
        return new ArrayIterator($this->data->receivers);
372
    }
373
374
    /**
375
     * Get releases associated with the request.
376
     *
377
     * @return ArrayIterator
378
     */
379
    public function getEventIterator()
380
    {
381
        return new ArrayIterator($this->data->events);
382
    }
383
384
    /**
385
     * Get repayments associated with the request.
386
     *
387
     * @return ArrayIterator
388
     */
389
    public function getRefundIterator()
390
    {
391
        return new ArrayIterator($this->data->refunds);
392
    }
393
394
    /**
395
     * Get order status.
396
     * Possible values: CREATED, WAITING, PAID, NOT_PAID, REVERTED.
397
     *
398
     * @return string
399
     */
400
    public function getStatus()
401
    {
402
        return $this->getIfSet('status');
403
    }
404
405
    /**
406
     * Get date of resource creation.
407
     *
408
     * @return \DateTime
409
     */
410
    public function getCreatedAt()
411
    {
412
        return $this->getIfSetDateTime('createdAt');
413
    }
414
415
    /**
416
     * Get updated resource.
417
     *
418
     * @return \DateTime
419
     */
420
    public function getUpdatedAt()
421
    {
422
        return $this->getIfSetDateTime('updatedAt');
423
    }
424
425
    /**
426
     * Structure of payment.
427
     *
428
     * @return \Moip\Resource\Payment
429
     */
430
    public function payments()
431
    {
432
        $payment = new Payment($this->moip);
433
        $payment->setOrder($this);
434
435
        return $payment;
436
    }
437
438
    /**
439
     * Structure of refund.
440
     *
441
     * @return \Moip\Resource\Refund
442
     */
443
    public function refunds()
444
    {
445
        $refund = new Refund($this->moip);
446
        $refund->setOrder($this);
447
448
        return $refund;
449
    }
450
451
    /**
452
     * Set additional value to the item will be added to the value of the items.
453
     *
454
     * @param int|float $value additional value to the item.
455
     *
456
     * @return $this
457
     */
458
    public function setAddition($value)
459
    {
460
        if (!isset($this->data->amount->subtotals)) {
461
            $this->data->amount->subtotals = new stdClass();
462
        }
463
        $this->data->amount->subtotals->addition = (float) $value;
464
465
        return $this;
466
    }
467
468
    /**
469
     * Set customer associated with the order.
470
     *
471
     * @param \Moip\Resource\Customer $customer customer associated with the request.
472
     *
473
     * @return $this
474
     */
475
    public function setCustomer(Customer $customer)
476
    {
477
        $this->data->customer = $customer;
478
479
        return $this;
480
    }
481
482
    /**
483
     * Set customer id associated with the order.
484
     *
485
     * @param string $id Customer's id.
486
     *
487
     * @return $this
488
     */
489
    public function setCustomerId($id)
490
    {
491
        if (!isset($this->data->customer)) {
492
            $this->data->customer = new stdClass();
493
        }
494
        $this->data->customer->id = $id;
495
496
        return $this;
497
    }
498
499
    /**
500
     * Set discounted value of the item will be subtracted from the total value of the items.
501
     *
502
     * @param int|float $value discounted value.
503
     *
504
     * @return $this
505
     */
506
    public function setDiscount($value)
507
    {
508
        $this->data->amount->subtotals->discount = (float) $value;
509
510
        return $this;
511
    }
512
513
    /**
514
     * Set discounted value of the item will be subtracted from the total value of the items.
515
     *
516
     * @deprecated
517
     *
518
     * @param int|float $value discounted value.
519
     *
520
     * @return $this
521
     */
522
    public function setDiscont($value)
523
    {
524
        $this->setDiscount($value);
525
526
        return $this;
527
    }
528
529
    /**
530
     * Set own request id. external reference.
531
     *
532
     * @param string $ownId external reference.
533
     *
534
     * @return $this
535
     */
536
    public function setOwnId($ownId)
537
    {
538
        $this->data->ownId = $ownId;
539
540
        return $this;
541
    }
542
543
    /**
544
     * Set shipping Amount.
545
     *
546
     * @param float $value shipping Amount.
547
     *
548
     * @return $this
549
     */
550
    public function setShippingAmount($value)
551
    {
552
        $this->data->amount->subtotals->shipping = (float) $value;
553
554
        return $this;
555
    }
556
}
557