ImportRunCommand::checkForUnfinishedImports()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 21

Duplication

Lines 21
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 21
loc 21
ccs 0
cts 14
cp 0
rs 9.584
c 0
b 0
f 0
cc 4
nc 4
nop 3
crap 20
1
<?php
2
3
namespace TreeHouse\IoBundle\Command;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use PK\CommandExtraBundle\Command\Command;
7
use Psr\Log\LoggerInterface;
8
use Symfony\Component\Console\Helper\QuestionHelper;
9
use Symfony\Component\Console\Input\ArrayInput;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Input\InputOption;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Symfony\Component\Console\Question\ConfirmationQuestion;
15
use TreeHouse\IoBundle\Entity\Feed;
16
use TreeHouse\IoBundle\Entity\FeedRepository;
17
use TreeHouse\IoBundle\EventListener\ImportOutputSubscriber;
18
use TreeHouse\IoBundle\Import\ImportFactory;
19
20
class ImportRunCommand extends Command
21
{
22
    /**
23
     * @var ManagerRegistry
24
     */
25
    protected $doctrine;
26
27
    /**
28
     * @var ImportFactory
29
     */
30
    protected $importFactory;
31
32
    /**
33
     * @var LoggerInterface
34
     */
35
    protected $logger;
36
37
    /**
38
     * @param ManagerRegistry $doctrine
39
     * @param ImportFactory   $importFactory
40
     * @param LoggerInterface $logger
41
     */
42 View Code Duplication
    public function __construct(ManagerRegistry $doctrine, ImportFactory $importFactory, LoggerInterface $logger)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
43
    {
44
        $this->doctrine = $doctrine;
45
        $this->importFactory = $importFactory;
46
        $this->logger = $logger;
47
48
        parent::__construct();
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54
    protected function configure()
55
    {
56
        $this->setName('io:import:run');
57
        $this->addArgument('id', InputArgument::IS_ARRAY, 'Specify which feeds to import, defaults to all');
58
        $this->addOption('force', 'f', InputOption::VALUE_NONE, 'Force import of all items, skipping modification date checks');
59
        $this->setDescription('Runs a new import');
60
        $this->isSingleProcessed();
61
        $this->setSummarizeDefinition(['time' => true, 'memory' => true]);
62
    }
63
64
    /**
65
     * @inheritdoc
66
     */
67
    protected function execute(InputInterface $input, OutputInterface $output)
68
    {
69
        $ids = (array) $input->getArgument('id');
70
71
        if (empty($ids)) {
72
            $feeds = $this->getFeedRepository()->findAll();
73
        } else {
74
            $feeds = $this->getFeedRepository()->findBy(['id' => $ids]);
75
        }
76
77
        $dispatcher = $this->importFactory->getEventDispatcher();
78
        $dispatcher->addSubscriber(new ImportOutputSubscriber($output));
79
80
        $force = $input->getOption('force');
81
82
        if (empty($feeds)) {
83
            $output->writeln('No feeds to import');
84
85
            return 1;
86
        }
87
88
        foreach ($feeds as $feed) {
89
            if ($input->isInteractive()) {
90
                $this->checkForUnfinishedImports($feed, $input, $output);
91
            }
92
93
            $this->runImport($output, $feed, $force);
94
        }
95
96
        return 0;
97
    }
98
99
    /**
100
     * @param OutputInterface $output
101
     * @param Feed            $feed
102
     * @param bool            $force
103
     */
104
    protected function runImport(OutputInterface $output, Feed $feed, $force = false)
105
    {
106
        $output->writeln(
107
            sprintf(
108
                'Starting a new import for <info>%s</info> feed <info>%d</info>',
109
                $feed->getOrigin()->getName(),
110
                $feed->getId()
111
            )
112
        );
113
114
        $import = $this->importFactory->createImport($feed, new \DateTime(), $force);
115
        $output->writeln(sprintf('Created import <info>%d</info>', $import->getId()));
116
117
        foreach ($import->getParts() as $part) {
118
            $output->writeln(sprintf('Importing part <comment>%d</comment>', $part->getPosition()));
119
120
            $job = $this->importFactory->createImportJob($part);
121
            $job->setLogger($this->logger);
122
            $job->run();
123
        }
124
    }
125
126
    /**
127
     * Checks feed for unfinished imports and gives the user an option to close them first.
128
     *
129
     * @param Feed            $feed
130
     * @param InputInterface  $input
131
     * @param OutputInterface $output
132
     *
133
     * @throws \Exception
134
     */
135 View Code Duplication
    protected function checkForUnfinishedImports(Feed $feed, InputInterface $input, OutputInterface $output)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
136
    {
137
        $helper = new QuestionHelper();
138
139
        foreach ($feed->getImports() as $import) {
140
            if (!$import->isFinished()) {
141
                $msg = sprintf(
142
                    'Import <info>%d</info> for this feed is unfinished, close it now? [y] ',
143
                    $import->getId()
144
                );
145
146
                $question = new ConfirmationQuestion($msg);
147
                if ($helper->ask($input, $output, $question)) {
148
                    $command = 'io:import:close';
149
                    $closeImport = $this->getApplication()->find($command);
150
                    $input = new ArrayInput(['command' => $command, 'import' => $import->getId()]);
151
                    $closeImport->run($input, $output);
152
                }
153
            }
154
        }
155
    }
156
157
    /**
158
     * @return FeedRepository
159
     */
160
    protected function getFeedRepository()
161
    {
162
        return $this->doctrine->getRepository('TreeHouseIoBundle:Feed');
163
    }
164
}
165