Completed
Push — master ( 2a8373...bfc208 )
by Andreas
03:38
created

Everypay::reset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
/**
3
 * EveryPay PHP Library
4
 */
5
6
namespace Everypay;
7
8
/**
9
 * EveryPay configuration class.
10
 */
11
class Everypay
12
{
13
    /**
14
     * @var string
15
     */
16
    const VERSION = '2.4.0';
17
18
    public static $isTest = false;
19
20
    public static $throwExceptions = false;
21
22
    /**
23
     * API request key.
24
     *
25
     * @var string
26
     */
27
    protected static $apiKey = null;
28
29
    /**
30
     * EveryPay API url.
31
     *
32
     * @var string
33
     */
34
    protected static $apiUrl = 'https://api.everypay.gr';
35
36
    protected static $testApiUrl = 'https://sandbox-api.everypay.gr';
37
38
    /**
39
     * Check for needed requirements.
40
     *
41
     * @throws Everypay\Exception\RuntimeException
42
     */
43
    public static function checkRequirements()
44
    {
45
        $extensions = array('curl', 'json');
46
47
        foreach ($extensions as $extension) {
48
            if (!extension_loaded($extension)) {
49
                throw new Exception\RuntimeException(
50
                    'You need the PHP ' . $extension
51
                    . ' extension in order to use EveryPay PHP Library'
52
                );
53
            }
54
        }
55
    }
56
57
    /**
58
     * Set an API key for the request.
59
     *
60
     * @param string $key
61
     */
62
    public static function setApiKey($key)
63
    {
64
        self::$apiKey = (string) $key;
65
    }
66
67
    /**
68
     * Get the API Key.
69
     *
70
     * @return string
71
     * @throws Everypay\Exception\RuntimeException
72
     */
73
    public static function getApiKey()
74
    {
75
        if (self::$apiKey === null) {
76
            throw new Exception\RuntimeException(
77
                "You must set first an API key."
78
            );
79
        }
80
81
        return self::$apiKey;
82
    }
83
84
    /**
85
     * Set the API url for the request.
86
     *
87
     * @param string $url
88
     * @throws Everypay\Exception\InvalidArgumentException
89
     */
90
    public static function setApiUrl($url)
91
    {
92
        $apiUrl = filter_var($url, FILTER_VALIDATE_URL);
93
94
        if (!$apiUrl) {
95
            throw new Exception\InvalidArgumentException(
96
                'API URL should be a valid url'
97
            );
98
        }
99
100
        self::$apiUrl = rtrim($url, '\\/');
101
    }
102
103
    /**
104
     * Get the API url.
105
     *
106
     * @return string
107
     */
108
    public static function getApiUrl()
109
    {
110
        return self::$isTest
111
            ? self::$testApiUrl
112
            : self::$apiUrl;
113
    }
114
115
    public static function throwExceptions()
116
    {
117
        return self::$throwExceptions;
118
    }
119
120
    /**
121
     * Reset Everypay class to its default values
122
     *
123
     * @return void
124
     */
125
    public static function reset()
126
    {
127
        self::$apiKey = null;
128
        self::$isTest = false;
129
        self::$throwExceptions = false;
130
    }
131
}
132