Completed
Push — master ( f0448f...4201d1 )
by Julien
14s
created

Log::setLevel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace TheAentMachine\AentDockerCompose\Aenthill;
3
4
use Symfony\Component\Console\Output\OutputInterface;
5
use TheAentMachine\AentDockerCompose\Aenthill\Exception\LogLevelException;
6
7
class Log
8
{
9
    /** @var array */
10
    private $levels = [
11
        "DEBUG" => OutputInterface::VERBOSITY_DEBUG,
12
        "INFO" => OutputInterface::VERBOSITY_VERY_VERBOSE,
13
        "WARN" => OutputInterface::VERBOSITY_VERBOSE,
14
        "ERROR" => OutputInterface::VERBOSITY_NORMAL,
15
    ];
16
17
    /** @var OutputInterface */
18
    private $output;
19
20
    /**
21
     * Log constructor.
22
     * @param OutputInterface $output
23
     */
24
    public function __construct(OutputInterface $output)
25
    {
26
        $this->output = $output;
27
        $output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE);
28
    }
29
30
    /**
31
     * @param string $logLevel
32
     * @throws LogLevelException
33
     * @return void
34
     */
35
    public function setLevel(string $logLevel): void
36
    {
37
        if (!array_key_exists($logLevel, $this->levels)) {
38
            throw new LogLevelException($logLevel);
39
        }
40
41
        $this->output->setVerbosity($this->levels[$logLevel]);
42
    }
43
44
    /**
45
     * @param string $message
46
     * @return void
47
     */
48
    public function debugln(string $message): void
49
    {
50
        $this->output->writeln($message, OutputInterface::VERBOSITY_DEBUG);
51
    }
52
53
    /**
54
     * @param string $message
55
     * @return void
56
     */
57
    public function infoln(string $message): void
58
    {
59
        $this->output->writeln("<info>$message</info>", OutputInterface::VERBOSITY_VERY_VERBOSE);
60
    }
61
62
    /**
63
     * @param string $message
64
     * @return void
65
     */
66
    public function warnln(string $message): void
67
    {
68
        $this->output->writeln("<comment>$message</comment>", OutputInterface::VERBOSITY_VERBOSE);
69
    }
70
71
    /**
72
     * @param string $message
73
     * @return void
74
     */
75
    public function errorln(string $message): void
76
    {
77
        $this->output->writeln("<error>$message</error>", OutputInterface::VERBOSITY_NORMAL);
78
    }
79
}
80