Completed
Push — master ( 2b0b4a...2b0b4a )
by ARCANEDEV
51s
created

Stripe::getAppInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 1
cts 1
cp 1
crap 1
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.19.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
     * The application's information (name, version, URL)
74
     *
75
     * @var array
76
     */
77
    public static $appInfo = [];
78
79
    /* ------------------------------------------------------------------------------------------------
80
     |  Getters & Setters
81 809
     | ------------------------------------------------------------------------------------------------
82
     */
83 809
    /**
84
     * Get the API key used for requests.
85
     *
86
     * @return string
87
     */
88
    public static function getApiKey()
89
    {
90
        return self::$apiKey;
91
    }
92
93
    /**
94 1434
     * Sets the API key to be used for requests.
95
     *
96 1434
     * @param  string  $apiKey
97
     *
98 1434
     * @throws ApiException
99 1434
     * @throws ApiKeyNotSetException
100
     */
101
    public static function setApiKey($apiKey)
102
    {
103
        self::checkApiKey($apiKey);
104
105
        self::$apiKey = $apiKey;
106 819
    }
107
108 819
    /**
109
     * Get API Base URL.
110
     *
111
     * @return string
112
     */
113
    public static function getApiBaseUrl()
114
    {
115
        return self::$apiBaseUrl;
116
    }
117
118 15
    /**
119
     * Set API Base URL.
120 15
     *
121
     * @param  string  $apiBaseUrl
122 5
     *
123 5
     * @throws ApiException
124
     */
125
    public static function setApiBaseUrl($apiBaseUrl)
126
    {
127
        self::checkApiBaseUrl($apiBaseUrl);
128
129
        self::$apiBaseUrl = $apiBaseUrl;
130 19
    }
131
132 19
    /**
133
     * Get Upload Base URL.
134
     *
135
     * @return string
136
     */
137
    public static function getUploadBaseUrl()
138
    {
139
        return self::$uploadBaseUrl;
140
    }
141
142
143 15
    /**
144
     * Set Upload Base URL.
145 15
     *
146
     * @param  string  $uploadBaseUrl
147 5
     *
148 5
     * @throws ApiException
149
     */
150
    public static function setUploadBaseUrl($uploadBaseUrl)
151
    {
152
        self::checkUploadBaseUrl($uploadBaseUrl);
153
154
        self::$uploadBaseUrl = $uploadBaseUrl;
155 15
    }
156
157 15
    /**
158
     * The API version used for requests. null if we're using the latest version.
159
     *
160
     * @return string
161
     */
162
    public static function getApiVersion()
163
    {
164
        return self::$apiVersion;
165 5
    }
166
167 5
    /**
168
     * The API version used for requests. null if we're using the latest version.
169
     *
170
     * @return string
171
     */
172
    public static function version()
173
    {
174
        return self::getApiVersion();
175 45
    }
176
177 45
    /**
178
     * Sets the API version to use for requests.
179 35
     *
180 35
     * @param  string  $apiVersion
181
     */
182
    public static function setApiVersion($apiVersion)
183
    {
184
        self::checkApiVersion($apiVersion);
185
186
        self::$apiVersion = $apiVersion;
187 5
    }
188
189 5
    /**
190
     * Get the Stripe account ID for connected accounts requests.
191
     *
192
     * @return string|null
193
     */
194
    public static function getAccountId()
195
    {
196
        return self::$accountId;
197 5
    }
198
199 5
    /**
200 5
     * Set the Stripe account ID to set for connected accounts requests.
201
     *
202
     * @param  string  $accountId
203
     */
204
    public static function setAccountId($accountId)
205
    {
206
        self::$accountId = $accountId;
207 5
    }
208
209 5
    /**
210
     * Get Verify SSL Certs.
211
     *
212
     * @return bool
213
     */
214
    public static function getVerifySslCerts()
215
    {
216
        return self::$verifySslCerts;
217 45
    }
218
219 45
    /**
220 45
     * Sets Verify SSL Certs.
221
     *
222
     * @param  bool  $verify
223
     */
224
    public static function setVerifySslCerts($verify)
225
    {
226
        self::$verifySslCerts = validate_bool($verify);
227
    }
228
229
    /**
230
     * Get the Application's information.
231 5
     *
232
     * @return array
233 5
     */
234 5
    public static function getAppInfo()
235
    {
236
        return self::$appInfo;
237
    }
