Test Failed
Push — release/3.x ( 447600...4272fa )
by
unknown
02:22 queued 10s
created

Listener::onKernelException()   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 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Gerard van Helden <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
namespace Zicht\Bundle\UrlBundle\Logging;
7
8
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
9
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
10
use Symfony\Component\HttpKernel\HttpKernelInterface;
11
12
/**
13
 * Listener used for logging
14
 */
15
class Listener
16
{
17
    /**
18
     * @var \Zicht\Bundle\UrlBundle\Entity\ErrorLog
19
     */
20
    protected $log = null;
21
22
    /**
23
     * Construct with the passed logger service
24
     *
25
     * @param Logging $logging
26
     */
27
    public function __construct(Logging $logging)
28
    {
29
        $this->logging = $logging;
0 ignored issues
show
Bug Best Practice introduced by
The property logging does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
30
    }
31
32
33
    /**
34
     * Create log entry if a kernelexception occurs.
35
     *
36
     * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
37
     * @return void
38
     */
39
    public function onKernelException(GetResponseForExceptionEvent $event)
40
    {
41
        $this->log = $this->logging->createLog($event->getRequest(), $event->getException()->getMessage());
42
    }
43
44
    /**
45
     * Save the log entry (if any) if the error response about to be sent is not handled otherwise.
46
     *
47
     * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $e
48
     * @return void
49
     */
50
    public function onKernelResponse(FilterResponseEvent $e)
51
    {
52
        if (isset($this->log)) {
53
            if ($e->getRequestType() === HttpKernelInterface::MASTER_REQUEST
54
                && ($status = $e->getResponse()->getStatusCode()) >= 400
55
            ) {
56
                $this->log->setStatus($status);
57
                $this->logging->flush($this->log);
58
            }
59
        }
60
    }
61
}
62