DataImporter   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 25
dl 0
loc 58
ccs 26
cts 26
cp 1
rs 10
c 4
b 0
f 0
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
B execute() 0 25 7
A __construct() 0 7 1
A serializeData() 0 9 2
A doArchive() 0 6 2
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())]);
0 ignored issues
show
Bug introduced by
The property serializer is declared read-only in IQ2i\DataImporter\DataImporter.
Loading history...
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()
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

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

55
                $this->processor->/** @scrutinizer ignore-call */ 
56
                                  batch(MessageFactory::create($this->reader));
Loading history...
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());
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

73
            return $serializer->denormalize($data, /** @scrutinizer ignore-type */ $this->reader->getDto());
Loading history...
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());
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

82
            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...
83 1
        } catch (IOException $ioException) {
84 1
            throw new IOException('An error occurred while archiving file: '.$ioException->getMessage());
85
        }
86
    }
87
}
88