Completed
Pull Request — master (#60)
by ARCANEDEV
08:31
created

ExternalAccount   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 71.43%

Importance

Changes 0
Metric Value
dl 0
loc 103
ccs 25
cts 35
cp 0.7143
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 3

4 Methods

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