Logger::warn()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Server\Loggers;
4
5
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
6
use Symfony\Component\Console\Output\OutputInterface;
7
8
class Logger
9
{
10
    /**
11
     * The console output interface.
12
     *
13
     * @var \Symfony\Component\Console\Output\OutputInterface
14
     */
15
    protected $consoleOutput;
16
17
    /**
18
     * Wether the logger is enabled.
19
     *
20
     * @var bool
21
     */
22
    protected $enabled = false;
23
24
    /**
25
     * Wether the verbose mode is on.
26
     *
27
     * @var bool
28
     */
29
    protected $verbose = false;
30
31
    /**
32
     * Check if the logger is active.
33
     *
34
     * @return bool
35
     */
36
    public static function isEnabled(): bool
37
    {
38
        $logger = app(WebSocketsLogger::class);
39
40
        return $logger->enabled;
41
    }
42
43
    /**
44
     * Create a new Logger instance.
45
     *
46
     * @param  \Symfony\Component\Console\Output\OutputInterface  $consoleOutput
47
     * @return void
48
     */
49
    public function __construct(OutputInterface $consoleOutput)
50
    {
51
        $this->consoleOutput = $consoleOutput;
52
    }
53
54
    /**
55
     * Enable the logger.
56
     *
57
     * @param  bool  $enabled
58
     * @return $this
59
     */
60
    public function enable($enabled = true)
61
    {
62
        $this->enabled = $enabled;
63
64
        return $this;
65
    }
66
67
    /**
68
     * Enable the verbose mode.
69
     *
70
     * @param  bool  $verbose
71
     * @return $this
72
     */
73
    public function verbose($verbose = false)
74
    {
75
        $this->verbose = $verbose;
76
77
        return $this;
78
    }
79
80
    /**
81
     * Trigger an Info message.
82
     *
83
     * @param  string  $message
84
     * @return void
85
     */
86
    protected function info(string $message)
87
    {
88
        $this->line($message, 'info');
89
    }
90
91
    /**
92
     * Trigger a Warning message.
93
     *
94
     * @param  string  $message
95
     * @return void
96
     */
97
    protected function warn(string $message)
98
    {
99
        if (! $this->consoleOutput->getFormatter()->hasStyle('warning')) {
100
            $style = new OutputFormatterStyle('yellow');
101
102
            $this->consoleOutput->getFormatter()->setStyle('warning', $style);
103
        }
104
105
        $this->line($message, 'warning');
106
    }
107
108
    /**
109
     * Trigger an Error message.
110
     *
111
     * @param  string  $message
112
     * @return void
113
     */
114
    protected function error(string $message)
115
    {
116
        $this->line($message, 'error');
117
    }
118
119
    protected function line(string $message, string $style)
120
    {
121
        $this->consoleOutput->writeln(
122
            $style ? "<{$style}>{$message}</{$style}>" : $message
123
        );
124
    }
125
}
126