AbstractImportCommand   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 89.47%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
dl 0
loc 76
ccs 34
cts 38
cp 0.8947
rs 10
c 1
b 0
f 0
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A handleBatch() 0 3 1
A getArchiver() 0 3 1
A getSerializer() 0 3 1
A handleBegin() 0 3 1
A execute() 0 23 2
A getProcessor() 0 3 1
A handleEnd() 0 3 1
A configure() 0 7 1
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\Bundle\Command;
15
16
use IQ2i\DataImporter\Archiver\ArchiverInterface;
17
use IQ2i\DataImporter\Bundle\Exception\ItemHandlingException;
18
use IQ2i\DataImporter\Bundle\Processor\CliProcessor;
19
use IQ2i\DataImporter\DataImporter;
20
use IQ2i\DataImporter\Exchange\Message;
21
use IQ2i\DataImporter\Processor\ProcessorInterface;
22
use IQ2i\DataImporter\Reader\ReaderInterface;
23
use Symfony\Component\Console\Command\Command;
24
use Symfony\Component\Console\Input\InputArgument;
25
use Symfony\Component\Console\Input\InputInterface;
26
use Symfony\Component\Console\Input\InputOption;
27
use Symfony\Component\Console\Output\OutputInterface;
28
use Symfony\Component\Console\Style\SymfonyStyle;
29
use Symfony\Component\Serializer\SerializerInterface;
30
31
abstract class AbstractImportCommand extends Command
32
{
33
    protected InputInterface $input;
34
35
    protected OutputInterface $output;
36
37
    abstract protected function handleItem(): callable;
38
39
    abstract protected function getReader(?string $filename = null): ReaderInterface;
40
41 2
    protected function configure(): void
42
    {
43 2
        $this
44 2
            ->addArgument('filename', InputArgument::OPTIONAL, 'File to import')
45 2
            ->addOption('step', null, InputOption::VALUE_NONE, 'Step through each record one-by-one')
46 2
            ->addOption('pause-on-error', null, InputOption::VALUE_NONE, 'Pause if an exception is thrown')
47 2
            ->addOption('batch-size', null, InputOption::VALUE_REQUIRED, 'Batch size', 100)
48 2
        ;
49
    }
50
51 2
    protected function execute(InputInterface $input, OutputInterface $output): int
52
    {
53 2
        $this->input = $input;
54 2
        $this->output = $output;
55
56 2
        $io = new SymfonyStyle($input, $output);
57 2
        $io->title('Start importing data');
58
59
        try {
60 2
            (new DataImporter(
61 2
                $this->getReader($input->getArgument('filename')),
62 2
                $this->getProcessor($input, $output),
63 2
                $this->getArchiver(),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getArchiver() targeting IQ2i\DataImporter\Bundle...tCommand::getArchiver() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
64 2
                $this->getSerializer()
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getSerializer() targeting IQ2i\DataImporter\Bundle...ommand::getSerializer() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
65 2
            ))->execute();
66
        } catch (ItemHandlingException $itemHandlingException) {
67
            $io->newLine(2);
68
            $io->error($itemHandlingException->getMessage());
69
70
            return $itemHandlingException->getCode();
71
        }
72
73 2
        return Command::SUCCESS;
74
    }
75
76 2
    protected function handleBegin(): callable
77
    {
78 2
        return static function (Message $message) {
0 ignored issues
show
Unused Code introduced by
The parameter $message is not used and could be removed. ( Ignorable by Annotation )

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

78
        return static function (/** @scrutinizer ignore-unused */ Message $message) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79 2
        };
80
    }
81
82 2
    protected function handleBatch(): callable
83
    {
84 2
        return static function (Message $message) {
0 ignored issues
show
Unused Code introduced by
The parameter $message is not used and could be removed. ( Ignorable by Annotation )

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

84
        return static function (/** @scrutinizer ignore-unused */ Message $message) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
85 2
        };
86
    }
87
88 2
    protected function handleEnd(): callable
89
    {
90 2
        return static function (Message $message, array $errors) {
0 ignored issues
show
Unused Code introduced by
The parameter $errors is not used and could be removed. ( Ignorable by Annotation )

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

90
        return static function (Message $message, /** @scrutinizer ignore-unused */ array $errors) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $message is not used and could be removed. ( Ignorable by Annotation )

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

90
        return static function (/** @scrutinizer ignore-unused */ Message $message, array $errors) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
91 2
        };
92
    }
93
94 2
    protected function getProcessor(InputInterface $input, OutputInterface $output): ProcessorInterface
95
    {
96 2
        return new CliProcessor($input, $output, $this->handleBegin(), $this->handleItem(), $this->handleBatch(), $this->handleEnd(), $this->getSerializer());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getSerializer() targeting IQ2i\DataImporter\Bundle...ommand::getSerializer() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
97
    }
98
99 2
    protected function getArchiver(): ?ArchiverInterface
100
    {
101 2
        return null;
102
    }
103
104 2
    protected function getSerializer(): ?SerializerInterface
105
    {
106 2
        return null;
107
    }
108
}
109