1 | <?php |
||
28 | class FileRepository implements RepositoryInterface |
||
29 | { |
||
30 | const RATE_KEY_FORMAT = '%currency_code%_%date%_%rate_type%'; |
||
31 | |||
32 | /** |
||
33 | * File where all rates are persisted. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $pathToFile; |
||
38 | |||
39 | /** |
||
40 | * Collection of loaded rates. |
||
41 | * |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $rates; |
||
45 | |||
46 | /** |
||
47 | * Collection of latest rates (to speed up search process). |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $latest; |
||
52 | |||
53 | 2 | public function __construct($pathToFile) |
|
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | 2 | public function save(array $rates) |
|
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | */ |
||
102 | public function delete(array $rates) |
||
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | 2 | public function has($currencyCode, \DateTime $date = null, $rateType = 'default') |
|
118 | { |
||
119 | 2 | if ($date === null) { |
|
120 | 2 | $date = new \DateTime('now'); |
|
121 | 1 | } |
|
122 | |||
123 | 2 | return array_key_exists( |
|
124 | 1 | str_replace( |
|
125 | 2 | array('%currency_code%', '%date%', '%rate_type%'), |
|
126 | 2 | array($currencyCode, $date->format('Y-m-d'), $rateType), |
|
127 | 1 | self::RATE_KEY_FORMAT |
|
128 | 1 | ), |
|
129 | 2 | $this->rates |
|
130 | 1 | ); |
|
131 | } |
||
132 | |||
133 | /** |
||
134 | * {@inheritdoc} |
||
135 | */ |
||
136 | public function get($currencyCode, \DateTime $date = null, $rateType = 'default') |
||
137 | { |
||
138 | if ($date === null) { |
||
139 | $date = new \DateTime('now'); |
||
140 | } |
||
141 | |||
142 | if ($this->has($currencyCode, $date, $rateType)) { |
||
143 | return $this->rates[ |
||
144 | str_replace( |
||
145 | array('%currency_code%', '%date%', '%rate_type%'), |
||
146 | array($currencyCode, $date->format('Y-m-d'), $rateType), |
||
147 | self::RATE_KEY_FORMAT |
||
148 | ) |
||
149 | ]; |
||
150 | } |
||
151 | |||
152 | throw new ExchangeRateException(sprintf('Could not fetch rate for rate currency code "%s" and rate type "%s" on date "%s".', $currencyCode, $rateType, $date->format('Y-m-d'))); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | */ |
||
158 | public function latest($currencyCode, $rateType = 'default') |
||
172 | |||
173 | /** |
||
174 | * {@inheritdoc} |
||
175 | */ |
||
176 | public function all(array $criteria = array()) |
||
196 | |||
197 | /** |
||
198 | * {@inheritdoc} |
||
199 | */ |
||
200 | public function count() |
||
204 | |||
205 | /** |
||
206 | * Load all rates from file. |
||
207 | * |
||
208 | * @return RateInterface[] |
||
209 | */ |
||
210 | 2 | protected function load() |
|
250 | |||
251 | /** |
||
252 | * Builds rate key to speed up search. |
||
253 | * |
||
254 | * @param RateInterface $rate |
||
255 | * @return string |
||
256 | */ |
||
257 | 2 | protected function getRateKey(RateInterface $rate) |
|
258 | { |
||
259 | 2 | return str_replace( |
|
260 | 2 | array('%currency_code%', '%date%', '%rate_type%'), |
|
261 | 2 | array($rate->getCurrencyCode(), $rate->getDate()->format('Y-m-d'), $rate->getRateType()), |
|
262 | 1 | self::RATE_KEY_FORMAT |
|
263 | 1 | ); |
|
264 | } |
||
265 | |||
266 | /** |
||
267 | * Initializes file storage. |
||
268 | */ |
||
269 | 2 | protected function initStorage() |
|
288 | } |
||
289 |