Completed
Push — master ( a33fba...4cd8af )
by Gabriel
05:33
created

RunCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
/**
3
 * Junty
4
 *
5
 * @author Gabriel Jacinto aka. GabrielJMJ <[email protected]>
6
 * @license MIT License
7
 */
8
 
9
namespace Junty\Console\Command;
10
11
use Junty\Runner\RunnerInterface;
12
use Junty\{Task};
13
use Symfony\Component\Console\Command\Command;
14
use Symfony\Component\Console\Input\{InputArgument, InputInterface};
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
class RunCommand extends Command
18
{
19
    private $runner;
20
21
    public function __construct(RunnerInterface $runner, $name = null)
22
    {
23
        parent::__construct($name);
24
25
        $this->runner = $runner;
26
    }
27
28
    protected function configure()
29
    {
30
        $this->setName('run')
31
            ->setDescription('Run tasks')
32
            ->addArgument(
33
                'task',
34
                InputArgument::OPTIONAL
35
            );
36
    }
37
38
    protected function execute(InputInterface $input, OutputInterface $output)
39
    {
40
        if ($input->hasArgument('task') && $task = $input->getArgument('task') !== null) {
41
            $output->writeln('Executing task: ' . $task = $input->getArgument('task'));
42
43
            $this->runner->runTask($task);
44
        } else {
45
            $tasks = $this->runner->getTasks();
46
            $output->writeln('Executing tasks');
47
            
48
            foreach ($tasks as $task) {
49
                try {
50
                    $output->writeln('Executing task \'' . $task->getName() . '\'');
51
                    $this->runner->runTask($task);
52
                } catch (\Exception $e) {
53
                    $output->writeln('Error on task \'' . $task->getName() . '\': ' . $e->getMessage());
54
                }
55
            }
56
        }
57
58
        $time = round((microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']) * 100) / 100;
59
        $output->writeln('Finished! Time: ' . $time . 'ms');
60
    }
61
}