Passed
Push — master ( 4c3962...96d1ab )
by Théo
03:03
created

CompilerLogger::log()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 26
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 26
rs 8.6346
c 0
b 0
f 0
cc 7
nc 7
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box\Console\Logger;
16
17
use InvalidArgumentException;
18
use KevinGH\Box\Console\IO\IO;
19
use function sprintf;
20
use Symfony\Component\Console\Output\OutputInterface;
21
22
/**
23
 * @internal
24
 */
25
final class CompilerLogger
26
{
27
    public const QUESTION_MARK_PREFIX = '?';
28
    public const STAR_PREFIX = '*';
29
    public const PLUS_PREFIX = '+';
30
    public const MINUS_PREFIX = '-';
31
    public const CHEVRON_PREFIX = '>';
32
33
    private $io;
34
35
    public function __construct(IO $io)
36
    {
37
        $this->io = $io;
38
    }
39
40
    public function getIO(): IO
41
    {
42
        return $this->io;
43
    }
44
45
    public function log(string $prefix, string $message, int $verbosity = OutputInterface::OUTPUT_NORMAL): void
46
    {
47
        switch ($prefix) {
48
            case '!':
49
                $prefix = "<error>$prefix</error>";
50
                break;
51
            case self::STAR_PREFIX:
52
                $prefix = "<info>$prefix</info>";
53
                break;
54
            case self::QUESTION_MARK_PREFIX:
55
                $prefix = "<comment>$prefix</comment>";
56
                break;
57
            case self::PLUS_PREFIX:
58
            case self::MINUS_PREFIX:
59
                $prefix = "  <comment>$prefix</comment>";
60
                break;
61
            case self::CHEVRON_PREFIX:
62
                $prefix = "    <comment>$prefix</comment>";
63
                break;
64
            default:
65
                throw new InvalidArgumentException('Expected one of the logger constant as a prefix.');
66
        }
67
68
        $this->io->writeln(
69
            "$prefix $message",
70
            $verbosity
71
        );
72
    }
73
74
    public function logStartBuilding(string $path): void
75
    {
76
        $this->io->writeln(
77
            sprintf(
78
                '🔨  Building the PHAR "<comment>%s</comment>"',
79
                $path
80
            )
81
        );
82
        $this->io->newLine();
83
    }
84
}
85