Passed
Push — 4.x ( 44360b...b6abc2 )
by Loïc
02:26
created

DataImporter::doArchive()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
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())]);
0 ignored issues
show
Bug introduced by
The property serializer is declared read-only in IQ2i\DataImporter\DataImporter.
Loading history...
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()
0 ignored issues
show
Bug introduced by
The method getBatchSize() does not exist on IQ2i\DataImporter\Processor\ProcessorInterface. It seems like you code against a sub-type of IQ2i\DataImporter\Processor\ProcessorInterface such as IQ2i\DataImporter\Proces...BatchProcessorInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
                0 === $this->reader->index() % $this->processor->/** @scrutinizer ignore-call */ getBatchSize() || $this->reader->index() === $this->reader->count()
Loading history...
63
            )) {
64 2
                $this->processor->batch(MessageFactory::create($this->reader));
0 ignored issues
show
Bug introduced by
The method batch() does not exist on IQ2i\DataImporter\Processor\ProcessorInterface. It seems like you code against a sub-type of IQ2i\DataImporter\Processor\ProcessorInterface such as IQ2i\DataImporter\Proces...BatchProcessorInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
                $this->processor->/** @scrutinizer ignore-call */ 
65
                                  batch(MessageFactory::create($this->reader));
Loading history...
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());
0 ignored issues
show
Bug introduced by
It seems like $this->reader->getDto() can also be of type null; however, parameter $type of Symfony\Component\Serial...rializer::denormalize() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
            return $this->serializer->denormalize($data, /** @scrutinizer ignore-type */ $this->reader->getDto());
Loading history...
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());
0 ignored issues
show
Bug introduced by
The method archive() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
            return $this->archiver->/** @scrutinizer ignore-call */ archive($this->reader->getFile());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
89 1
        } catch (IOException $ioException) {
90 1
            throw new IOException('An error occurred while archiving file: '.$ioException->getMessage());
91
        }
92
    }
93
}
94