Passed
Push — delete-progressbar ( 50d654...4f5bf1 )
by Arnaud
10:42 queued 06:12
created

ConsoleLogger::printProgressBar()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 9
rs 10
1
<?php
2
/**
3
 * This file is part of the Cecil/Cecil package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cecil\Logger;
12
13
use Psr\Log\InvalidArgumentException;
14
use Psr\Log\LogLevel;
15
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
class ConsoleLogger extends PrintLogger
19
{
20
    const ERROR = 'error';
21
    const WARNING = 'comment';
22
    const NOTICE = 'info';
23
    const INFO = 'text';
24
    const DEBUG = 'debug';
25
26
    protected $output;
27
    protected $verbosityLevelMap = [
28
        LogLevel::EMERGENCY => OutputInterface::VERBOSITY_NORMAL,
29
        LogLevel::ALERT     => OutputInterface::VERBOSITY_NORMAL,
30
        LogLevel::CRITICAL  => OutputInterface::VERBOSITY_NORMAL,
31
        LogLevel::ERROR     => OutputInterface::VERBOSITY_NORMAL,
32
        LogLevel::WARNING   => OutputInterface::VERBOSITY_VERY_VERBOSE,
33
        LogLevel::NOTICE    => OutputInterface::VERBOSITY_VERBOSE,
34
        LogLevel::INFO      => OutputInterface::VERBOSITY_VERY_VERBOSE,
35
        LogLevel::DEBUG     => OutputInterface::VERBOSITY_DEBUG,
36
    ];
37
    protected $formatLevelMap = [
38
        LogLevel::EMERGENCY => self::ERROR,
39
        LogLevel::ALERT     => self::ERROR,
40
        LogLevel::CRITICAL  => self::ERROR,
41
        LogLevel::ERROR     => self::ERROR,
42
        LogLevel::WARNING   => self::WARNING,
43
        LogLevel::NOTICE    => self::NOTICE,
44
        LogLevel::INFO      => self::INFO,
45
        LogLevel::DEBUG     => self::DEBUG,
46
    ];
47
48
    public function __construct(OutputInterface $output, array $verbosityLevelMap = [], array $formatLevelMap = [])
49
    {
50
        $this->output = $output;
51
        $this->verbosityLevelMap = $verbosityLevelMap + $this->verbosityLevelMap;
52
        $this->formatLevelMap = $formatLevelMap + $this->formatLevelMap;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     *
58
     * @return void
59
     */
60
    public function log($level, $message, array $context = [])
61
    {
62
        $output = $this->output;
63
        $output->getFormatter()->setStyle('text', new OutputFormatterStyle('white'));
64
        $output->getFormatter()->setStyle('debug', new OutputFormatterStyle('blue', 'yellow'));
65
66
        if (!isset($this->verbosityLevelMap[$level])) {
67
            throw new InvalidArgumentException(sprintf('The log level "%s" does not exist.', $level));
68
        }
69
70
        // default pattern: <level>message</level>
71
        $pattern = '<%1$s>%2$s%3$s</%1$s>';
72
        $prefix = '';
73
74
        // steps prefix
75
        if (array_key_exists('step', $context)) {
76
            $prefix = sprintf('%s. ', $this->padPrefix($context['step'][0], $context['step'][1]));
77
        }
78
79
        // sub steps progress
80
        if (array_key_exists('progress', $context)) {
81
            // prefix
82
            $prefix = sprintf(
83
                '[%s/%s] ',
84
                $this->padPrefix($context['progress'][0], $context['progress'][1]),
85
                $context['progress'][1]
86
            );
87
        }
88
89
        $output->writeln(
90
            sprintf($pattern, $this->formatLevelMap[$level], $prefix, $this->interpolate($message, $context)),
91
            $this->verbosityLevelMap[$level]
92
        );
93
    }
94
95
    /**
96
     * Prefix padding.
97
     *
98
     * @param string $prefix
99
     * @param string $max
100
     *
101
     * @return string
102
     */
103
    private function padPrefix(string $prefix, string $max): string
104
    {
105
        return str_pad($prefix, strlen($max), ' ', STR_PAD_LEFT);
106
    }
107
}
108