Completed
Push — master ( 38df09...7a08de )
by Daniel
08:33
created

OutputCallback   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 53
ccs 0
cts 17
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __invoke() 0 18 4
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\Process;
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
 * Output callback class
17
 *
18
 * @package GravityMedia\Commander\Process
19
 */
20
class OutputCallback
21
{
22
    /**
23
     * The output.
24
     *
25
     * @var OutputInterface
26
     */
27
    protected $output;
28
29
    /**
30
     * The logger.
31
     *
32
     * @var LoggerInterface
33
     */
34
    protected $logger;
35
36
    /**
37
     * Create output callback object.
38
     *
39
     * @param OutputInterface $output
40
     * @param LoggerInterface $logger
41
     */
42
    public function __construct(OutputInterface $output, LoggerInterface $logger)
43
    {
44
        $this->output = $output;
45
        $this->logger = $logger;
46
    }
47
48
    /**
49
     * Gets called when the object is used as a callback.
50
     *
51
     * @param string $type
52
     * @param string $buffer
53
     */
54
    public function __invoke($type, $buffer)
55
    {
56
        $logLevel = Logger::INFO;
57
        if (Process::ERR === $type) {
58
            $logLevel = Logger::ERROR;
59
        }
60
61
        $this->logger->log($logLevel, $buffer);
62
63
        if (OutputInterface::VERBOSITY_QUIET === $this->output->getVerbosity()) {
64
            return;
65
        }
66
67
        if (Process::ERR === $type) {
68
            $buffer = sprintf('<error>%s</error>', $buffer);
69
        }
70
        $this->output->writeln($buffer);
71
    }
72
}
73