1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SteffenBrand\CurrCurr\Client; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Psr7\Response; |
7
|
|
|
use Psr\SimpleCache\CacheInterface; |
8
|
|
|
use SteffenBrand\CurrCurr\Exception\ExchangeRatesRequestFailedException; |
9
|
|
|
use SteffenBrand\CurrCurr\Mapper\ExchangeRatesMapper; |
10
|
|
|
use SteffenBrand\CurrCurr\Model\ExchangeRate; |
11
|
|
|
|
12
|
|
|
class EcbClient implements EcbClientInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @const string |
16
|
|
|
*/ |
17
|
|
|
const DEFAULT_EXCHANGE_RATES_URL = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @const int |
21
|
|
|
*/ |
22
|
|
|
const CACHE_UNTIL_MIDNIGHT = -1; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Client |
26
|
|
|
*/ |
27
|
|
|
private $client; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var CacheInterface |
31
|
|
|
*/ |
32
|
|
|
private $cache; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
private $cacheTimeInSeconds; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $exchangeRatesUrl; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $exchangeRatesUrl |
46
|
|
|
* @param CacheInterface $cache |
47
|
|
|
* @param int $cacheTimeInSeconds -1 to cache until midnight |
48
|
|
|
*/ |
49
|
|
|
public function __construct(string $exchangeRatesUrl = self::DEFAULT_EXCHANGE_RATES_URL, CacheInterface $cache = null, int $cacheTimeInSeconds = self::CACHE_UNTIL_MIDNIGHT) |
50
|
|
|
{ |
51
|
|
|
$this->exchangeRatesUrl = $exchangeRatesUrl; |
52
|
|
|
$this->cache = $cache; |
53
|
|
|
$this->cacheTimeInSeconds = $cacheTimeInSeconds; |
54
|
|
|
$this->client = new Client(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @throws ExchangeRatesRequestFailedException |
59
|
|
|
* @return ExchangeRate[] |
60
|
|
|
*/ |
61
|
|
|
public function getExchangeRates(): array |
62
|
|
|
{ |
63
|
|
|
try { |
64
|
|
|
if (null !== $this->cache) { |
65
|
|
|
$response = $this->performCachedRequest(); |
66
|
|
|
} else { |
67
|
|
|
$response = $this->performRequest(); |
68
|
|
|
} |
69
|
|
|
} catch (\Exception $e) { |
70
|
|
|
throw new ExchangeRatesRequestFailedException($e); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$mapper = new ExchangeRatesMapper(); |
74
|
|
|
return $mapper->map($response); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return Response |
79
|
|
|
*/ |
80
|
|
|
private function performCachedRequest() |
81
|
|
|
{ |
82
|
|
|
$now = new \DateTime(); |
83
|
|
|
$key = 'curr-curr-' . $now->format('YY-mm-dd'); |
84
|
|
|
$responseBody = $this->cache->get($key); |
85
|
|
|
|
86
|
|
|
if (null === $responseBody) { |
87
|
|
|
$response = $this->performRequest(); |
88
|
|
|
if ($this->cacheTimeInSeconds === self::CACHE_UNTIL_MIDNIGHT) { |
89
|
|
|
$this->cacheTimeInSeconds = strtotime('tomorrow') - time(); |
90
|
|
|
} |
91
|
|
|
$this->cache->set($key, $response->getBody()->getContents(), $this->cacheTimeInSeconds); |
92
|
|
|
} else { |
93
|
|
|
$response = new Response(200, [], $responseBody); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $response; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return Response |
101
|
|
|
*/ |
102
|
|
|
private function performRequest() |
103
|
|
|
{ |
104
|
|
|
$response = $this->client->request('GET', $this->exchangeRatesUrl); |
105
|
|
|
return $response; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
} |
109
|
|
|
|