Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.59% |
25 / 27 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| TatraBankaStatementMailParser | |
92.59% |
25 / 27 |
|
66.67% |
2 / 3 |
9.03 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| parseMulti | |
87.50% |
14 / 16 |
|
0.00% |
0 / 1 |
6.07 | |||
| parse | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | |
| 4 | namespace Tomaj\BankMailsParser\Parser\TatraBanka; |
| 5 | |
| 6 | use Tomaj\BankMailsParser\MailContent; |
| 7 | use Tomaj\BankMailsParser\Parser\ParserInterface; |
| 8 | |
| 9 | class TatraBankaStatementMailParser implements ParserInterface |
| 10 | { |
| 11 | private TatraBankaMailDecryptor $decryptor; |
| 12 | |
| 13 | public function __construct( |
| 14 | TatraBankaMailDecryptor $decryptor |
| 15 | ) { |
| 16 | $this->decryptor = $decryptor; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * @return MailContent[]|null |
| 21 | */ |
| 22 | public function parseMulti(string $content): ?array |
| 23 | { |
| 24 | $mailContents = []; |
| 25 | |
| 26 | $results = []; |
| 27 | $res = preg_match('/(-{5}BEGIN[A-Za-z0-9 \-\r?\n+\/=]+END PGP MESSAGE-{5})/m', $content, $results); |
| 28 | if (!$res) { |
| 29 | return null; |
| 30 | } |
| 31 | |
| 32 | $decrypted = $this->decryptor->decrypt($results[0]); |
| 33 | if ($decrypted === null) { |
| 34 | return null; |
| 35 | } |
| 36 | $transactions = preg_split("/\r\n|\n|\r/", $decrypted); |
| 37 | |
| 38 | foreach ($transactions as $line => $transaction) { |
| 39 | if (!$line) { |
| 40 | continue; |
| 41 | } |
| 42 | |
| 43 | $mailContent = $this->parse($transaction); |
| 44 | if ($mailContent !== null) { |
| 45 | $mailContents[] = $mailContent; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | return $mailContents; |
| 50 | } |
| 51 | |
| 52 | public function parse(string $content): ?MailContent |
| 53 | { |
| 54 | $cols = array_filter(explode('|', $content)); |
| 55 | if (empty($cols)) { |
| 56 | return null; |
| 57 | } |
| 58 | |
| 59 | $mailContent = new MailContent(); |
| 60 | $mailContent->setAmount((float) $cols[9]); |
| 61 | $mailContent->setCurrency($cols[10]); |
| 62 | $mailContent->setVs($cols[18]); |
| 63 | $mailContent->setAccountNumber(trim($cols[19])); |
| 64 | $mailContent->setTransactionDate(strtotime($cols[0])); |
| 65 | |
| 66 | return $mailContent; |
| 67 | } |
| 68 | } |