Test Failed
Push — master ( 5ff3d7...670d03 )
by
05:03
created

Charge::getCreateUsingTokenData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 4
cp 0
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the PhpMob package.
5
 *
6
 * (c) Ishmael Doss <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace PhpMob\Omise\Domain;
15
16
use PhpMob\Omise\Country;
17
use PhpMob\Omise\Currency;
18
use PhpMob\Omise\Exception\InvalidRequestArgumentException;
19
use PhpMob\Omise\Model;
20
21
/**
22
 * @author Ishmael Doss <[email protected]>
23
 *
24
 * @property string id
25
 * @property string object
26
 * @property bool livemode
27
 * @property string location
28
 * @property string status
29
 * @property int amount
30
 * @property string currency
31
 * @property string description
32
 * @property string metadata
33
 * @property bool capture
34
 * @property bool authorized
35
 * @property bool reversed
36
 * @property bool paid
37
 * @property string transaction
38
 * @property Card card
39
 * @property int refunded
40
 * @property Pagination refunds
41
 * @property string failureCode
42
 * @property string failureMessage
43
 * @property Customer customer
44
 * @property string ip
45
 * @property Dispute dispute
46
 * @property string created
47
 * @property string returnUri
48
 * @property string authorizeUri
49
 * @property string $cardToken
50
 */
51
class Charge extends Model
52
{
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function __set($name, $value)
57
    {
58
        if ('currency' === $name) {
59
            $value = strtolower($value);
60
        }
61
62
        parent::__set($name, $value);
63 4
    }
64
65 4
    /**
66
     * @param string $countryCode
67
     *
68
     * @return array
69
     */
70
    public function getCreateData($countryCode = Country::TH)
71
    {
72 4
        if (!in_array($this->currency, Currency::getSupporteds($countryCode))) {
73 4
            throw new InvalidRequestArgumentException(
74 4
                sprintf('The currency `%s` is not supported in your country `%s`.', $this->currency, $countryCode)
75 4
            );
76 4
        }
77 4
78 4
        return [
79 4
            'customer' => (string) ($this->customer),
80
            'card' => $this->card,
81
            'amount' => $this->amount * Currency::getDivisionOffset($this->currency),
82
            'currency' => $this->currency,
83
            'description' => $this->description,
84
            'metadata' => $this->metadata,
85
            'capture' => $this->capture,
86 1
            'return_uri' => $this->returnUri,
87
        ];
88
    }
89 1
90 1
    /**
91
     * @param string $countryCode
92
     *
93
     * @return array
94
     */
95
    public function getCreateUsingTokenData($countryCode = Country::TH)
96
    {
97
        $data = $this->getCreateData($countryCode);
98
        $data['card'] = $this->cardToken;
99
100
        unset($data['customer']);
101
102
        return $data;
103
    }
104
105
    /**
106
     * @return array
107
     */
108
    public function getUpdateData()
109
    {
110
        return [
111
            'description' => $this->description,
112
            'metadata' => $this->metadata,
113
        ];
114
    }
115
116
    /**
117
     * @return array|Refund[]
118
     */
119
    public function getRefunds()
120
    {
121
        if (count($this->refunds)) {
122
            return $this->refunds->data;
123
        }
124
125
        return [];
126
    }
127
128
    /**
129
     * @param $id
130
     *
131
     * @return Refund|null
132
     */
133
    public function findRefunds($id)
134
    {
135
        $charge = array_filter(
136
            $this->getRefunds(),
137
            function (Refund $charge) use ($id) {
138
                return $charge->id === $id;
139
            }
140
        );
141
142
        if (empty($charge)) {
143
            return null;
144
        }
145
146
        return $charge[0];
147
    }
148
}
149