Completed
Pull Request — master (#9)
by ARCANEDEV
15:10
created

Collection::checkPath()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 4
nc 2
nop 1
crap 3
1
<?php namespace Arcanedev\Stripe;
2
3
use Arcanedev\Stripe\Contracts\CollectionInterface;
4
use Arcanedev\Stripe\Exceptions\ApiException;
5
use Arcanedev\Stripe\Utilities\AutoPagingIterator;
6
use Arcanedev\Stripe\Utilities\Util;
7
8
/**
9
 * Class     Collection
10
 *
11
 * @package  Arcanedev\Stripe
12
 * @author   ARCANEDEV <[email protected]>
13
 *
14
 * @property  string  object
15
 * @property  array   data
16
 * @property  int     total_count
17
 * @property  bool    has_more
18
 * @property  string  url
19
 */
20
class Collection extends StripeResource implements CollectionInterface
21
{
22
    /* ------------------------------------------------------------------------------------------------
23
     |  Properties
24
     | ------------------------------------------------------------------------------------------------
25
     */
26
    protected $requestParams = [];
27
28
    /* ------------------------------------------------------------------------------------------------
29
     |  Getters & Setters
30
     | ------------------------------------------------------------------------------------------------
31
     */
32
    /**
33
     * Set the request parameters.
34
     *
35
     * @param  array  $params
36
     */
37 205
    public function setRequestParams($params)
38
    {
39 205
        $this->requestParams = $params;
40 205
    }
41
42
    /* ------------------------------------------------------------------------------------------------
43
     |  CRUD Functions
44
     | ------------------------------------------------------------------------------------------------
45
     */
46
    /**
47
     * List Function.
48
     *
49
     * @param  array              $params
50
     * @param  array|string|null  $options
51
     *
52
     * @throws ApiException
53
     *
54
     * @return self|array
55
     */
56 60
    public function all($params = [], $options = null)
57
    {
58 60
        return $this->requestAndConvertToStripeObject('get', $params, $options);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->requestAndConvert...t', $params, $options); of type Arcanedev\Stripe\StripeObject|array adds the type Arcanedev\Stripe\StripeObject to the return on line 58 which is incompatible with the return type declared by the interface Arcanedev\Stripe\Contrac...ollectionInterface::all of type Arcanedev\Stripe\Contrac...llectionInterface|array.
Loading history...
59
    }
60
61
    /**
62
     * Create Function.
63
     *
64
     * @param  array              $params
65
     * @param  array|string|null  $options
66
     *
67
     * @throws ApiException
68
     *
69
     * @return StripeObject|StripeResource|array
70
     */
71 70
    public function create($params = [], $options = null)
72
    {
73 70
        return $this->requestAndConvertToStripeObject('post', $params, $options);
74
    }
75
76
    /**
77
     * Retrieve Function.
78
     *
79
     * @param  string             $id
80
     * @param  array              $params
81
     * @param  array|string|null  $options
82
     *
83
     * @throws ApiException
84
     *
85
     * @return StripeObject|StripeResource|array
86
     */
87 25
    public function retrieve($id, $params = [], $options = null)
88
    {
89 25
        list($url, $params)    = $this->extractPathAndUpdateParams($params);
90 25
        $extn                  = urlencode(str_utf8($id));
91 25
        list($response, $opts) = $this->request('get', "$url/$extn", $params);
92
93 25
        $this->setRequestParams($params);
94
95 25
        return Util::convertToStripeObject($response, $opts);
96
    }
97
98
    /**
99
     * Make a request and convert to Stripe object.
100
     *
101
     * @param  string             $method
102
     * @param  array              $params
103
     * @param  array|string|null  $options
104
     *
105
     * @return self|StripeObject|StripeResource|array|
106
     */
107 100
    private function requestAndConvertToStripeObject($method, $params, $options)
108
    {
109 100
        list($url, $params)    = $this->extractPathAndUpdateParams($params);
110 95
        list($response, $opts) = $this->request($method, $url, $params, $options);
111
112 95
        $this->setRequestParams($params);
113
114 95
        return Util::convertToStripeObject($response, $opts);
115
    }
116
117
    /**
118
     * Get An iterator that can be used to iterate across all objects across all pages.
119
     *     As page boundaries are encountered, the next page will be fetched automatically
120
     *     for continued iteration.
121
     *
122
     * @return \Arcanedev\Stripe\Utilities\AutoPagingIterator
123
     */
124 10
    public function autoPagingIterator()
125
    {
126 10
        return AutoPagingIterator::make($this, $this->requestParams);
127
    }
128
129
    /* ------------------------------------------------------------------------------------------------
130
     |  Check Functions
131
     | ------------------------------------------------------------------------------------------------
132
     */
133
    /**
134
     * Check if Object is list.
135
     *
136
     * @return bool
137
     */
138 55
    public function isList()
139
    {
140 55
        return $this->object === 'list';
141
    }
142
143
    /**
144
     * Check if URL has path.
145
     *
146
     * @param  array  $url
147
     *
148
     * @throws ApiException
149
     */
150 100
    private function checkPath(array $url)
151
    {
152 100
        if ( ! isset($url['path']) || empty($url['path'])) {
153 5
            throw new ApiException(
154 5
                'Could not parse list url into parts: ' . $this->url
155 4
            );
156
        }
157 95
    }
158
159
    /* ------------------------------------------------------------------------------------------------
160
     |  Other Functions
161
     | ------------------------------------------------------------------------------------------------
162
     */
163
    /**
164
     * Get items Count.
165
     *
166
     * @return int
167
     */
168 5
    public function count()
169
    {
170 5
        return ($this->isList() && isset($this->total_count))
171 5
            ? $this->total_count
172 5
            : 0;
173
    }
174
175
    /**
176
     * Extract Path And Update Parameters.
177
     *
178
     * @param  array|null $params
179
     *
180
     * @throws ApiException
181
     *
182
     * @return array
183
     */
184 100
    private function extractPathAndUpdateParams($params)
185
    {
186 100
        $url = parse_url($this->url);
187
188 100
        if ($url === false) {
189
            $url = [];
190
        }
191
192 100
        $this->checkPath($url);
193
194 95
        if (isset($url['query'])) {
195
            // If the URL contains a query param, parse it out into $params so they
196
            // don't interact weirdly with each other.
197 5
            $query  = [];
198 5
            parse_str($url['query'], $query);
199
200 5
            $params = array_merge($params ?: [], $query);
201 4
        }
202
203 95
        return [$url['path'], $params];
204
    }
205
}
206