1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MidasSoft\DominicanBankParser\Parsers; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use DateTimeZone; |
7
|
|
|
use MidasSoft\DominicanBankParser\Collections\DepositCollection; |
8
|
|
|
use MidasSoft\DominicanBankParser\Exceptions\EmptyFileException; |
9
|
|
|
use MidasSoft\DominicanBankParser\Exceptions\InvalidArgumentException; |
10
|
|
|
use MidasSoft\DominicanBankParser\Interfaces\CacheInterface; |
11
|
|
|
use MidasSoft\DominicanBankParser\Interfaces\ParserInterface; |
12
|
|
|
|
13
|
|
|
abstract class AbstractParser implements ParserInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* CacheInterface instance. |
17
|
|
|
* |
18
|
|
|
* @var \MidasSoft\DominicanBankParser\Interfaces\CacheInterface |
19
|
|
|
*/ |
20
|
|
|
protected $cacheManager; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Sends a value to the cache manager. |
24
|
|
|
* |
25
|
|
|
* @return void |
26
|
|
|
*/ |
27
|
8 |
|
public function cache($data) |
28
|
|
|
{ |
29
|
8 |
|
if (!is_null($this->cacheManager)) { |
30
|
8 |
|
$timezone = $this->cacheManager->getConfigKey('timezone') ?? 'America/Santo_Domingo'; |
|
|
|
|
31
|
8 |
|
$key = (new DateTime('now', new DateTimeZone($timezone)))->format('Y-m-d H:i:s'); |
32
|
|
|
|
33
|
8 |
|
$this->cacheManager->add($key, $data); |
34
|
|
|
} |
35
|
8 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Validates that a file has data to parse. |
39
|
|
|
* |
40
|
|
|
* @param \MidasSoft\DominicanBankParser\Collections\DepositCollection $fileData |
41
|
|
|
* |
42
|
|
|
* @throws \MidasSoft\DominicanBankParser\Exceptions\EmptyFileException |
43
|
|
|
* |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
12 |
|
protected function failIfParsedFileIsEmpty(DepositCollection $fileData) |
47
|
|
|
{ |
48
|
12 |
|
if (count($fileData) == 0) { |
49
|
4 |
|
throw new EmptyFileException('You\'re trying to parse an empty file.'); |
50
|
|
|
} |
51
|
8 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns the cacheManager property. |
55
|
|
|
* |
56
|
|
|
* @return \MidasSoft\DominicanBankParser\Interfaces\CacheInterface |
57
|
|
|
*/ |
58
|
4 |
|
public function getCacheManager() |
59
|
|
|
{ |
60
|
4 |
|
return $this->cacheManager; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns the cacheManager property. |
65
|
|
|
* |
66
|
|
|
* @param \MidasSoft\DominicanBankParser\Interfaces\CacheInterface |
67
|
|
|
* |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
12 |
|
public function setCacheManager(CacheInterface $cacheManager) |
71
|
|
|
{ |
72
|
12 |
|
$this->cacheManager = $cacheManager; |
73
|
12 |
|
} |
74
|
|
|
} |
75
|
|
|
|