Completed
Push — master ( 7b285b...480624 )
by Rigel Kent
04:14
created

Payment::setData()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 4
nc 3
nop 1
1
<?php
2
3
namespace Luigel\Paymongo\Models;
4
5
class Payment
6
{
7
    public $id;
8
    public $type;
9
    public $amount;
10
    public $currency;
11
    public $description;
12
    public $external_reference_number;
13
    public $fee;
14
    public $net_amount;
15
    public $statement_descriptor;
16
    public $status;
17
    public $source;
18
    public $created_at;
19
    public $updated_at;
20
    public $paid_at;
21
    public $payout;
22
    public $access_url;
23
    public $billing;
24
25
    public function setData($data)
26
    {
27
        if (is_array($data) && isset($data['id']))
28
        {
29
            return $this->convertToObject($data);
30
        }
31
        $payments = collect();
32
33
        foreach ($data as $item)
34
        {
35
            $payments->push($this->convertToObject($item));
36
        }
37
        return $payments;
38
39
    }
40
41
    protected function convertToObject($data)
42
    {
43
        $this->id = $data['id'];
44
        $this->type = $data['type'];
45
        $this->amount = number_format($data['attributes']['amount'] / 100, 2);
46
        $this->currency = $data['attributes']['currency'] ?? 'PHP';
47
        $this->description = $data['attributes']['description'];
48
        $this->external_reference_number = $data['attributes']['external_reference_number'];
49
        $this->fee = $data['attributes']['fee'];
50
        $this->net_amount = $data['attributes']['net_amount'];
51
        $this->statement_descriptor = $data['attributes']['statement_descriptor'];
52
        $this->status = $data['attributes']['status'];
53
        $this->source = new PaymentSource($data['attributes']['source']);
54
        $this->created_at = $data['attributes']['created_at'];
55
        $this->updated_at = $data['attributes']['updated_at'];
56
        $this->paid_at = $data['attributes']['paid_at'];
57
        $this->payout = $data['attributes']['payout'];
58
        $this->access_url = $data['attributes']['access_url'];
59
        $this->billing = $data['attributes']['billing'];
60
61
        return $this;
62
    }
63
}
64