AlphaVantageService::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace cHeeSaW\AlphaVantageBundle\Service;
6
7
use cHeeSaW\AlphaVantageBundle\Endpoints\Endpoint;
8
use Exception;
9
use GuzzleHttp\ClientInterface;
10
use GuzzleHttp\Exception\GuzzleException;
11
use GuzzleHttp\Psr7\Uri;
12
13
class AlphaVantageService implements AlphaVantage
14
{
15
    private ClientInterface $client;
16
    private string $alphaVantageApiUrl;
17
    private string $alphaVantageApiKey;
18
19 3
    public function __construct(ClientInterface $client, string $alphaVantageApiUrl, string $alphaVantageApiKey)
20
    {
21 3
        $this->client = $client;
22 3
        $this->alphaVantageApiUrl = $alphaVantageApiUrl;
23 3
        $this->alphaVantageApiKey = $alphaVantageApiKey;
24
    }
25
26 3
    public function get(Endpoint $endpoint): string
27
    {
28 3
        $uri = new Uri(
29 3
            $this->alphaVantageApiUrl . $endpoint->getQueryString() . '&apikey=' . $this->alphaVantageApiKey
30
        );
31
32
        try {
33 3
            $response = $this->client->request('GET', $uri);
34 2
            if ($response->getStatusCode() === 200) {
35 1
                return $response->getBody()->getContents();
36
            }
37 1
            throw new Exception('Invalid statuscode returned: ' . $response->getStatusCode());
38 2
        } catch (GuzzleException $exception) {
39 1
            throw new Exception('Invalid request made: ' . $exception->getMessage());
40
        }
41
    }
42
}
43