OutputtingListener::__invoke()   B
last analyzed

Complexity

Conditions 10
Paths 10

Size

Total Lines 21
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 18
nc 10
nop 1
dl 0
loc 21
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This file is part of byrokrat\giroapp.
5
 *
6
 * byrokrat\giroapp is free software: you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License as published
8
 * by the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * byrokrat\giroapp is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with byrokrat\giroapp. If not, see <http://www.gnu.org/licenses/>.
18
 *
19
 * Copyright 2016-21 Hannes Forsgård
20
 */
21
22
declare(strict_types=1);
23
24
namespace byrokrat\giroapp\Event\Listener;
25
26
use byrokrat\giroapp\Event\LogEvent;
27
use Psr\Log\LogLevel;
28
use Symfony\Component\Console\Output\OutputInterface;
29
30
final class OutputtingListener implements ListenerInterface
31
{
32
    /**
33
     * @var OutputInterface
34
     */
35
    private $output;
36
37
    public function __construct(OutputInterface $output)
38
    {
39
        $this->output = $output;
40
    }
41
42
    public function __invoke(LogEvent $event): void
43
    {
44
        switch ($event->getSeverity()) {
45
            case LogLevel::EMERGENCY:
46
            case LogLevel::ALERT:
47
            case LogLevel::CRITICAL:
48
            case LogLevel::ERROR:
49
                $this->output->writeln("<error>ERROR: {$event->getMessage()}</error>");
50
                break;
51
            case LogLevel::WARNING:
52
                $this->output->writeln("<question>WARNING: {$event->getMessage()}</question>");
53
                break;
54
            case LogLevel::NOTICE:
55
            case LogLevel::INFO:
56
                $this->output->writeln($event->getMessage());
57
                break;
58
            case LogLevel::DEBUG:
59
                if ($this->output->isVerbose()) {
60
                    $this->output->writeln($event->getMessage());
61
                }
62
                break;
63
        }
64
    }
65
}
66