Completed
Push — master ( 6d9ae5...9656aa )
by
18:43 queued 08:49
created

Charge::findRefunds()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 2
eloc 8
nc 2
nop 1
crap 6
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
     * @var string
55
     */
56
    protected $cardToken;
57
58
    /**
59
     * @param string $countryCode
60
     *
61
     * @return array
62
     */
63 1
    public function getCreateData($countryCode = Country::TH)
64
    {
65 1
        if (!in_array($this->currency, Currency::getSupporteds($countryCode))) {
66
            throw new InvalidRequestArgumentException(
67
                sprintf('The currency `%s` is not supported in your country `%s`.', $this->currency, $countryCode)
68
            );
69
        }
70
71
        return [
72 1
            'customer' => (string) ($this->customer),
73 1
            'card' => $this->__get('cardToken'),
74 1
            'amount' => $this->amount,
75 1
            'currency' => $this->currency,
76 1
            'description' => $this->description,
77 1
            'metadata' => $this->metadata,
78 1
            'capture' => $this->capture,
79 1
            'return_uri' => $this->returnUri,
80
        ];
81
    }
82
83
    /**
84
     * @return array
85
     */
86
    public function getUpdateData()
87
    {
88
        return [
89
            'description' => $this->description,
90
            'metadata' => $this->metadata,
91
        ];
92
    }
93
94
    /**
95
     * @return array|Refund[]
96
     */
97
    public function getRefunds()
98
    {
99
        if (count($this->refunds)) {
100
            return $this->refunds->data;
101
        }
102
103
        return [];
104
    }
105
106
    /**
107
     * @param $id
108
     *
109
     * @return Refund|null
110
     */
111
    public function findRefunds($id)
112
    {
113
        $charge = array_filter(
114
            $this->getRefunds(),
115
            function (Refund $charge) use ($id) {
116
                return $charge->id === $id;
117
            }
118
        );
119
120
        if (empty($charge)) {
121
            return null;
122
        }
123
124
        return $charge[0];
125
    }
126
}
127