1 <?php
2 3 4 5 6 7
8
9 namespace frankmayer\ArangoDbPhpCore;
10
11 use frankmayer\ArangoDbPhpCore\Connectors\AbstractHttpConnector;
12 use frankmayer\ArangoDbPhpCore\Plugins\PluginManager;
13 use frankmayer\ArangoDbPhpCore\Protocols\Http\HttpRequestInterface;
14 use frankmayer\ArangoDbPhpCore\Protocols\RequestInterface;
15 use frankmayer\ArangoDbPhpCore\Protocols\ResponseInterface;
16
17
18 19 20 21 22 23 24
25 class Client
26 {
27 protected $iocContainerArray;
28
29 30 31
32 public $pluginManager;
33 34 35
36 public $connector;
37 38 39
40 public $clientOptions;
41 42 43
44 public $databasePathPrefix;
45 46 47
48 public $database;
49 50 51
52 public $fullDatabasePath;
53 54 55
56 public $endpoint;
57 58 59
60 public $requestClass;
61
62 63 64
65 public $responseClass;
66 67 68
69 public $arangoDBApiVersion;
70
71
72 73 74 75 76
77 public function __construct(ConnectorInterface $connector, $clientOptions = null)
78 {
79 $this->connector = $connector;
80 $this->connector->client = $this;
81 $this->clientOptions = $clientOptions;
82 $this->endpoint = $this->clientOptions[ClientOptions::OPTION_ENDPOINT];
83 $this->databasePathPrefix = $this->clientOptions[ClientOptions::OPTION_DATABASE_PATH_PREFIX];
84 $this->database = $this->clientOptions[ClientOptions::OPTION_DEFAULT_DATABASE];
85 $this->fullDatabasePath = $this->databasePathPrefix . $this->database;
86
87 $this->requestClass = $this->clientOptions[ClientOptions::OPTION_REQUEST_CLASS];
88 $this->responseClass = $this->clientOptions[ClientOptions::OPTION_RESPONSE_CLASS];
89
90 if (isset($this->clientOptions['plugins'])) {
91 $this->pluginManager = new PluginManager($this,
92 isset($this->clientOptions['plugins']) ? $this->clientOptions['plugins'] : null,
93 isset($this->clientOptions['PluginManager']['options']) ? $this->clientOptions['PluginManager']['options'] : null);
94 };
95 }
96
97
98 99 100 101 102
103 public function setPluginsFromPluginArray($plugins = null)
104 {
105 return $this->pluginManager->setPluginsFromPluginArray($plugins);
106 }
107
108
109 110 111 112
113 public function notifyPlugins($eventName, $eventData = [])
114 {
115 if ($this->pluginManager) {
116 $this->pluginManager->notifyPlugins($eventName, $eventData);
117 }
118 }
119
120
121 122 123 124 125
126 public function doRequest($request)
127 {
128 $responseClass = $this->responseClass;
129
130
131 $responseObject = new $responseClass();
132
133 return $responseObject->build($request);
134 }
135
136
137 138 139 140 141 142
143 public function bind($type, \Closure $closure)
144 {
145 $this->iocContainerArray[$type] = $closure;
146 }
147
148
149 150 151 152 153 154 155 156
157 public function make($type)
158 {
159 if (isset($this->iocContainerArray[$type])) {
160 $type = $this->iocContainerArray[$type];
161
162 return $type();
163 }
164 throw new ClientException('No type registered with that name');
165 }
166
167
168 169 170 171 172
173 public function setArangoDBApiVersion($version)
174 {
175 $this->arangoDBApiVersion = $version;
176
177 return $this;
178 }
179
180
181 182 183
184 public function getArangoDBApiVersion()
185 {
186 return $this->arangoDBApiVersion;
187 }
188
189
190 191 192 193 194 195 196
197 public function setClientOptions($clientOptions)
198 {
199 $this->clientOptions = $clientOptions;
200
201 return $this;
202 }
203
204
205 206 207
208 public function getClientOptions()
209 {
210 return $this->clientOptions;
211 }
212
213
214 215 216 217 218
219 public function setConnector(ConnectorInterface $connector)
220 {
221 $this->connector = $connector;
222
223 return $this;
224 }
225
226
227 228 229
230 public function getConnector()
231 {
232 return $this->connector;
233 }
234
235
236 237 238 239 240
241 public function setDatabase($database)
242 {
243 $this->database = $database;
244
245 return $this;
246 }
247
248
249 250 251
252 public function getDatabase()
253 {
254 return $this->database;
255 }
256
257
258 259 260 261 262
263 public function setEndpoint($endpoint)
264 {
265 $this->endpoint = $endpoint;
266
267 return $this;
268 }
269
270
271 272 273
274 public function getEndpoint()
275 {
276 return $this->endpoint;
277 }
278
279
280 281 282 283 284
285 public function setPluginManager($pluginManager)
286 {
287 $this->pluginManager = $pluginManager;
288
289 return $this;
290 }
291
292
293 294 295
296 public function getPluginManager()
297 {
298 return $this->pluginManager;
299 }
300
301
302 303 304 305 306
307 public function setRequestClass($requestClass)
308 {
309 $this->requestClass = $requestClass;
310
311 return $this;
312 }
313
314
315 316 317
318 public function getRequestClass()
319 {
320 return $this->requestClass;
321 }
322
323
324 325 326
327 public function getRequest()
328 {
329 return new $this->requestClass($this);
330 }
331
332
333 334 335
336 public function getResponse()
337 {
338 return new $this->responseClass($this);
339 }
340
341
342 343 344 345 346
347 public function setResponseClass($responseClass)
348 {
349 $this->responseClass = $responseClass;
350
351 return $this;
352 }
353
354
355 356 357
358 public function getResponseClass()
359 {
360 return $this->responseClass;
361 }
362 }
363