Completed
Push — master ( 7a08de...9ce2ea )
by Daniel
04:39
created

TaskRunner::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

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 12
ccs 0
cts 8
cp 0
rs 9.4285
cc 1
eloc 7
nc 1
nop 2
crap 2
1
<?php
2
/**
3
 * This file is part of the Commander project.
4
 *
5
 * @author Daniel Schröder <[email protected]>
6
 */
7
8
namespace GravityMedia\Commander\Commander;
9
10
use Monolog\Logger;
11
use Psr\Log\LoggerInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Process\Process;
14
15
/**
16
 * Task runner class.
17
 *
18
 * @package GravityMedia\Commander\Commander
19
 */
20
class TaskRunner
21
{
22
    /**
23
     * The task manager.
24
     *
25
     * @var TaskManager
26
     */
27
    protected $taskManager;
28
29
    /**
30
     * The output.
31
     *
32
     * @var OutputInterface
33
     */
34
    protected $output;
35
36
    /**
37
     * The logger.
38
     *
39
     * @var LoggerInterface
40
     */
41
    protected $logger;
42
43
    /**
44
     * Create task runner object.
45
     *
46
     * @param TaskManager     $taskManager
47
     * @param OutputInterface $output
48
     * @param LoggerInterface $logger
49
     */
50
    public function __construct(TaskManager $taskManager, OutputInterface $output, LoggerInterface $logger)
51
    {
52
        $this->taskManager = $taskManager;
53
        $this->output = $output;
54
        $this->logger = $logger;
55
    }
56
57
    /**
58
     * Gets called when the object is used as a callback.
59
     *
60
     * @param string $type
61
     * @param string $buffer
62
     *
63
     * @return void
64
     */
65
    public function __invoke($type, $buffer)
66
    {
67
        $this->logMessage($type, $buffer);
68
        $this->renderOutput($type, $buffer);
69
    }
70
71
    /**
72
     * Log message.
73
     *
74
     * @param string $type
75
     * @param string $message
76
     *
77
     * @return void
78
     */
79
    protected function logMessage($type, $message)
80
    {
81
        $logLevel = Logger::INFO;
82
        if (Process::ERR === $type) {
83
            $logLevel = Logger::ERROR;
84
        }
85
86
        $this->logger->log($logLevel, $message);
87
    }
88
89
    /**
90
     * Render output.
91
     *
92
     * @param string $type
93
     * @param string $message
94
     *
95
     * @return void
96
     */
97
    protected function renderOutput($type, $message)
98
    {
99
        if ($this->output->isQuiet()) {
100
            return;
101
        }
102
103
        if (Process::ERR === $type) {
104
            $message = sprintf('<error>%s</error>', $message);
105
        }
106
107
        $this->output->writeln($message);
108
    }
109
110
    /**
111
     * Run all tasks from the task manager.
112
     *
113
     * @param int $timeout
114
     *
115
     * @return void
116
     */
117
    public function runAll($timeout)
118
    {
119
        while (null !== $task = $this->taskManager->findNextTask()) {
120
            $this->run($task, $timeout);
121
        }
122
    }
123
124
    /**
125
     * Run single task.
126
     *
127
     * @param Task $task
128
     * @param int  $timeout
129
     *
130
     * @return void
131
     */
132
    public function run($task, $timeout)
133
    {
134
        $process = $this->createProcess($task->getEntity()->getCommandline());
135
        $process->setTimeout($timeout);
136
        $process->start();
137
138
        $task->begin($process->getPid());
139
140
        $process->wait($this);
141
142
        $task->finish($process->getExitCode());
143
    }
144
145
    /**
146
     * Create process.
147
     *
148
     * @param string $commandline
149
     *
150
     * @return Process
151
     */
152
    protected function createProcess($commandline)
153
    {
154
        return new Process($commandline);
155
    }
156
}
157