Completed
Push — master ( 282f78...6dce89 )
by Sam
05:20
created

EchoLogger::log()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of PHP DNS Server.
4
 *
5
 * (c) Yif Swery <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace yswery\DNS;
12
13
use Psr\Log\AbstractLogger;
14
use Psr\Log\LogLevel;
15
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16
use yswery\DNS\Event\Events;
17
use yswery\DNS\Event\ServerExceptionEvent;
18
use yswery\DNS\Event\QueryReceiveEvent;
19
use yswery\DNS\Event\QueryResponseEvent;
20
use yswery\DNS\Event\ServerStartEvent;
21
22
class EchoLogger extends AbstractLogger implements EventSubscriberInterface
23
{
24
    public static function getSubscribedEvents(): array
25
    {
26
        return [
27
            Events::SERVER_START => 'onServerStart',
28
            Events::SERVER_EXCEPTION => 'onException',
29
            Events::QUERY_RECEIVE => 'onQueryReceive',
30
            Events::QUERY_RESPONSE => 'onQueryResponse',
31
        ];
32
    }
33
34
    public function onServerStart(ServerStartEvent $event): void
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

34
    public function onServerStart(/** @scrutinizer ignore-unused */ ServerStartEvent $event): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
35
    {
36
        $this->log(LogLevel::INFO, 'Server started.');
37
    }
38
39
    public function onException(ServerExceptionEvent $event): void
40
    {
41
        $this->log(LogLevel::ERROR, $event->getException()->getMessage());
42
    }
43
44
    public function onQueryReceive(QueryReceiveEvent $event): void
45
    {
46
        foreach ($event->getMessage()->getQuestions() as $question) {
47
            $this->log(LogLevel::INFO, 'Query: '.$question);
48
        }
49
    }
50
51
    public function onQueryResponse(QueryResponseEvent $event): void
52
    {
53
        foreach ($event->getMessage()->getAnswers() as $answer) {
54
            $this->log(LogLevel::INFO, 'Answer: '.$answer);
55
        }
56
    }
57
58
    public function log($level, $message, array $context = [])
59
    {
60
        echo sprintf('[%s] %s: %s'.PHP_EOL, date('c'), $level, $message);
61
    }
62
}
63