MvcEventError   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 42
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getLogger() 0 4 1
A __invoke() 0 10 2
1
<?php
2
/**
3
 * Polder Knowledge / log-module (https://polderknowledge.com)
4
 *
5
 * @link https://github.com/polderknowledge/log-module for the canonical source repository
6
 * @copyright Copyright (c) 2016-2017 Polder Knowledge (https://polderknowledge.com)
7
 * @license https://github.com/polderknowledge/log-module/blob/master/LICENSE.md MIT
8
 */
9
10
namespace PolderKnowledge\LogModule\Listener;
11
12
use Psr\Log\LoggerInterface;
13
use Zend\Mvc\MvcEvent;
14
15
/**
16
 * This class acts as an event listener so that we can log errors that occur in zendframework/zend-mvc.
17
 */
18
final class MvcEventError
19
{
20
    /**
21
     * @var LoggerInterface
22
     */
23
    protected $logger;
24
25
    /**
26
     * @param LoggerInterface $logger
27
     */
28 24
    public function __construct(LoggerInterface $logger)
29
    {
30 24
        $this->logger = $logger;
31 24
    }
32
33
    /**
34
     * Gets the logger that is used.
35
     *
36
     * @return LoggerInterface
37
     */
38 3
    public function getLogger(): LoggerInterface
39
    {
40 3
        return $this->logger;
41
    }
42
43
    /**
44
     * Should be called when an MvcEvent is fired.
45
     *
46
     * @param MvcEvent $event The event that is fired.
47
     * @return void
48
     */
49 6
    public function __invoke(MvcEvent $event)
50
    {
51 6
        if ($event->getError() !== 'error-exception') {
52 3
            return;
53
        }
54
55 3
        $exception = $event->getParam('exception');
56
57 3
        $this->logger->error($exception->getMessage(), ['exception' => $exception]);
58 3
    }
59
}
60