Completed
Pull Request — master (#66)
by ARCANEDEV
12:06 queued 05:05
created

Source::detach()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3.0593

Importance

Changes 0
Metric Value
dl 0
loc 28
c 0
b 0
f 0
cc 3
eloc 16
nc 3
nop 2
ccs 13
cts 16
cp 0.8125
crap 3.0593
rs 8.8571
1
<?php namespace Arcanedev\Stripe\Resources;
2
3
use Arcanedev\Stripe\Contracts\Resources\Source as SourceContract;
4
use Arcanedev\Stripe\Exceptions\ApiException;
5
use Arcanedev\Stripe\Exceptions\InvalidRequestException;
6
use Arcanedev\Stripe\StripeResource;
7
use Arcanedev\Stripe\Utilities\Util;
8
9
/**
10
 * Class     Source
11
 *
12
 * @package  Arcanedev\Stripe\Resources
13
 * @author   ARCANEDEV <[email protected]>
14
 *
15
 * @property  string  type
16
 */
17
class Source extends StripeResource implements SourceContract
18
{
19
    /* -----------------------------------------------------------------
20
     |  Main Methods
21
     | -----------------------------------------------------------------
22
     */
23
24
    /**
25
     * Retrieve a Source.
26
     *
27
     * @param  string             $id
28
     * @param  array|string|null  $options
29
     *
30
     * @return self
31
     */
32 8
    public static function retrieve($id, $options = null)
33
    {
34 8
        return self::scopedRetrieve($id, $options);
35
    }
36
37
    /**
38
     * Create a Source.
39
     *
40
     * @param  array|null         $params
41
     * @param  array|string|null  $options
42
     *
43
     * @return self
44
     */
45 2
    public static function create($params = null, $options = null)
46
    {
47 2
        return self::scopedCreate($params, $options);
48
    }
49
50
    /**
51
     * Verify the bank account.
52
     *
53
     * @param  array|null         $params
54
     * @param  array|string|null  $options
55
     *
56
     * @return self
57
     */
58 2
    public function verify($params = null, $options = null)
59
    {
60 2
        list($response, $opts) = $this->request('post', $this->instanceUrl() . '/verify', $params, $options);
61 2
        $this->refreshFrom($response, $opts);
62
63 2
        return $this;
64
    }
65
66
    /**
67
     * Update a source.
68
     *
69
     * @param  string             $id
70
     * @param  array|null         $params
71
     * @param  array|string|null  $options
72
     *
73
     * @return self
74
     */
75
    public static function update($id, $params = null, $options = null)
76
    {
77
        return self::scopedUpdate($id, $params, $options);
78
    }
79
80
    /**
81
     * Save a source.
82
     *
83
     * @param array|string|null $options
84
     *
85
     * @return self
86
     */
87 4
    public function save($options = null)
88
    {
89 4
        return $this->scopedSave($options);
90
    }
91
92
    /**
93
     * Detach a source.
94
     *
95
     * @param  array|null         $params
96
     * @param  array|string|null  $options
97
     *
98
     * @return self
99
     *
100
     * @throws \Arcanedev\Stripe\Exceptions\ApiException
101
     * @throws \Arcanedev\Stripe\Exceptions\InvalidRequestException
102
     */
103 4
    public function detach($params = null, $options = null)
104
    {
105 4
        static::checkArguments($params, $options);
106
107 4
        $id = $this['id'];
108 4
        if ( ! $id) {
109
            $class = get_class($this);
110
111
            throw new InvalidRequestException(
112
                "Could not determine which URL to request: {$class} instance has invalid ID: {$id}", null
113
            );
114
        }
115
116 4
        if ($this['customer']) {
117 2
            $base       = Customer::classUrl();
118 2
            $parentExtn = urlencode(str_utf8($this['customer']));
119 2
            $extn       = urlencode(str_utf8($id));
120
121 2
            list($response, $opts) = $this->request('delete', "{$base}/{$parentExtn}/sources/{$extn}", $params, $options);
122 2
            $this->refreshFrom($response, $opts);
123
124 2
            return $this;
125
        }
126
127 2
        throw new ApiException(
128 2
            'This source object does not appear to be currently attached to a customer object.'
129
        );
130
    }
131
132
    /**
133
     * Delete a source.
134
     *
135
     * @deprecated Use the `detach` method instead.
136
     *
137
     * @param  array|null         $params
138
     * @param  array|string|null  $options
139
     *
140
     * @return self
141
     */
142
    public function delete($params = null, $options = null)
143
    {
144
        return $this->detach($params, $options);
145
    }
146
147
    /**
148
     * Get the source transactions.
149
     *
150
     * @param  array|null         $params
151
     * @param  array|string|null  $options
152
     *
153
     * @return \Arcanedev\Stripe\Collection
154
     */
155 2
    public function sourceTransactions($params = null, $options = null)
156
    {
157 2
        list($response, $opts) = static::staticRequest(
158 2
            'get', $this->instanceUrl().'/source_transactions', $params, $options
159
        );
160
161
        /** @var  \Arcanedev\Stripe\Http\Response  $response */
162 2
        return Util::convertToStripeObject($response->getJson(), $opts)
0 ignored issues
show
Bug introduced by
It seems like $response->getJson() targeting Arcanedev\Stripe\Http\Response::getJson() can also be of type null; however, Arcanedev\Stripe\Utiliti...convertToStripeObject() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
163 2
                   ->setLastResponse($response);
164
    }
165
}
166