TaskRunner::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
crap 1
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 null|LoggerInterface
40
     */
41
    protected $logger;
42
43
    /**
44
     * Create task runner object.
45
     *
46
     * @param TaskManager          $taskManager
47
     * @param OutputInterface      $output
48
     * @param null|LoggerInterface $logger
49
     */
50 4
    public function __construct(TaskManager $taskManager, OutputInterface $output, LoggerInterface $logger = null)
51
    {
52 4
        $this->taskManager = $taskManager;
53 4
        $this->output = $output;
54 4
        $this->logger = $logger;
55 4
    }
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 4
    public function __invoke($type, $buffer)
66
    {
67 4
        $this->logMessage($type, $buffer);
68 4
        $this->renderOutput($type, $buffer);
69 4
    }
70
71
    /**
72
     * Log message.
73
     *
74
     * @param string $type
75
     * @param string $message
76
     *
77
     * @return void
78
     */
79 4
    protected function logMessage($type, $message)
80
    {
81 4
        if (null === $this->logger) {
82 2
            return;
83
        }
84
85 2
        $logLevel = Logger::INFO;
86 2
        if (Process::ERR === $type) {
87 2
            $logLevel = Logger::ERROR;
88 1
        }
89
90 2
        $this->logger->log($logLevel, $message);
91 2
    }
92
93
    /**
94
     * Render output.
95
     *
96
     * @param string $type
97
     * @param string $message
98
     *
99
     * @return void
100
     */
101 4
    protected function renderOutput($type, $message)
102
    {
103 4
        if ($this->output->isQuiet()) {
104 2
            return;
105
        }
106
107 2
        if (Process::ERR === $type) {
108 2
            $message = sprintf('<error>%s</error>', $message);
109 1
        }
110
111 2
        $this->output->writeln($message);
112 2
    }
113
114
    /**
115
     * Run all tasks from the task manager.
116
     *
117
     * @param int $timeout
118
     *
119
     * @return void
120
     */
121 4
    public function runAll($timeout)
122
    {
123 4
        while (null !== $task = $this->taskManager->findNextTask()) {
124 4
            $this->run($task, $timeout);
125 2
        }
126 4
    }
127
128
    /**
129
     * Run single task.
130
     *
131
     * @param Task $task
132
     * @param int  $timeout
133
     *
134
     * @return void
135
     */
136 4
    public function run($task, $timeout)
137
    {
138 4
        $process = new Process($task->getEntity()->getCommandline());
139 4
        $process->setTimeout($timeout);
140 4
        $process->start($this);
141
142 4
        $task->defer($process->getPid(), function () use ($process) {
143 4
            $process->wait($this);
144
145 4
            return $process->getExitCode();
146 4
        });
147 4
    }
148
}
149