Passed
Push — master ( 6d2ec0...d97bf6 )
by Sebastiaan
02:45
created

Run::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Basebuilder\Scheduling\Command;
4
5
use Basebuilder\Scheduling\Event\Process;
6
use Basebuilder\Scheduling\Schedule;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
/**
12
 * This command runs all scheduled tasks that are due for execution, replacing the need for multiple CRON jobs
13
 */
14
class Run extends Command
15
{
16
    /**
17
     * @var Schedule
18
     */
19
    protected $schedule;
20
21
    protected $minimumVerbosity = OutputInterface::VERBOSITY_VERBOSE;
22
23
    /**
24
     * @param Schedule    $schedule
25
     * @param string|null $name
26
     */
27
    public function __construct(Schedule $schedule, $name = null)
28
    {
29
        $this->schedule = $schedule;
30
        parent::__construct($name);
31
    }
32
33
    /**
34
     * @inheritdoc
35
     */
36
    protected function configure()
37
    {
38
        $this->setName('scheduler:run');
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44
    protected function execute(InputInterface $input, OutputInterface $output)
45
    {
46
        $schedule = $this->schedule;
47
        $events   = $schedule->dueEvents();
48
49
        if ($output->getVerbosity() >= $this->minimumVerbosity) {
50
            $output->writeln(
51
                '======================================================' . PHP_EOL .
52
                '# Running schedule for <info>' . $schedule->getName() . '</info>' . PHP_EOL .
53
                '======================================================'
54
            );
55
        }
56
57
        foreach ($events as $event) {
58
            if ($output->getVerbosity() >= $this->minimumVerbosity) {
59
                $output->writeln('Running event <info>"' . (string)$event . '"</info>');
60
            }
61
62
            $result = $event->run();
63
            if ($event instanceof Process)  {
64
                $this->handleProcessOutput($result, $output);
65
            }
66
        }
67
    }
68
69
    protected function handleProcessOutput(\Symfony\Component\Process\Process $process, OutputInterface $output)
70
    {
71
        if ($process->isTerminated() && $output->getVerbosity() >= $this->minimumVerbosity) {
72
            $tag = $process->isSuccessful() ? 'info' : 'error';
73
            $output->writeln('Exit code: <' . $tag . '>' . $process->getExitCode() . '</' . $tag . '>');
74
        }
75
    }
76
}
77