Passed
Pull Request — master (#259)
by Théo
02:56
created

CompileLogger::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
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 Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Console\Style\SymfonyStyle;
20
21
/**
22
 * @internal
23
 */
24
final class CompileLogger
25
{
26
    public const QUESTION_MARK_PREFIX = '?';
27
    public const STAR_PREFIX = '*';
28
    public const PLUS_PREFIX = '+';
29
    public const MINUS_PREFIX = '-';
30
    public const CHEVRON_PREFIX = '>';
31
32
    private $io;
33
34
    public function __construct(SymfonyStyle $io)
35
    {
36
        $this->io = $io;
37
    }
38
39
    public function log(string $prefix, string $message, int $verbosity = OutputInterface::OUTPUT_NORMAL): void
40
    {
41
        switch ($prefix) {
42
            case '!':
43
                $prefix = "<error>$prefix</error>";
44
                break;
45
            case self::STAR_PREFIX:
46
                $prefix = "<info>$prefix</info>";
47
                break;
48
            case self::QUESTION_MARK_PREFIX:
49
                $prefix = "<comment>$prefix</comment>";
50
                break;
51
            case self::PLUS_PREFIX:
52
            case self::MINUS_PREFIX:
53
                $prefix = "  <comment>$prefix</comment>";
54
                break;
55
            case self::CHEVRON_PREFIX:
56
                $prefix = "    <comment>$prefix</comment>";
57
                break;
58
            default:
59
                throw new InvalidArgumentException('Expected one of the logger constant as a prefix.');
60
        }
61
62
        $this->io->writeln(
63
            "$prefix $message",
64
            $verbosity
65
        );
66
    }
67
68
    public function logStartBuilding(string $path): void
69
    {
70
        $message = sprintf(
71
            'Building the PHAR "<comment>%s</comment>"',
72
            $path
73
        );
74
75
        if ($this->io->isVerbose()) {
76
            $this->log(
77
                self::STAR_PREFIX,
78
                $message
79
            );
80
        } else {
81
            $this->io->writeln($message);
82
        }
83
    }
84
}
85