238
239
    /**
240
     * Set the Application's information.
241
     *
242
     * @param  string  $name     The application's name
243
     * @param  string  $version  The application's version
244
     * @param  string  $url      The application's URL
245
     */
246
    public static function setAppInfo($name, $version = null, $url = null)
247
    {
248 1434
        self::$appInfo = compact('name', 'version', 'url');
249
    }
250 1434
251 5
    /* ------------------------------------------------------------------------------------------------
252 5
     |  Main Functions
253 1
     | ------------------------------------------------------------------------------------------------
254 4
     */
255
    /**
256
     * Init Stripe.
257 1434
     *
258
     * @param  string  $apiKey
259 1434
     */
260 5
    public static function init($apiKey)
261 1
    {
262 4
        self::setApiKey($apiKey);
263
    }
264 1434
265
    /* ------------------------------------------------------------------------------------------------
266
     |  Check Functions
267
     | ------------------------------------------------------------------------------------------------
268
     */
269
    /**
270
     * Check API Key.
271
     *
272
     * @param  string  $apiKey
273 15
     *
274
     * @throws ApiException
275 15
     * @throws ApiKeyNotSetException
276 5
     */
277 5
    private static function checkApiKey(&$apiKey)
278 1
    {
279 4
        if ( ! is_string($apiKey)) {
280
            throw new ApiException(
281
                'The API KEY must be a string value.',
282 10
                500
283 5
            );
284 5
        }
285 1
286 4
        $apiKey = trim($apiKey);
287
288 5
        if (empty($apiKey)) {
289
            throw new ApiKeyNotSetException(
290
                'You must specify your api key to use stripe.'
291
            );
292
        }
293
    }
294
295
    /**
296
     * Check API Base URL.
297 15
     *
298
     * @param  string  $apiBaseUrl
299 15
     *
300 5
     * @throws ApiException
301 5
     */
302 1
    private static function checkApiBaseUrl($apiBaseUrl)
303 4
    {
304
        if ( ! is_string($apiBaseUrl)) {
305
            throw new ApiException(
306 10
                'The API base URL be string value. ' . gettype($apiBaseUrl) . ' is given.',
307 5
                500
308 5
            );
309 1
        }
310 4
311
        if ( ! validate_url($apiBaseUrl)) {
312 5
            throw new ApiException(
313
                'The API base URL is not a valid URL. ',
314
                500
315
            );
316
        }
317
    }
318
319
    /**
320
     * Check Upload Base URL.
321 45
     *
322
     * @param  string  $uploadBaseUrl
323
     *
324 45
     * @throws ApiException
325 29
     */
326 36
    private static function checkUploadBaseUrl($uploadBaseUrl)
327 5
    {
328 5
        if ( ! is_string($uploadBaseUrl)) {
329 1
            throw new ApiException(
330 4
                'The Upload base URL be string value. ' . gettype($uploadBaseUrl) . ' is given.',
331
                500
332
            );
333 40
        }
334 35
335
        if ( ! validate_url($uploadBaseUrl)) {
336
            throw new ApiException(
337 20
                'The Upload base URL is not a valid URL. ',
338
                500
339 20
            );
340 5
        }
341 5
    }
342 1
343 4
    /**
344
     * Check API Version.
345 15
     *
346
     * @param  string|null  $apiVersion
347
     *
348
     * @throws ApiException
349
     */
350
    private static function checkApiVersion(&$apiVersion)
351
    {
352 774
        if (
353
            ! is_null($apiVersion) &&
354 774
            ! is_string($apiVersion)
355
        ) {
356
            throw new ApiException(
357
                'The API version must be a null or string value. ' . gettype($apiVersion) . ' is given.',
358
                500
359
            );
360
        }
361
362 774
        if (is_null($apiVersion)) {
363
            return;
364 774
        }
365
366
        $apiVersion = trim($apiVersion);
367
368
        if ( ! validate_version($apiVersion)) {
369
            throw new ApiException(
370
                'The API version must valid a semantic version [x.x.x].',
371
                500
372
            );
373
        }
374
    }
375
376
    /**
377
     * Check if API version exists.
378
     *
379
     * @return bool
380
     */
381
    public static function hasApiVersion()
382
    {
383
        return ! empty(self::$apiVersion);
384
    }
385
386
    /**
387
     * Check if the Stripe has account ID for connected accounts requests.
388
     *
389
     * @return bool
390
     */
391
    public static function hasAccountId()
392
    {
393
        return ! empty(self::$accountId);
394
    }
395
}
396