ApiCaller   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 85%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 48
ccs 17
cts 20
cp 0.85
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A convert() 0 26 3
A isLastCallEmpty() 0 4 1
A getLastResponse() 0 4 1
1
<?php
2
3
namespace CurrencyConverter;
4
5
class ApiCaller
6
{
7
    private $response;
8
9
    private $apiKey;
10
11 7
    public function __construct(string $apiKey)
12
    {
13 7
        $this->apiKey = $apiKey;
14 7
    }
15
16 2
    public function convert($from, $to)
17
    {
18
        $url = 'https://free.currencyconverterapi.com'
19
            . '/api/v5/convert'
20 2
            . '?q=' . $from . '_' . $to
21 2
            . '&compact=ultra'
22 2
            . '&apiKey=' . $this->apiKey;
23
24
25 2
        if ($handle = @fopen($url, 'r')) {
26
            $this->response = fgets($handle, 4096);
27
28
            fclose($handle);
29
30
            return;
31
        }
32
33 2
        $errorMessage = 'Error on convert rates';
34
35 2
        $errors = error_get_last();
36 2
        if (isset ($errors['message'])) {
37 2
            $errorMessage = $errors['message'];
38
        }
39
40 2
        throw new \Exception($errorMessage);
41
    }
42
43 1
    public function isLastCallEmpty(): bool
44
    {
45 1
        return $this->response == null;
46
    }
47
48 1
    public function getLastResponse(): ?string
49
    {
50 1
        return $this->response;
51
    }
52
}
53