NightlyTaskCommand::runCommand()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Dekalee\NightlyTaskBundle\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\ArrayInput;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
/**
11
 * Class NightlyTaskCommand
12
 */
13
class NightlyTaskCommand extends AbstractNightlyTaskCommand
14
{
15
    /**
16
     * Configures the current command.
17
     */
18
    protected function configure()
19
    {
20
        $this->setName('dekalee:nightly:tasks')
21
            ->setDescription('Launch all the command that should run nightly');
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    protected function execute(InputInterface $input, OutputInterface $output)
28
    {
29
        $this->beforeExecute($input, $output);
30
        $this->fillTasks();
31
32
        foreach ($this->getTasks() as $priority => $taskPriority) {
33
            /** @var Command $task */
34
            foreach ($taskPriority as $task) {
35
                try {
36
                    $this->runCommand($task->getName());
37
                } catch (\Exception $e) {
38
                    if ($task instanceof NightlyCommandInterface && $task->isEssential()) {
39
                        throw $e;
40
                    }
41
                    $this->logger->error('Error during non essential nightly task', [
42
                        'command name' => $task->getName(),
43
                        'exception message' => $e->getMessage(),
44
                        'exception class' => get_class($e),
45
                    ]);
46
                }
47
            }
48
        }
49
    }
50
51
    /**
52
     * Run command.
53
     *
54
     * @param string $commandName
55
     * @param array  $arguments
56
     *
57
     * @throws \Exception
58
     */
59
    protected function runCommand($commandName, array $arguments = [])
60
    {
61
        $arguments['command'] = $commandName;
62
        $arguments['--env'] = $this->input->getOption('env');
63
64
        $input = new ArrayInput($arguments);
65
        $input->setInteractive(false);
66
        $command = $this->getApplication()->find($commandName);
67
        $command->run($input, $this->output);
68
    }
69
}
70