1 | <?php |
||
5 | class ExRatesTableFinder |
||
6 | { |
||
7 | const NBP_XML_URL = 'http://www.nbp.pl/kursy/xml/'; |
||
8 | const MIN_PUB_DATE = '2002-01-02'; |
||
9 | const MAX_ONE_TIME_API_REQ = 7; |
||
10 | |||
11 | /** @var \GuzzleHttp\Client */ |
||
12 | private $guzzle; |
||
13 | |||
14 | /** @var ExRatesTableFactory */ |
||
15 | private $ratesTableFactory; |
||
16 | |||
17 | /** @var string */ |
||
18 | private $cachePath; |
||
19 | |||
20 | /** @var \DateTime */ |
||
21 | private $soughtPubDate; |
||
22 | |||
23 | /** |
||
24 | * @param \GuzzleHttp\Client $guzzle |
||
25 | * @param ExRatesTableFactory $ratesTableFactory |
||
26 | * @param string $cachePath Optional path to an existing folder where the cache files will be stored |
||
27 | * |
||
28 | * @throws \Exception |
||
29 | */ |
||
30 | public function __construct( |
||
44 | |||
45 | /** |
||
46 | * Get the ExRatesTable instance |
||
47 | * |
||
48 | * @param \DateTime $pubDate Optional rates table publication date |
||
49 | * |
||
50 | * @return ExRatesTable |
||
51 | * |
||
52 | * @throws \Exception |
||
53 | */ |
||
54 | public function getExRatesTable(\DateTime $pubDate = null) |
||
91 | |||
92 | /** |
||
93 | * Set the sought publication date necessary for finder operation |
||
94 | * |
||
95 | * @param \DateTime|null $pubDate |
||
96 | * |
||
97 | * @throws \Exception |
||
98 | */ |
||
99 | private function setSoughtPubDate($pubDate) |
||
110 | |||
111 | /** |
||
112 | * Get the raw xml content from a cache file |
||
113 | * |
||
114 | * @return string|int Content string or 0 if the file doesn't exist |
||
115 | */ |
||
116 | private function getCachedXml() |
||
128 | |||
129 | /** |
||
130 | * Get the raw xml content from the NBP api |
||
131 | * |
||
132 | * @return string|int Content string or 0 if the file doesn't exist |
||
133 | * |
||
134 | * @throws \Exception |
||
135 | */ |
||
136 | private function downloadXml() |
||
156 | |||
157 | /** |
||
158 | * Find the file related to the publication date |
||
159 | * |
||
160 | * @return string|int Filename or 0 if the file was not found |
||
161 | * |
||
162 | * @throws \Exception |
||
163 | */ |
||
164 | private function findFileInRatesDir() |
||
182 | |||
183 | /** |
||
184 | * Construct the name of directory containing the files |
||
185 | * |
||
186 | * @return string |
||
187 | */ |
||
188 | private function constructDirname() |
||
197 | |||
198 | /** |
||
199 | * Searches files array for a match to the publication date |
||
200 | * |
||
201 | * @todo Optimize to avoid unnecessary regex |
||
202 | * |
||
203 | * @param array $filesArray |
||
204 | * |
||
205 | * @return string|int Filename or 0 if the file was not found |
||
206 | */ |
||
207 | private function matchFilename(array $filesArray) |
||
218 | } |
||
219 |