Passed
Branch 4.x (8bbc02)
by Loïc
02:29
created

DataImporter::execute()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 6
nop 0
dl 0
loc 23
ccs 14
cts 14
cp 1
crap 7
rs 8.8333
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\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
26
class DataImporter
27
{
28
    private readonly Serializer $serializer;
29
30 4
    public function __construct(
31
        private readonly ReaderInterface $reader,
32
        private readonly ProcessorInterface $processor,
33
        private readonly ?ArchiverInterface $archiver = null,
34
        ?Serializer $serializer = null,
35
    ) {
36 4
        $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...
37
    }
38
39 4
    public function execute(): void
40
    {
41 4
        $this->processor->begin(MessageFactory::create($this->reader));
42
43 4
        foreach ($this->reader as $data) {
44 4
            $this->processor->item(MessageFactory::create(
45 4
                $this->reader,
46 4
                $this->reader->isDenormalizable() ? $this->serializeData($data) : $data
47 4
            ));
48
49 3
            if ($this->processor instanceof BatchProcessorInterface && (
50 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

50
                0 === $this->reader->index() % $this->processor->/** @scrutinizer ignore-call */ getBatchSize() || $this->reader->index() === $this->reader->count()
Loading history...
51
            )) {
52 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

52
                $this->processor->/** @scrutinizer ignore-call */ 
53
                                  batch(MessageFactory::create($this->reader));
Loading history...
53
            }
54
        }
55
56 3
        $archivedFilePath = null;
57 3
        if (null !== $this->archiver) {
58 1
            $archivedFilePath = $this->doArchive();
59
        }
60
61 2
        $this->processor->end(MessageFactory::create($this->reader, null, $archivedFilePath));
62
    }
63
64 1
    private function serializeData(array $data): mixed
65
    {
66
        try {
67 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

67
            return $this->serializer->denormalize($data, /** @scrutinizer ignore-type */ $this->reader->getDto());
Loading history...
68 1
        } catch (\Exception $exception) {
69 1
            throw new \InvalidArgumentException('An error occurred while denormalizing data: '.$exception->getMessage(), $exception->getCode(), $exception);
70
        }
71
    }
72
73 1
    private function doArchive(): string
74
    {
75
        try {
76 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

76
            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...
77 1
        } catch (IOException $ioException) {
78 1
            throw new IOException('An error occurred while archiving file: '.$ioException->getMessage());
79
        }
80
    }
81
}
82