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