Completed
Pull Request — master (#49)
by ARCANEDEV
10:48
created

Stripe::setLogger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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