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.

EcbClient   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 8
dl 0
loc 109
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A getExchangeRates() 0 14 4
A performCachedRequest() 0 23 3
A performRequest() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SteffenBrand\CurrCurr\Client;
6
7
use GuzzleHttp\Client;
8
use GuzzleHttp\Psr7\Response;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\SimpleCache\CacheInterface;
11
use SteffenBrand\CurrCurr\Exception\ExchangeRatesRequestFailedException;
12
use SteffenBrand\CurrCurr\Mapper\ExchangeRatesMapper;
13
use SteffenBrand\CurrCurr\Mapper\MapperInterface;
14
use SteffenBrand\CurrCurr\Model\CacheConfig;
15
use SteffenBrand\CurrCurr\Model\ExchangeRate;
16
17
/**
18
 * Class EcbClient
19
 * @package SteffenBrand\CurrCurr\Client
20
 */
21
class EcbClient implements EcbClientInterface
22
{
23
    /**
24
     * @var string
25
     */
26
    private $exchangeRatesUrl;
27
28
    /**
29
     * @var CacheConfig
30
     */
31
    private $cacheConfig;
32
33
    /**
34
     * @var MapperInterface
35
     */
36
    private $mapper;
37
38
    /**
39
     * @var Client
40
     */
41
    private $client;
42
43
    /**
44
     * EcbClient constructor.
45
     *
46
     * @param string $exchangeRatesUrl
47
     * @param CacheConfig|null $cacheConfig
48
     * @param MapperInterface|null $mapper
49
     */
50
    public function __construct(
51
        string $exchangeRatesUrl = self::DEFAULT_EXCHANGE_RATES_URL,
52
        CacheConfig $cacheConfig = null,
53
        MapperInterface $mapper = null)
54
    {
55
        $this->exchangeRatesUrl = $exchangeRatesUrl;
56
        $this->cacheConfig = $cacheConfig;
57
        if (null === $mapper) {
58
            $mapper = new ExchangeRatesMapper();
59
        }
60
        $this->mapper = $mapper;
61
        $this->client = new Client();
62
    }
63
64
    /**
65
     * Get exchange rates.
66
     *
67
     * @return ExchangeRate[]
68
     * @throws \SteffenBrand\CurrCurr\Exception\ExchangeRatesRequestFailedException
69
     * @throws \GuzzleHttp\Exception\GuzzleException
70
     * @throws \Psr\SimpleCache\InvalidArgumentException
71
     */
72
    public function getExchangeRates(): array
73
    {
74
        try {
75
            if (null !== $this->cacheConfig && $this->cacheConfig->getCache() instanceof CacheInterface) {
76
                $response = $this->performCachedRequest();
77
            } else {
78
                $response = $this->performRequest();
79
            }
80
        } catch (\Exception $e) {
81
            throw new ExchangeRatesRequestFailedException($e);
82
        }
83
84
        return $this->mapper->map($response);
85
    }
86
87
    /**
88
     * Perform cached request.
89
     *
90
     * @return \Psr\Http\Message\ResponseInterface
91
     * @throws \GuzzleHttp\Exception\GuzzleException
92
     * @throws \Psr\SimpleCache\InvalidArgumentException
93
     */
94
    private function performCachedRequest(): ResponseInterface
95
    {
96
        $responseBody = $this->cacheConfig->getCache()->get($this->cacheConfig->getCacheKey(), null);
97
        if (null !== $responseBody) {
98
            return new Response(200, [], $responseBody);
99
        }
100
101
        $response = $this->performRequest();
102
103
        if ($this->cacheConfig->getCacheTimeInSeconds() === CacheConfig::CACHE_UNTIL_MIDNIGHT) {
104
            $this->cacheConfig->setCacheTimeInSeconds(strtotime('tomorrow') - time());
105
        }
106
107
        $responseBody = (string) $response->getBody();
108
109
        $this->cacheConfig->getCache()->set(
110
            $this->cacheConfig->getCacheKey(),
111
            $responseBody,
112
            $this->cacheConfig->getCacheTimeInSeconds()
113
        );
114
115
        return $response;
116
    }
117
118
    /**
119
     * Perform request.
120
     *
121
     * @return \Psr\Http\Message\ResponseInterface
122
     * @throws \GuzzleHttp\Exception\GuzzleException
123
     */
124
    private function performRequest(): ResponseInterface
125
    {
126
        return $this->client->request('GET', $this->exchangeRatesUrl);
127
    }
128
129
}
130