Completed
Push — master ( eece8c...a3a436 )
by Mr
02:11
created

Config::__isset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Bookeo;
4
5
use ErrorException;
6
7
class Config
8
{
9
    /**
10
     * List of allowed parameters
11
     */
12
    public const ALLOWED = [
13
        'user_agent',
14
        'base_uri',
15
        'secret_key',
16
        'api_key',
17
        'timeout',
18
        'tries',
19
        'seconds',
20
        'debug',
21
        'track_redirects'
22
    ];
23
24
    /**
25
     * List of minimal required parameters
26
     */
27
    public const REQUIRED = [
28
        'user_agent',
29
        'base_uri',
30
        'timeout',
31
        'secret_key',
32
        'api_key',
33
    ];
34
35
    /**
36
     * List of configured parameters
37
     *
38
     * @var array
39
     */
40
    private $_parameters;
41
42
    /**
43
     * Config constructor.
44
     *
45
     * @param array $parameters List of parameters which can be set on object creation stage
46
     * @throws \ErrorException
47
     */
48
    public function __construct(array $parameters = [])
49
    {
50
        // Set default parameters of client
51
        $this->_parameters = [
52
            // Errors must be disabled by default, because we need to get error codes
53
            // @link http://docs.guzzlephp.org/en/stable/request-options.html#http-errors
54
            'http_errors'     => false,
55
56
            // Wrapper settings
57
            'tries'           => 2,  // Count of tries
58
            'seconds'         => 10, // Waiting time per each try
59
60
            // Optional parameters
61
            'debug'           => false,
62
            'track_redirects' => false,
63
64
            // Main parameters
65
            'timeout'         => 20,
66
            'user_agent'      => 'Bookeo PHP Client',
67
            'base_uri'        => 'https://api.bookeo.com/v2'
68
        ];
69
70
        // Overwrite parameters by client input
71
        foreach ($parameters as $key => $value) {
72
            $this->set($key, $value);
73
        }
74
    }
75
76
    /**
77
     * Magic setter parameter by name
78
     *
79
     * @param string               $name  Name of parameter
80
     * @param string|bool|int|null $value Value of parameter
81
     *
82
     * @throws \ErrorException
83
     */
84
    public function __set(string $name, $value)
85
    {
86
        $this->set($name, $value);
87
    }
88
89
    /**
90
     * Check if parameter if available
91
     *
92
     * @param string $name Name of parameter
93
     *
94
     * @return bool
95
     */
96
    public function __isset($name): bool
97
    {
98
        return isset($this->_parameters[$name]);
99
    }
100
101
    /**
102
     * Get parameter via magic call
103
     *
104
     * @param string $name Name of parameter
105
     *
106
     * @return bool|int|string|null
107
     * @throws ErrorException
108
     */
109
    public function __get($name)
110
    {
111
        return $this->get($name);
112
    }
113
114
    /**
115
     * Remove parameter from array
116
     *
117
     * @param string $name Name of parameter
118
     */
119
    public function __unset($name)
120
    {
121
        unset($this->_parameters[$name]);
122
    }
123
124
    /**
125
     * Set parameter by name
126
     *
127
     * @param string               $name  Name of parameter
128
     * @param string|bool|int|null $value Value of parameter
129
     *
130
     * @return $this
131
     * @throws ErrorException
132
     */
133
    public function set(string $name, $value): self
134
    {
135
        if (!\in_array($name, self::ALLOWED, false)) {
136
            throw new \ErrorException("Parameter \"$name\" is not in available list [" . implode(',', self::ALLOWED) . ']');
137
        }
138
139
        // Add parameters into array
140
        $this->_parameters[$name] = $value;
141
        return $this;
142
    }
143
144
    /**
145
     * Get available parameter by name
146
     *
147
     * @param string $name Name of parameter
148
     *
149
     * @return bool|int|string|null
150
     * @throws ErrorException
151
     */
152
    public function get(string $name)
153
    {
154
        if (!isset($this->_parameters[$name])) {
155
            throw new \ErrorException("Parameter \"$name\" is not in set");
156
        }
157
158
        return $this->_parameters[$name];
159
    }
160
161
    /**
162
     * Get all available parameters
163
     *
164
     * @return array
165
     */
166
    public function all(): array
167
    {
168
        return $this->_parameters;
169
    }
170
171
    /**
172
     * Return all ready for Guzzle parameters
173
     *
174
     * @return array
175
     * @throws ErrorException
176
     */
177
    public function guzzle(): array
178
    {
179
        return [
180
            // 'base_uri'        => $this->get('base_uri'), // By some reasons base_uri option is not work anymore
181
            'timeout'         => $this->get('timeout'),
182
            'track_redirects' => $this->get('track_redirects'),
183
            'debug'           => $this->get('debug'),
184
            'headers'         => [
185
                'User-Agent' => $this->get('user_agent'),
186
            ]
187
        ];
188
    }
189
}
190