GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 55d6ce...878a2b )
by Steffen
03:42
created

EcbClient::performRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace SteffenBrand\CurrCurr\Client;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Psr7\Response;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\SimpleCache\CacheInterface;
9
use SteffenBrand\CurrCurr\Exception\ExchangeRatesRequestFailedException;
10
use SteffenBrand\CurrCurr\Mapper\ExchangeRatesMapper;
11
use SteffenBrand\CurrCurr\Model\ExchangeRate;
12
13
class EcbClient implements EcbClientInterface
14
{
15
    /**
16
     * @const string
17
     */
18
    const DEFAULT_EXCHANGE_RATES_URL = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml';
19
20
    /**
21
     * @var Client
22
     */
23
    private $client;
24
25
    /**
26
     * @var CacheInterface
27
     */
28
    private $cache;
29
30
    /**
31
     * @var string
32
     */
33
    private $exchangeRatesUrl;
34
35
    /**
36
     * @param string $exchangeRatesUrl
37
     * @param CacheInterface $cache
38
     */
39
    public function __construct(string $exchangeRatesUrl = self::DEFAULT_EXCHANGE_RATES_URL, CacheInterface $cache = null)
40
    {
41
        $this->exchangeRatesUrl = $exchangeRatesUrl;
42
        $this->cache = $cache;
43
        $this->client = new Client();
44
    }
45
46
    /**
47
     * @throws ExchangeRatesRequestFailedException
48
     * @return ExchangeRate[]
49
     */
50
    public function getExchangeRates(): array
51
    {
52
        try {
53
            if (null !== $this->cache) {
54
                $date = new \DateTime('now');
55
                $key = $date->format('YY-mm-dd');
56
                if (null === $responseBody = $this->cache->get($key)) {
57
                    $response = $this->performRequest();
58
                    $this->cache->clear();
59
                    $this->cache->set($key, $response->getBody()->getContents());
60
                } else {
61
                    $response = new Response(200, [], $responseBody);
62
                }
63
            } else {
64
                $response = $this->performRequest();
65
            }
66
        } catch (\Exception $e) {
67
            throw new ExchangeRatesRequestFailedException($e);
68
        }
69
70
        $mapper = new ExchangeRatesMapper();
71
        return $mapper->map($response);
72
    }
73
74
    /**
75
     * @return ResponseInterface
76
     */
77
    private function performRequest()
78
    {
79
        $response = $this->client->request('GET', $this->exchangeRatesUrl);
80
        return $response;
81
    }
82
83
}
84