LogListener::log()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 7
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
/*
4
 * This file is part of the FOSHttpCache package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\HttpCache\EventListener;
13
14
use FOS\HttpCache\Event;
15
use FOS\HttpCache\Events;
16
use Psr\Log\LoggerInterface;
17
use Psr\Log\LogLevel;
18
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
19
20
/**
21
 * Log when the caching proxy client can't  send requests to the caching server.
22
 */
23
class LogListener implements EventSubscriberInterface
24
{
25
    /**
26
     * @var LoggerInterface
27
     */
28
    private $logger;
29
30 1
    public function __construct(LoggerInterface $logger)
31
    {
32 1
        $this->logger = $logger;
33 1
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 1
    public static function getSubscribedEvents(): array
39
    {
40
        return [
41 1
            Events::PROXY_UNREACHABLE_ERROR => 'onProxyUnreachableError',
42 1
            Events::PROXY_RESPONSE_ERROR => 'onProxyResponseError',
43
        ];
44
    }
45
46 1
    public function onProxyUnreachableError(Event $event)
47
    {
48 1
        $this->log(LogLevel::CRITICAL, $event->getException());
49 1
    }
50
51 1
    public function onProxyResponseError(Event $event)
52
    {
53 1
        $this->log(LogLevel::CRITICAL, $event->getException());
54 1
    }
55
56 1
    private function log($level, \Exception $exception)
57
    {
58
        $context = [
59 1
            'exception' => $exception,
60
        ];
61
62 1
        $this->logger->log($level, $exception->getMessage(), $context);
63 1
    }
64
}
65