ImportCloseCommand::execute()   B
last analyzed

Complexity

Conditions 8
Paths 7

Size

Total Lines 45

Duplication

Lines 5
Ratio 11.11 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 5
loc 45
ccs 0
cts 23
cp 0
rs 7.9555
c 0
b 0
f 0
cc 8
nc 7
nop 2
crap 72
1
<?php
2
3
namespace TreeHouse\IoBundle\Command;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use TreeHouse\IoBundle\Entity\Import;
11
use TreeHouse\IoBundle\Entity\ImportRepository;
12
13
class ImportCloseCommand extends Command
14
{
15
    /**
16
     * @var ManagerRegistry
17
     */
18
    protected $doctrine;
19
20
    /**
21
     * @param ManagerRegistry $doctrine
22
     */
23
    public function __construct(ManagerRegistry $doctrine)
24
    {
25
        $this->doctrine = $doctrine;
26
27
        parent::__construct();
28
    }
29
30
    /**
31
     * @inheritdoc
32
     */
33
    protected function configure()
34
    {
35
        $this->setName('io:import:close');
36
        $this->addArgument('import', InputArgument::REQUIRED, 'The id of the import');
37
        $this->setDescription('Closes an import');
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43
    protected function execute(InputInterface $input, OutputInterface $output)
44
    {
45 View Code Duplication
        if (null === $import = $this->findImportById($input->getArgument('import'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Documentation introduced by
$input->getArgument('import') is of type string|array<integer,string>|null, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
46
            $output->writeln(sprintf('<error>Import %d does not exist</error>', $input->getArgument('import')));
47
48
            return 1;
49
        }
50
51
        // start import if it hasn't already
52
        if (!$import->isStarted()) {
53
            $output->writeln(sprintf('Starting import <comment>%d</comment> before closing it', $import->getId()));
54
            $this->getRepository()->startImport($import);
55
        }
56
57
        if ($import->isFinished()) {
58
            $output->writeln(sprintf('Import <comment>%d</comment> is already finished', $import->getId()));
59
60
            return 1;
61
        }
62
63
        $output->writeln(sprintf('Closing import <comment>%d</comment>', $import->getId()));
64
65
        if ($this->getRepository()->importHasUnfinishedParts($import)) {
66
            $output->writeln('Closing unfinished parts first');
67
68
            foreach ($import->getParts() as $part) {
69
                $output->writeln(sprintf('Closing part <comment>%d</comment>', $part->getPosition()));
70
71
                if (!$part->isStarted()) {
72
                    $part->setProcess(0);
73
                    $this->getRepository()->startImportPart($part);
74
                }
75
76
                if (!$part->isFinished()) {
77
                    $this->getRepository()->finishImportPart($part);
78
                }
79
            }
80
        }
81
82
        $this->getRepository()->finishImport($import);
83
84
        $output->writeln('<info>Import closed</info>');
85
86
        return 0;
87
    }
88
89
    /**
90
     * @param int $id
91
     *
92
     * @return Import
93
     */
94
    protected function findImportById($id)
95
    {
96
        return $this->getRepository()->find($id);
97
    }
98
99
    /**
100
     * @return ImportRepository
101
     */
102
    protected function getRepository()
103
    {
104
        return $this->doctrine->getRepository('TreeHouseIoBundle:Import');
105
    }
106
}
107