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\Exchange\MessageFactory; |
18
|
|
|
use IQ2i\DataImporter\Processor\BatchProcessorInterface; |
19
|
|
|
use IQ2i\DataImporter\Processor\ProcessorInterface; |
20
|
|
|
use IQ2i\DataImporter\Reader\ReaderInterface; |
21
|
|
|
use Symfony\Component\Filesystem\Exception\IOException; |
22
|
|
|
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter; |
23
|
|
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; |
24
|
|
|
use Symfony\Component\Serializer\Serializer; |
25
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
26
|
|
|
|
27
|
|
|
class DataImporter |
28
|
|
|
{ |
29
|
|
|
private readonly SerializerInterface $serializer; |
30
|
|
|
|
31
|
9 |
|
public function __construct( |
32
|
|
|
private readonly ReaderInterface $reader, |
33
|
|
|
private readonly ProcessorInterface $processor, |
34
|
|
|
private readonly ?ArchiverInterface $archiver = null, |
35
|
|
|
?SerializerInterface $serializer = null, |
36
|
|
|
) { |
37
|
9 |
|
$this->serializer = $serializer ?? new Serializer([new ObjectNormalizer(null, new CamelCaseToSnakeCaseNameConverter())]); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
9 |
|
public function execute(): void |
41
|
|
|
{ |
42
|
9 |
|
$this->processor->begin(MessageFactory::create($this->reader)); |
43
|
|
|
|
44
|
9 |
|
foreach ($this->reader as $data) { |
45
|
9 |
|
$message = MessageFactory::create( |
46
|
9 |
|
$this->reader, |
47
|
9 |
|
$this->reader->isDenormalizable() ? $this->serializeData($data) : $data |
48
|
9 |
|
); |
49
|
|
|
|
50
|
8 |
|
$this->processor->item($message); |
51
|
|
|
|
52
|
8 |
|
if ($this->processor instanceof BatchProcessorInterface && ( |
53
|
8 |
|
0 === $this->reader->index() % $this->processor->getBatchSize() || $this->reader->index() === $this->reader->count() |
|
|
|
|
54
|
|
|
)) { |
55
|
3 |
|
$this->processor->batch(MessageFactory::create($this->reader)); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
8 |
|
$archivedFilePath = null; |
60
|
8 |
|
if (null !== $this->archiver) { |
61
|
4 |
|
$archivedFilePath = $this->doArchive(); |
62
|
|
|
} |
63
|
|
|
|
64
|
7 |
|
$this->processor->end(MessageFactory::create($this->reader, null, $archivedFilePath)); |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
private function serializeData(array $data): mixed |
68
|
|
|
{ |
69
|
|
|
try { |
70
|
|
|
/** @var Serializer $serializer */ |
71
|
2 |
|
$serializer = $this->serializer; |
72
|
|
|
|
73
|
2 |
|
return $serializer->denormalize($data, $this->reader->getDto()); |
|
|
|
|
74
|
1 |
|
} catch (\Exception $exception) { |
75
|
1 |
|
throw new \InvalidArgumentException('An error occurred while denormalizing data: '.$exception->getMessage(), $exception->getCode(), $exception); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
4 |
|
private function doArchive(): string |
80
|
|
|
{ |
81
|
|
|
try { |
82
|
4 |
|
return $this->archiver->archive($this->reader->getFile()); |
|
|
|
|
83
|
1 |
|
} catch (IOException $ioException) { |
84
|
1 |
|
throw new IOException('An error occurred while archiving file: '.$ioException->getMessage()); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|