Completed
Pull Request — master (#18)
by ARCANEDEV
07:31
created

ExternalAccount::instanceUrl()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 36
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 5.0909

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 36
ccs 22
cts 26
cp 0.8462
rs 8.439
cc 5
eloc 24
nc 5
nop 0
crap 5.0909
1
<?php namespace Arcanedev\Stripe\Bases;
2
3
use Arcanedev\Stripe\Exceptions\ApiException;
4
use Arcanedev\Stripe\Exceptions\InvalidRequestException;
5
use Arcanedev\Stripe\StripeResource;
6
7
/**
8
 * Class     ExternalAccount
9
 *
10
 * @package  Arcanedev\Stripe\Bases
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
abstract class ExternalAccount extends StripeResource
14
{
15
    /* ------------------------------------------------------------------------------------------------
16
     |  Main Functions
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    /**
20
     * Get The instance URL for this resource.
21
     * It needs to be special cased because it doesn't fit into the standard resource pattern.
22
     *
23
     * @throws InvalidRequestException
24
     *
25
     * @return string
26
     */
27 55
    public function instanceUrl()
28
    {
29 55
        $id = $this['id'];
30
31 55
        if ( ! $id) {
32 5
            $class = get_class($this);
33 5
            $msg   = "Could not determine which URL to request: $class instance has invalid ID: $id";
34
35 5
            throw new InvalidRequestException($msg, null);
36
        }
37
38 50
        if ($this['account']) {
39
            $parent = $this['account'];
40
            $class  = 'Arcanedev\\Stripe\\Resources\\Account';
41
            $path   = 'external_accounts';
42
        }
43 50
        elseif ($this['customer']) {
44 25
            $parent = $this['customer'];
45 25
            $class  = 'Arcanedev\\Stripe\\Resources\\Customer';
46 25
            $path   = 'sources';
47 20
        }
48 30
        elseif ($this['recipient']) {
49 15
            $parent = $this['recipient'];
50 15
            $class  = 'Arcanedev\\Stripe\\Resources\\Recipient';
51 15
            $path   = 'cards';
52 12
        }
53
        else {
54 20
            return null;
55
        }
56
57 35
        $base       = self::classUrl($class);
58 35
        $parentExtn = urlencode(str_utf8($parent));
59 35
        $extn       = str_utf8($id);
60
61 35
        return "$base/$parentExtn/$path/$extn";
62
    }
63
64
    /* ------------------------------------------------------------------------------------------------
65
     |  CRUD Functions
66
     | ------------------------------------------------------------------------------------------------
67
     */
68
    /**
69
     * Delete an external account.
70
     *
71
     * @param  array|null         $params
72
     * @param  array|string|null  $options
73
     *
74
     * @return self
75
     */
76 10
    public function delete($params = null, $options = null)
77
    {
78 10
        return $this->scopedDelete($params, $options);
79
    }
80
81
    /**
82
     * Save an external account.
83
     *
84
     * @param  array|string|null  $options
85
     *
86
     * @return self
87
     */
88 10
    public function save($options = null)
89
    {
90 10
        return $this->scopedSave($options);
91
    }
92
93
    /**
94
     * Verify the external account.
95
     *
96
     * @param  array|null         $params
97
     * @param  array|string|null  $options
98
     *
99
     * @return self
100
     * @throws \Arcanedev\Stripe\Exceptions\ApiException
101
     */
102
    public function verify($params = null, $options = null)
103
    {
104
        if ( ! $this['customer']) {
105
            throw new ApiException(
106
                'Only customer external accounts can be verified in this manner.'
107
            );
108
        }
109
110
        $url = $this->instanceUrl() . '/verify';
111
        list($response, $options) = $this->request('post', $url, $params, $options);
112
        $this->refreshFrom($response, $options);
113
114
        return $this;
115
    }
116
}
117