Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.29% |
33 / 35 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| DepositImporter | |
94.29% |
33 / 35 |
|
66.67% |
2 / 3 |
11.02 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| importFromRequest | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
6 | |||
| getCsvDataFromRequest | |
80.00% |
8 / 10 |
|
0.00% |
0 / 1 |
4.13 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Service; |
| 6 | |
| 7 | use App\Entity\Deposit; |
| 8 | use App\Enum\PaymentMethodId; |
| 9 | use App\Helper\CsvParser\CsvObject; |
| 10 | use App\Helper\CsvParser\CsvParser; |
| 11 | use App\Repository\DepositRepository; |
| 12 | use App\Repository\PaymentMethodRepository; |
| 13 | use DateTime; |
| 14 | use Doctrine\ORM\EntityManagerInterface; |
| 15 | use RuntimeException; |
| 16 | use Symfony\Component\HttpFoundation\File\UploadedFile; |
| 17 | use Symfony\Component\HttpFoundation\Request; |
| 18 | use UnexpectedValueException; |
| 19 | |
| 20 | readonly class DepositImporter |
| 21 | { |
| 22 | public function __construct( |
| 23 | private PaymentMethodRepository $paymentMethodRepository, |
| 24 | private DepositRepository $depositRepository, |
| 25 | private EntityManagerInterface $entityManager, |
| 26 | ) {} |
| 27 | |
| 28 | public function importFromRequest(Request $request): int |
| 29 | { |
| 30 | $entity = $this->paymentMethodRepository->find(PaymentMethodId::BANK->value); |
| 31 | if (!$entity) { |
| 32 | throw new UnexpectedValueException('Invalid entity'); |
| 33 | } |
| 34 | |
| 35 | $csvData = $this->getCsvDataFromRequest($request); |
| 36 | $insertCount = 0; |
| 37 | |
| 38 | foreach ($csvData->lines as $line) { |
| 39 | if (!isset( |
| 40 | $line->descripcion, $line->fecha, |
| 41 | $line->{'numero de documento'}, $line->credito |
| 42 | )) { |
| 43 | continue; |
| 44 | } |
| 45 | |
| 46 | if (!str_starts_with( |
| 47 | (string) $line->descripcion, |
| 48 | 'TRANSFERENCIA DIRECTA DE' |
| 49 | )) { |
| 50 | continue; |
| 51 | } |
| 52 | |
| 53 | $deposit = new Deposit(); |
| 54 | $deposit->setEntity($entity) |
| 55 | ->setDate(new DateTime(str_replace('/', '-', (string) $line->fecha))) |
| 56 | ->setDocument($line->{'numero de documento'}) |
| 57 | ->setAmount($line->credito); |
| 58 | |
| 59 | if (false === $this->depositRepository->has($deposit)) { |
| 60 | $this->entityManager->persist($deposit); |
| 61 | ++$insertCount; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | $this->entityManager->flush(); |
| 66 | |
| 67 | return $insertCount; |
| 68 | } |
| 69 | |
| 70 | private function getCsvDataFromRequest(Request $request): CsvObject |
| 71 | { |
| 72 | $csvFile = $request->files->get('csv_file'); |
| 73 | if (!$csvFile instanceof UploadedFile) { |
| 74 | throw new RuntimeException('No CSV file received.'); |
| 75 | } |
| 76 | |
| 77 | $path = $csvFile->getRealPath(); |
| 78 | if (!$path) { |
| 79 | throw new RuntimeException('Invalid CSV file.'); |
| 80 | } |
| 81 | |
| 82 | $contents = file($path); |
| 83 | if (!$contents) { |
| 84 | throw new RuntimeException('Cannot read CSV file.'); |
| 85 | } |
| 86 | |
| 87 | return new CsvParser()->parseCSV($contents); |
| 88 | } |
| 89 | } |