Passed
Push — 4.x ( ef1972...15d466 )
by Loïc
02:23
created

AbstractImportCommand   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 87.8%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 79
ccs 36
cts 41
cp 0.878
rs 10
wmc 10

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 5 2
A handleEnd() 0 3 1
A configure() 0 8 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\AsyncCliProcessor;
19
use IQ2i\DataImporter\Bundle\Processor\CliProcessor;
20
use IQ2i\DataImporter\DataImporter;
21
use IQ2i\DataImporter\Exchange\Message;
22
use IQ2i\DataImporter\Processor\ProcessorInterface;
23
use IQ2i\DataImporter\Reader\ReaderInterface;
24
use Symfony\Component\Console\Command\Command;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Command\Command was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use Symfony\Component\Console\Input\InputArgument;
26
use Symfony\Component\Console\Input\InputInterface;
27
use Symfony\Component\Console\Input\InputOption;
28
use Symfony\Component\Console\Output\OutputInterface;
29
use Symfony\Component\Console\Style\SymfonyStyle;
30
use Symfony\Component\Serializer\Serializer;
31
32
abstract class AbstractImportCommand extends Command
33
{
34
    protected InputInterface $input;
35
36
    protected OutputInterface $output;
37
38
    abstract protected function handleItem(): callable;
39
40
    abstract protected function getReader(?string $filename = null): ReaderInterface;
41
42 2
    protected function configure(): void
43
    {
44 2
        $this
45 2
            ->addArgument('filename', InputArgument::OPTIONAL, 'File to import')
46 2
            ->addOption('async', null, InputOption::VALUE_NONE, 'Parallelize row process using symfony/messenger')
47 2
            ->addOption('step', null, InputOption::VALUE_NONE, 'Step through each record one-by-one')
48 2
            ->addOption('pause-on-error', null, InputOption::VALUE_NONE, 'Pause if an exception is thrown')
49 2
            ->addOption('batch-size', null, InputOption::VALUE_REQUIRED, 'Batch size', 100)
50 2
        ;
51
    }
52
53 2
    protected function execute(InputInterface $input, OutputInterface $output): int
54
    {
55 2
        $this->input = $input;
56 2
        $this->output = $output;
57
58 2
        $io = new SymfonyStyle($input, $output);
59 2
        $io->title('Start importing data');
60
61
        try {
62 2
            (new DataImporter(
63 2
                $this->getReader($input->getArgument('filename')),
64 2
                $this->getProcessor($input, $output),
65 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...
66 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...
67 2
            ))->execute();
68
        } catch (ItemHandlingException $itemHandlingException) {
69
            $io->newLine(2);
70
            $io->error($itemHandlingException->getMessage());
71
72
            return $itemHandlingException->getCode();
73
        }
74
75 2
        return Command::SUCCESS;
76
    }
77
78 2
    protected function handleBegin(): callable
79
    {
80 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

80
        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...
81 2
        };
82
    }
83
84 2
    protected function handleBatch(): callable
85
    {
86 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

86
        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...
87 2
        };
88
    }
89
90 2
    protected function handleEnd(): callable
91
    {
92 2
        return static function (Message $message, array $errors) {
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

92
        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...
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

92
        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...
93 2
        };
94
    }
95
96 2
    protected function getProcessor(InputInterface $input, OutputInterface $output): ProcessorInterface
97
    {
98 2
        return $input->getOption('async')
99
            ? new AsyncCliProcessor($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...
100 2
            : 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...
101
    }
102
103 2
    protected function getArchiver(): ?ArchiverInterface
104
    {
105 2
        return null;
106
    }
107
108 2
    protected function getSerializer(): ?Serializer
109
    {
110 2
        return null;
111
    }
112
}
113