Passed
Pull Request — master (#19)
by Alex
02:25
created

Client::send()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.1481

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 4
cts 6
cp 0.6667
rs 9.8333
c 0
b 0
f 0
cc 2
nc 3
nop 2
crap 2.1481
1
<?php
2
3
namespace Digitonic\IexCloudSdk;
4
5
use GuzzleHttp\Client as Guzzle;
6
use GuzzleHttp\Exception\ClientException;
7
use GuzzleHttp\Exception\GuzzleException;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
use Digitonic\IexCloudSdk\Contracts\IEXCloud;
11
use Digitonic\IexCloudSdk\Exceptions\WrongData;
12
13
class Client implements IEXCloud
14
{
15
    /**
16
     * @var Guzzle|null
17
     */
18
    private $client = null;
19
20
    /**
21
     * Client constructor.
22
     *
23
     * @param  Guzzle  $client
24
     */
25 167
    public function __construct(Guzzle $client)
26
    {
27 167
        $this->client = $client;
28 167
    }
29
30
    /**
31
     * @param  RequestInterface  $request
32
     * @param  array  $options
33
     *
34
     * @return ResponseInterface
35
     * @throws GuzzleException
36
     * @throws WrongData
37
     */
38 67
    public function send(RequestInterface $request, array $options = []): ResponseInterface
39
    {
40
        try {
41 67
            parse_str(parse_url($request->getUri(), PHP_URL_QUERY), $query);
42
43 67
            return $this->client->send($request, [
44 67
                'query' => array_merge($this->client->getConfig('query') ?? [], $query)
45
            ]);
46
        }
47
        catch (ClientException $e) {
48
            throw WrongData::invalidValuesProvided($e->getMessage());
49
        }
50
    }
51
}
52