1 | <?php |
||
29 | class FileRepository implements RepositoryInterface |
||
30 | { |
||
31 | const RATE_KEY_FORMAT = '%currency_code%_%date%_%rate_type%_%source_name%'; |
||
32 | |||
33 | /** |
||
34 | * File where all rates are persisted. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $pathToFile; |
||
39 | |||
40 | /** |
||
41 | * Collection of loaded rates. |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $rates; |
||
46 | |||
47 | /** |
||
48 | * Collection of latest rates (to speed up search process). |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $latest; |
||
53 | |||
54 | 2 | public function __construct($pathToFile) |
|
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | */ |
||
64 | 2 | public function save(array $rates) |
|
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | public function delete(array $rates) |
||
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | 2 | public function has($sourceName, $currencyCode, \DateTime $date = null, $rateType = 'default') |
|
119 | { |
||
120 | 2 | if ($date === null) { |
|
121 | 2 | $date = new \DateTime('now'); |
|
122 | 1 | } |
|
123 | |||
124 | 2 | return array_key_exists( |
|
125 | 1 | str_replace( |
|
126 | 2 | array('%currency_code%', '%date%', '%rate_type%', '%source_name%'), |
|
127 | 2 | array(CurrencyCodeUtil::clean($currencyCode), $date->format('Y-m-d'), $rateType, $sourceName), |
|
128 | 1 | self::RATE_KEY_FORMAT |
|
129 | 1 | ), |
|
130 | 2 | $this->rates |
|
131 | 1 | ); |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * {@inheritdoc} |
||
136 | */ |
||
137 | public function get($sourceName, $currencyCode, \DateTime $date = null, $rateType = 'default') |
||
138 | { |
||
139 | if ($date === null) { |
||
140 | $date = new \DateTime('now'); |
||
141 | } |
||
142 | |||
143 | if ($this->has($sourceName, $currencyCode, $date, $rateType)) { |
||
144 | return $this->rates[ |
||
145 | str_replace( |
||
146 | array('%currency_code%', '%date%', '%rate_type%', '%source_name%'), |
||
147 | array(CurrencyCodeUtil::clean($currencyCode), $date->format('Y-m-d'), $rateType, $sourceName), |
||
148 | self::RATE_KEY_FORMAT |
||
149 | ) |
||
150 | ]; |
||
151 | } |
||
152 | |||
153 | 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'))); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * {@inheritdoc} |
||
158 | */ |
||
159 | public function latest($sourceName, $currencyCode, $rateType = 'default') |
||
180 | |||
181 | /** |
||
182 | * {@inheritdoc} |
||
183 | */ |
||
184 | public function all(array $criteria = array()) |
||
204 | |||
205 | /** |
||
206 | * {@inheritdoc} |
||
207 | */ |
||
208 | public function count() |
||
212 | |||
213 | /** |
||
214 | * Load all rates from file. |
||
215 | * |
||
216 | * @return RateInterface[] |
||
217 | */ |
||
218 | 2 | protected function load() |
|
258 | |||
259 | /** |
||
260 | * Builds rate key to speed up search. |
||
261 | * |
||
262 | * @param RateInterface $rate |
||
263 | * @return string |
||
264 | */ |
||
265 | 2 | protected function getRateKey(RateInterface $rate) |
|
266 | { |
||
267 | 2 | return str_replace( |
|
268 | 2 | array('%currency_code%', '%date%', '%rate_type%', '%source_name%'), |
|
269 | 2 | array($rate->getCurrencyCode(), $rate->getDate()->format('Y-m-d'), $rate->getRateType(), $rate->getSourceName()), |
|
270 | 1 | self::RATE_KEY_FORMAT |
|
271 | 1 | ); |
|
272 | } |
||
273 | |||
274 | /** |
||
275 | * Initializes file storage. |
||
276 | */ |
||
277 | 2 | protected function initStorage() |
|
296 | } |
||
297 |