ProcessOutputPrinter   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 87
ccs 27
cts 30
cp 0.9
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A callback() 0 8 2
A command() 0 8 2
B writeln() 0 29 6
A filterOutput() 0 4 1
1
<?php
2
/* (c) Anton Medvedev <[email protected]>
3
 *
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace Deployer\Utility;
9
10
use Deployer\Logger\Logger;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Process\Process;
13
14
class ProcessOutputPrinter
15
{
16
    /**
17
     * @var OutputInterface
18
     */
19
    private $output;
20
21
    /**
22
     * @var Logger
23
     */
24
    private $logger;
25
26 10
    public function __construct(OutputInterface $output, Logger $logger)
27
    {
28 10
        $this->output = $output;
29 10
        $this->logger = $logger;
30 10
    }
31
32
    /**
33
     * Returns a callable for use with the symfony Process->run($callable) method.
34
     *
35
     * @return callable A function expecting a int $type (e.g. Process::OUT or Process::ERR) and string $buffer parameters.
36
     */
37
    public function callback(string $hostname)
38
    {
39 10
        return function ($type, $buffer) use ($hostname) {
40 10
            foreach (explode("\n", rtrim($buffer)) as $line) {
41 10
                $this->writeln($type, $hostname, $line);
42
            }
43 10
        };
44
    }
45
46 10
    public function command(string $hostname, string $command)
47
    {
48 10
        $this->logger->log("[$hostname] > $command");
49
50 10
        if ($this->output->isVeryVerbose()) {
51 1
            $this->output->writeln("[$hostname] <fg=cyan>></fg=cyan> $command");
52
        }
53 10
    }
54
55
    /**
56
     * @param int $type Process::OUT or Process::ERR
57
     * @param string $hostname for debugging
58
     * @param string $line to print
59
     */
60 10
    public function writeln($type, $hostname, $line)
61
    {
62 10
        $line = $this->filterOutput($line);
63
64
        // Omit empty lines
65 10
        if (empty($line)) {
66 3
            return;
67
        }
68
69 10
        if ($type === Process::ERR) {
70 6
            $this->logger->log("[$hostname] < [error] $line");
71
        } else {
72 10
            $this->logger->log("[$hostname] < $line");
73
        }
74
75 10
        if ($this->output->isDecorated()) {
76
            if ($type === Process::ERR) {
77
                $line = "[$hostname] \033[0;31m<\e[0m $line";
78
            } else {
79
                $line = "[$hostname] \033[0;90m< $line\033[0m";
80
            }
81
        } else {
82 10
            $line = "[$hostname] < $line";
83
        }
84
85 10
        if ($this->output->isDebug()) {
86 1
            $this->output->writeln($line, OutputInterface::OUTPUT_RAW);
87
        }
88 10
    }
89
90
    /**
91
     * This filtering used only in Ssh\Client, but for simplify putted here.
92
     *
93
     * @param string $output
94
     * @return string
95
     */
96 10
    public function filterOutput($output)
97
    {
98 10
        return preg_replace('/\[exit_code:(.*?)\]/', '', $output);
99
    }
100
}
101