Completed
Push — master ( b9ac23...46f1c6 )
by ARCANEDEV
7s
created

Stripe::checkApiVersion()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
ccs 17
cts 17
cp 1
rs 8.439
cc 5
eloc 14
nc 4
nop 1
crap 5
1
<?php namespace Arcanedev\Stripe;
2
3
use Arcanedev\Stripe\Contracts\StripeInterface;
4
use Arcanedev\Stripe\Exceptions\ApiException;
5
use Arcanedev\Stripe\Exceptions\ApiKeyNotSetException;
6
7
/**
8
 * Class     Stripe
9
 *
10
 * @package  Arcanedev\Stripe
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
abstract class Stripe implements StripeInterface
14
{
15
    /* ------------------------------------------------------------------------------------------------
16
     |  Constants
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    /**
20
     * Library Version
21
     *
22
     * @var string
23
     */
24
    const VERSION = '3.10.0';
25
26
    /* ------------------------------------------------------------------------------------------------
27
     |  Properties
28
     | ------------------------------------------------------------------------------------------------
29
     */
30
    /**
31
     * The Stripe API key to be used for requests.
32
     *
33
     * @var string
34
     */
35
    private static $apiKey;
36
37
    /**
38
     * The base URL for the Stripe API.
39
     *
40
     * @var string
41
     */
42
    public static $apiBaseUrl    = 'https://api.stripe.com';
43
44
    /**
45
     * The base URL for the Stripe API uploads endpoint.
46
     *
47
     * @var string
48
     */
49
    public static $uploadBaseUrl = 'https://uploads.stripe.com';
50
51
    /**
52
     * The version of the Stripe API to use for requests.
53
     *
54
     * @var string|null
55
     */
56
    public static $apiVersion    = null;
57
58
    /**
59
     * The account ID for connected accounts requests.
60
     *
61
     * @var string|null
62
     */
63
    public static $accountId     = null;
64
65
    /**
66
     * Verify SSL Certs
67
     *
68
     * @var bool
69
     */
70
    public static $verifySslCerts = true;
71
72
    /* ------------------------------------------------------------------------------------------------
73
     |  Getters & Setters
74
     | ------------------------------------------------------------------------------------------------
75
     */
76
    /**
77
     * Get the API key used for requests.
78
     *
79
     * @return string
80
     */
81 699
    public static function getApiKey()
82
    {
83 699
        return self::$apiKey;
84
    }
85
86
    /**
87
     * Sets the API key to be used for requests.
88
     *
89
     * @param  string  $apiKey
90
     *
91
     * @throws ApiException
92
     * @throws ApiKeyNotSetException
93
     */
94 1299
    public static function setApiKey($apiKey)
95
    {
96 1299
        self::checkApiKey($apiKey);
97
98 1299
        self::$apiKey = $apiKey;
99 1299
    }
100
101
    /**
102
     * Get API Base URL.
103
     *
104
     * @return string
105
     */
106 709
    public static function getApiBaseUrl()
107
    {
108 709
        return self::$apiBaseUrl;
109
    }
110
111
    /**
112
     * Set API Base URL.
113
     *
114
     * @param  string  $apiBaseUrl
115
     *
116
     * @throws ApiException
117
     */
118 15
    public static function setApiBaseUrl($apiBaseUrl)
119
    {
120 15
        self::checkApiBaseUrl($apiBaseUrl);
121
122 5
        self::$apiBaseUrl = $apiBaseUrl;
123 5
    }
124
125
    /**
126
     * Get Upload Base URL.
127
     *
128
     * @return string
129
     */
130 19
    public static function getUploadBaseUrl()
131
    {
132 19
        return self::$uploadBaseUrl;
133
    }
134
135
136
    /**
137
     * Set Upload Base URL.
138
     *
139
     * @param  string  $uploadBaseUrl
140
     *
141
     * @throws ApiException
142
     */
143 15
    public static function setUploadBaseUrl($uploadBaseUrl)
144
    {
145 15
        self::checkUploadBaseUrl($uploadBaseUrl);
146
147 5
        self::$uploadBaseUrl = $uploadBaseUrl;
148 5
    }
149
150
    /**
151
     * The API version used for requests. null if we're using the latest version.
152
     *
153
     * @return string
154
     */
155 15
    public static function getApiVersion()
156
    {
157 15
        return self::$apiVersion;
158
    }
159
160
    /**
161
     * The API version used for requests. null if we're using the latest version.
162
     *
163
     * @return string
164
     */
165 5
    public static function version()
166
    {
167 5
        return self::getApiVersion();
168
    }
169
170
    /**
171
     * Sets the API version to use for requests.
172
     *
173
     * @param  string  $apiVersion
174
     */
175 45
    public static function setApiVersion($apiVersion)
176
    {
177 45
        self::checkApiVersion($apiVersion);
178
179 35
        self::$apiVersion = $apiVersion;
180 35
    }
181
182
    /**
183
     * Get the Stripe account ID for connected accounts requests.
184
     *
185
     * @return string|null
186
     */
