ImportRescheduleCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace TreeHouse\IoBundle\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use TreeHouse\IoBundle\Entity\ImportPart;
10
use TreeHouse\IoBundle\Import\ImportScheduler;
11
use TreeHouse\IoBundle\Import\Processor\ProcessorInterface;
12
13
class ImportRescheduleCommand extends Command
14
{
15
    /**
16
     * @var ImportScheduler
17
     */
18
    protected $scheduler;
19
20
    /**
21
     * @var ProcessorInterface
22
     */
23
    protected $processor;
24
25
    /**
26
     * @param ImportScheduler    $scheduler
27
     * @param ProcessorInterface $processor
28
     */
29
    public function __construct(ImportScheduler $scheduler, ProcessorInterface $processor)
30
    {
31
        $this->scheduler = $scheduler;
32
        $this->processor = $processor;
33
34
        parent::__construct();
35
    }
36
37
    /**
38
     * @inheritdoc
39
     */
40
    protected function configure()
41
    {
42
        $this->setName('io:import:reschedule');
43
        $this->addOption('import', 'i', InputOption::VALUE_OPTIONAL, 'A specific import you want to reschedule parts for');
44
        $this->setDescription('Reschedules eligible import parts');
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50
    protected function execute(InputInterface $input, OutputInterface $output)
51
    {
52
        if ($importId = $input->getOption('import')) {
53
            $output->writeln(sprintf('Rescheduling parts for import <info>%s</info>', $importId));
54
            $parts = $this->scheduler->findPartsByImportId($importId);
55
            $this->scheduleParts($parts, $output);
56
        } else {
57
            $output->writeln('Rescheduling parts that have started, but not finished');
58
            $parts = $this->scheduler->findUnfinishedParts();
59
            $this->scheduleParts($parts, $output);
60
61
            $output->writeln('Rescheduling parts that should have started, but didn\'t');
62
            $parts = $this->scheduler->findOverdueParts();
63
            $this->scheduleParts($parts, $output);
64
        }
65
    }
66
67
    /**
68
     * @param ImportPart[]    $parts
69
     * @param OutputInterface $output
70
     */
71
    protected function scheduleParts(array $parts, OutputInterface $output)
72
    {
73
        foreach ($parts as $part) {
74
            if ($this->processor->isRunning($part)) {
75
                continue;
76
            }
77
78
            $this->scheduler->schedulePart($part);
79
80
            $output->writeln(
81
                sprintf(
82
                    'Rescheduled part <comment>%d</comment> of <comment>%s</comment> import with id <comment>%s</comment>',
83
                    $part->getPosition(),
84
                    $part->getImport()->getFeed()->getOrigin()->getTitle(),
85
                    $part->getImport()->getId()
86
                )
87
            );
88
        }
89
    }
90
}
91