ApiCaller::getLastResponse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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