187 10
    public static function getAccountId()
188
    {
189 10
        return self::$accountId;
190
    }
191
192
    /**
193
     * Set the Stripe account ID to set for connected accounts requests.
194
     *
195
     * @param  string  $accountId
196
     */
197 5
    public static function setAccountId($accountId)
198
    {
199 5
        self::$accountId = $accountId;
200 5
    }
201
202
    /**
203
     * Get Verify SSL Certs.
204
     *
205
     * @return bool
206
     */
207 5
    public static function getVerifySslCerts()
208
    {
209 5
        return self::$verifySslCerts;
210
    }
211
212
    /**
213
     * Sets Verify SSL Certs.
214
     *
215
     * @param  bool  $verify
216
     */
217 45
    public static function setVerifySslCerts($verify)
218
    {
219 45
        self::$verifySslCerts = validate_bool($verify);
220 45
    }
221
222
    /* ------------------------------------------------------------------------------------------------
223
     |  Main Functions
224
     | ------------------------------------------------------------------------------------------------
225
     */
226
    /**
227
     * Init Stripe.
228
     *
229
     * @param  string  $apiKey
230
     */
231 5
    public static function init($apiKey)
232
    {
233 5
        self::setApiKey($apiKey);
234 5
    }
235
236
    /* ------------------------------------------------------------------------------------------------
237
     |  Check Functions
238
     | ------------------------------------------------------------------------------------------------
239
     */
240
    /**
241
     * Check API Key.
242
     *
243
     * @param  string  $apiKey
244
     *
245
     * @throws ApiException
246
     * @throws ApiKeyNotSetException
247
     */
248 1299
    private static function checkApiKey(&$apiKey)
249
    {
250 1299
        if ( ! is_string($apiKey)) {
251 5
            throw new ApiException(
252 5
                'The API KEY must be a string value.',
253 1
                500
254 4
            );
255
        }
256
257 1299
        $apiKey = trim($apiKey);
258
259 1299
        if (empty($apiKey)) {
260 5
            throw new ApiKeyNotSetException(
261 1
                'You must specify your api key to use stripe.'
262 4
            );
263
        }
264 1299
    }
265
266
    /**
267
     * Check API Base URL.
268
     *
269
     * @param  string  $apiBaseUrl
270
     *
271
     * @throws ApiException
272
     */
273 15
    private static function checkApiBaseUrl($apiBaseUrl)
274
    {
275 15
        if ( ! is_string($apiBaseUrl)) {
276 5
            throw new ApiException(
277 5
                'The API base URL be string value. ' . gettype($apiBaseUrl) . ' is given.',
278 1
                500
279 4
            );
280
        }
281
282 10
        if ( ! validate_url($apiBaseUrl)) {
283 5
            throw new ApiException(
284 5
                'The API base URL is not a valid URL. ',
285 1
                500
286 4
            );
287
        }
288 5
    }
289
290
    /**
291
     * Check Upload Base URL.
292
     *
293
     * @param  string  $uploadBaseUrl
294
     *
295
     * @throws ApiException
296
     */
297 15
    private static function checkUploadBaseUrl($uploadBaseUrl)
298
    {
299 15
        if ( ! is_string($uploadBaseUrl)) {
300 5
            throw new ApiException(
301 5
                'The Upload base URL be string value. ' . gettype($uploadBaseUrl) . ' is given.',
302 1
                500
303 4
            );
304
        }
305
306 10
        if ( ! validate_url($uploadBaseUrl)) {
307 5
            throw new ApiException(
308 5
                'The Upload base URL is not a valid URL. ',
309 1
                500
310 4
            );
311
        }
312 5
    }
313
314
    /**
315
     * Check API Version.
316
     *
317
     * @param  string|null  $apiVersion
318
     *
319
     * @throws ApiException
320
     */
321 45
    private static function checkApiVersion(&$apiVersion)
322
    {
323
        if (
324 45
            ! is_null($apiVersion) &&
325 29
            ! is_string($apiVersion)
326 36
        ) {
327 5
            throw new ApiException(
328 5
                'The API version must be a null or string value. ' . gettype($apiVersion) . ' is given.',
329 1
                500
330 4
            );
331
        }
332
333 40
        if (is_null($apiVersion)) {
334 35
            return;
335
        }
336
337 20
        $apiVersion = trim($apiVersion);
338
339 20
        if ( ! validate_version($apiVersion)) {
340 5
            throw new ApiException(
341 5
                'The API version must valid a semantic version [x.x.x].',
342 1
                500
343 4
            );
344
        }
345 15
    }
346
347
    /**
348
     * Check if API version exists.
349
     *
350
     * @return bool
351
     */
352 684
    public static function hasApiVersion()
353
    {
354 684
        return ! empty(self::$apiVersion);
355
    }
356
357
    /**
358
     * Check if the Stripe has account ID for connected accounts requests.
359
     *
360
     * @return bool
361
     */
362 684
    public static function hasAccountId()
363
    {
364 684
        return ! empty(self::$accountId);
365
    }
366
}
367