Completed
Push — develop ( c28dd2...149d19 )
by Baptiste
04:27
created

LoggerListener   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%
Metric Value
wmc 3
lcom 0
cbo 4
dl 0
loc 45
ccs 17
cts 17
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSubscribedEvents() 0 6 1
A log() 0 18 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Neo4jBundle\EventListener;
5
6
use Innmind\Neo4j\DBAL\{
7
    Events,
8
    Event\PreQueryEvent,
9
    Query\Parameter
10
};
11
use Psr\Log\LoggerInterface;
12
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13
14
class LoggerListener implements EventSubscriberInterface
15
{
16
    private $logger;
17
18 1
    public function __construct(LoggerInterface $logger)
19
    {
20 1
        $this->logger = $logger;
21 1
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26 1
    public static function getSubscribedEvents()
27
    {
28
        return [
29 1
            Events::PRE_QUERY => 'log',
30
        ];
31
    }
32
33
    /**
34
     * Log the query about to be run
35
     *
36
     * @param PreQueryEvent $event
37
     *
38
     * @return void
39
     */
40 1
    public function log(PreQueryEvent $event)
41
    {
42 1
        $parameters = [];
43
        $event
44 1
            ->query()
45 1
            ->parameters()
46 1
            ->each(function(int $idx, Parameter $param) use (&$parameters) {
47 1
                $parameters[$param->key()] = $param->value();
48 1
            });
49
50 1
        $this->logger->info(
51 1
            'Cypher query about to be executed',
52
            [
53 1
                'cypher' => (string) $event->query(),
54 1
                'parameters' => $parameters,
55
            ]
56
        );
57 1
    }
58
}
59