AlphaVantageService   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 16
c 1
b 0
f 0
dl 0
loc 27
ccs 13
cts 13
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A get() 0 14 3
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