1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digitonic\IexCloudSdk; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use GuzzleHttp\Client as Guzzle; |
7
|
|
|
use GuzzleHttp\Exception\ClientException; |
8
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
9
|
|
|
use Psr\Http\Message\RequestInterface; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
use Digitonic\IexCloudSdk\Contracts\IEXCloud; |
12
|
|
|
use Digitonic\IexCloudSdk\Exceptions\WrongData; |
13
|
|
|
|
14
|
|
|
class Client implements IEXCloud |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var Guzzle|null |
18
|
|
|
*/ |
19
|
|
|
private $client = null; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Client constructor. |
23
|
|
|
* |
24
|
|
|
* @param Guzzle $client |
25
|
|
|
*/ |
26
|
92 |
|
public function __construct(Guzzle $client) |
27
|
|
|
{ |
28
|
92 |
|
$this->client = $client; |
29
|
92 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param RequestInterface $request |
33
|
|
|
* @param array $options |
34
|
|
|
* |
35
|
|
|
* @return ResponseInterface |
36
|
|
|
* @throws GuzzleException |
37
|
|
|
* @throws WrongData |
38
|
|
|
*/ |
39
|
|
|
public function send(RequestInterface $request, array $options = []): ResponseInterface |
40
|
|
|
{ |
41
|
|
|
try { |
42
|
|
|
$options['query'] = $this->mergeQuery($request); |
43
|
|
|
return $this->client->send($request); |
44
|
|
|
} |
45
|
|
|
catch (ClientException $e) { |
46
|
|
|
throw WrongData::invalidValuesProvided($e->getMessage()); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Merge the global query with current query (if has it) |
52
|
|
|
* |
53
|
|
|
* @param RequestInterface $request |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
|
|
private function mergeQuery(RequestInterface $request) { |
57
|
|
|
$currentQueryArray = Arr::get($this->client->getConfig(), 'query'); |
58
|
|
|
$newQueryArray = []; |
59
|
|
|
if($query = $request->getUri()->getQuery()) { |
60
|
|
|
parse_str($query, $newQueryArray); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return array_merge($currentQueryArray, $newQueryArray); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|