Logger   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 3
cbo 3
dl 0
loc 63
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A isEnabled() 0 4 1
A __construct() 0 4 1
A enable() 0 6 1
A verbose() 0 6 1
A info() 0 4 1
A warn() 0 10 2
A error() 0 4 1
A line() 0 6 2
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Server\Logger;
4
5
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
6
use Symfony\Component\Console\Output\OutputInterface;
7
8
class Logger
9
{
10
    /** @var \Symfony\Component\Console\Output\OutputInterface */
11
    protected $consoleOutput;
12
13
    /** @var bool */
14
    protected $enabled = false;
15
16
    /** @var bool */
17
    protected $verbose = false;
18
19
    public static function isEnabled(): bool
20
    {
21
        return app(WebsocketsLogger::class)->enabled;
22
    }
23
24
    public function __construct(OutputInterface $consoleOutput)
25
    {
26
        $this->consoleOutput = $consoleOutput;
27
    }
28
29
    public function enable($enabled = true)
30
    {
31
        $this->enabled = $enabled;
32
33
        return $this;
34
    }
35
36
    public function verbose($verbose = false)
37
    {
38
        $this->verbose = $verbose;
39
40
        return $this;
41
    }
42
43
    protected function info(string $message)
44
    {
45
        $this->line($message, 'info');
46
    }
47
48
    protected function warn(string $message)
49
    {
50
        if (! $this->consoleOutput->getFormatter()->hasStyle('warning')) {
51
            $style = new OutputFormatterStyle('yellow');
52
53
            $this->consoleOutput->getFormatter()->setStyle('warning', $style);
54
        }
55
56
        $this->line($message, 'warning');
57
    }
58
59
    protected function error(string $message)
60
    {
61
        $this->line($message, 'error');
62
    }
63
64
    protected function line(string $message, string $style)
65
    {
66
        $styled = $style ? "<$style>$message</$style>" : $message;
67
68
        $this->consoleOutput->writeln($styled);
69
    }
70
}
71