1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the DataImporter package. |
7
|
|
|
* |
8
|
|
|
* (c) Loïc Sapone <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace IQ2i\DataImporter; |
15
|
|
|
|
16
|
|
|
use IQ2i\DataImporter\Archiver\ArchiverInterface; |
17
|
|
|
use IQ2i\DataImporter\Bundle\Messenger\ProcessItemMessage; |
18
|
|
|
use IQ2i\DataImporter\Exchange\MessageFactory; |
19
|
|
|
use IQ2i\DataImporter\Processor\AsyncProcessorInterface; |
20
|
|
|
use IQ2i\DataImporter\Processor\BatchProcessorInterface; |
21
|
|
|
use IQ2i\DataImporter\Processor\ProcessorInterface; |
22
|
|
|
use IQ2i\DataImporter\Reader\ReaderInterface; |
23
|
|
|
use Symfony\Component\Filesystem\Exception\IOException; |
24
|
|
|
use Symfony\Component\Messenger\MessageBusInterface; |
25
|
|
|
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter; |
26
|
|
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; |
27
|
|
|
use Symfony\Component\Serializer\Serializer; |
28
|
|
|
|
29
|
|
|
class DataImporter |
30
|
|
|
{ |
31
|
|
|
private readonly Serializer $serializer; |
32
|
|
|
|
33
|
5 |
|
public function __construct( |
34
|
|
|
private readonly ReaderInterface $reader, |
35
|
|
|
private readonly ProcessorInterface $processor, |
36
|
|
|
private readonly ?ArchiverInterface $archiver = null, |
37
|
|
|
?Serializer $serializer = null, |
38
|
|
|
private readonly ?MessageBusInterface $bus = null, |
39
|
|
|
) { |
40
|
5 |
|
$this->serializer = $serializer ?? new Serializer([new ObjectNormalizer(null, new CamelCaseToSnakeCaseNameConverter())]); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
5 |
|
public function execute(): void |
44
|
|
|
{ |
45
|
5 |
|
$this->processor->begin(MessageFactory::create($this->reader)); |
46
|
|
|
|
47
|
5 |
|
foreach ($this->reader as $data) { |
48
|
5 |
|
$message = MessageFactory::create( |
49
|
5 |
|
$this->reader, |
50
|
5 |
|
$this->reader->isDenormalizable() ? $this->serializeData($data) : $data |
51
|
5 |
|
); |
52
|
|
|
|
53
|
4 |
|
if ($this->processor instanceof AsyncProcessorInterface && null !== $this->bus) { |
54
|
1 |
|
$this->bus->dispatch(new ProcessItemMessage(fn () => $this->processor->item($message), $message)); |
55
|
|
|
|
56
|
1 |
|
continue; |
57
|
|
|
} |
58
|
|
|
|
59
|
3 |
|
$this->processor->item($message); |
60
|
|
|
|
61
|
3 |
|
if ($this->processor instanceof BatchProcessorInterface && ( |
62
|
3 |
|
0 === $this->reader->index() % $this->processor->getBatchSize() || $this->reader->index() === $this->reader->count() |
|
|
|
|
63
|
|
|
)) { |
64
|
2 |
|
$this->processor->batch(MessageFactory::create($this->reader)); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
4 |
|
$archivedFilePath = null; |
69
|
4 |
|
if (null !== $this->archiver) { |
70
|
1 |
|
$archivedFilePath = $this->doArchive(); |
71
|
|
|
} |
72
|
|
|
|
73
|
3 |
|
$this->processor->end(MessageFactory::create($this->reader, null, $archivedFilePath)); |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
private function serializeData(array $data): mixed |
77
|
|
|
{ |
78
|
|
|
try { |
79
|
1 |
|
return $this->serializer->denormalize($data, $this->reader->getDto()); |
|
|
|
|
80
|
1 |
|
} catch (\Exception $exception) { |
81
|
1 |
|
throw new \InvalidArgumentException('An error occurred while denormalizing data: '.$exception->getMessage(), $exception->getCode(), $exception); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
private function doArchive(): string |
86
|
|
|
{ |
87
|
|
|
try { |
88
|
1 |
|
return $this->archiver->archive($this->reader->getFile()); |
|
|
|
|
89
|
1 |
|
} catch (IOException $ioException) { |
90
|
1 |
|
throw new IOException('An error occurred while archiving file: '.$ioException->getMessage()); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|