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

Client::getQuery()   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 0
crap 2
1
<?php
2
3
namespace Bookeo;
4
5
use BadMethodCallException;
6
use ErrorException;
7
use GuzzleHttp\Exception\ClientException;
8
use Bookeo\Endpoints\Availability;
9
use Bookeo\Endpoints\Settings;
10
11
/**
12
 * @property Availability $availability Availability of time slots
13
 * @property Settings     $settings     Settings of account
14
 *
15
 * Single entry point for all classes
16
 *
17
 * @package Bookeo
18
 */
19
class Client
20
{
21
    use HttpTrait;
22
23
    /**
24
     * @var string
25
     */
26
    protected $namespace = __NAMESPACE__ . '\\Endpoints';
27
28
    /**
29
     * Type of query
30
     *
31
     * @var string
32
     */
33
    protected $type;
34
35
    /**
36
     * Endpoint of query
37
     *
38
     * @var string
39
     */
40
    protected $endpoint;
41
42
    /**
43
     * Parameters of query
44
     *
45
     * @var mixed
46
     */
47
    protected $params;
48
49
    /**
50
     * @var array
51
     */
52
    protected static $variables = [];
53
54
55
    /**
56
     * @var array
57
     */
58
    protected $query = [];
59
60
    /**
61
     * API constructor.
62
     *
63
     * @param array|Config $config
64
     * @throws ErrorException
65
     */
66
    public function __construct($config)
67
    {
68
        // If array then need create object
69
        if (is_array($config)) {
70
            $config = new Config($config);
71
        }
72
73
        if ($config instanceof Config) {
74
            // Save config into local variable
75
            $this->config = $config;
76
77
            // Each request should contain keys
78
            $this
79
                ->appendToQuery('secretKey', $config->get('secret_key'))
80
                ->appendToQuery('apiKey', $config->get('api_key'));
81
82
            // Store the client object
83
            $this->client = new \GuzzleHttp\Client($config->guzzle());
84
85
        } else {
86
            throw new \ErrorException('Config object is invalid');
87
        }
88
    }
89
90
    /**
91
     * Append some value to query
92
     *
93
     * @param string     $name
94
     * @param string|int $value
95
     *
96
     * @return $this
97
     */
98
    protected function appendToQuery(string $name, $value): self
99
    {
100
        $this->query[$name] = $value;
101
        return $this;
102
    }
103
104
    /**
105
     * Generate ready to use query from array of parameters
106
     *
107
     * @return string
108
     */
109
    protected function getQuery(): string
110
    {
111
        return http_build_query($this->query);
112
    }
113
114
    /**
115
     * Convert underscore_strings to camelCase (medial capitals).
116
     *
117
     * @param string $str
118
     *
119
     * @return string
120
     */
121
    private function snakeToPascal(string $str): string
122
    {
123
        // Remove underscores, capitalize words, squash, lowercase first.
124
        return str_replace(' ', '', ucwords(str_replace('_', ' ', $str)));
125
    }
126
127
    /**
128
     * Magic method required for call of another classes
129
     *
130
     * @param string $name
131
     *
132
     * @return bool|object
133
     * @throws BadMethodCallException
134
     */
135
    public function __get(string $name)
136
    {
137
        if (isset(self::$variables[$name])) {
138
            return self::$variables[$name];
139
        }
140
141
        // By default return is empty
142
        $object = '';
143
144
        // Set class name as namespace
145
        $class = $this->namespace . '\\' . $this->snakeToPascal($name);
146
147
        try {
148
149
            // Try to create object by name
150
            $object = new $class($this->config);
151
152
        } catch (ErrorException | ClientException $e) {
153
            echo $e->getMessage() . "\n";
154
            echo $e->getTrace();
155
        }
156
157
        // If object is not created
158
        if (!is_object($object)) {
159
            throw new BadMethodCallException("Class $class could not to be loaded");
160
        }
161
162
        return $object;
163
    }
164
165
    /**
166
     * Magic method required for call of another classes
167
     *
168
     * @param string $name
169
     * @param array  $arguments
170
     *
171
     * @return bool|object
172
     * @throws BadMethodCallException
173
     */
174
    public function __call(string $name, array $arguments)
175
    {
176
        // By default return is empty
177
        $object = '';
178
179
        // Set class name as namespace
180
        $class = $this->namespace . '\\' . $this->snakeToPascal($name);
181
182
        try {
183
184
            // Try to create object by name
185
            $object = new $class($this->config);
186
187
        } catch (ErrorException | ClientException $e) {
188
            echo $e->getMessage() . "\n";
189
            echo $e->getTrace();
190
        }
191
192
        // If object is not created
193
        if (!is_object($object)) {
194
            throw new BadMethodCallException("Class $class could not to be loaded");
195
        }
196
197
        return call_user_func_array($object, $arguments);
198
    }
199
200
    /**
201
     * Check if class is exist in folder
202
     *
203
     * @param string $name
204
     * @return bool
205
     */
206
    public function __isset(string $name): bool
207
    {
208
        return isset(self::$variables[$name]);
209
    }
210
211
    /**
212
     * Ordinary dummy setter, it should be ignored (added to PSR reasons)
213
     *
214
     * @param string $name
215
     * @param mixed  $value
216
     * @throws BadMethodCallException
217
     */
218
    public function __set(string $name, $value)
219
    {
220
        self::$variables[$name] = $value;
221
    }
222
}
223