Failed Conditions
Pull Request — master (#1861)
by k-yamamura
501:04 queued 494:33
created

EccubeMonologListener::onKernelController()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Eccube\Monolog\Listener;
4
5
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
6
use Symfony\Component\HttpFoundation\Request;
7
use Symfony\Component\HttpFoundation\Response;
8
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
9
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
10
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
11
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
12
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
13
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
14
use Symfony\Component\HttpKernel\KernelEvents;
15
16
/**
17
 * Log request,controller,terminate and exceptions.
18
 */
19
class EccubeMonologListener implements EventSubscriberInterface
20
{
21
22
    /**
23
     * Logs master requests on event KernelEvents::Request.
24
     *
25
     * @param FilterControllerEvent $event
26
     */
27 462
    public function onKernelRequest(GetResponseEvent $event)
28
    {
29 462
        if (!$event->isMasterRequest()) {
30 72
            return;
31
        }
32
33 462
        $this->logRequest($event->getRequest());
34 462
    }
35
36
    /**
37
     * Logs master requests on event KernelEvents::CONTROLLER.
38
     *
39
     * @param FilterControllerEvent $event
40
     */
41 459
    public function onKernelController(FilterControllerEvent $event)
42
    {
43 459
        if (!$event->isMasterRequest()) {
44 72
            return;
45
        }
46
47 457
        $this->logController($event->getRequest());
48 457
    }
49
50 450
    public function onKernelResponse(FilterResponseEvent $event)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
51
    {
52 450
        if (!$event->isMasterRequest()) {
53 72
            return;
54
        }
55
56 450
        $this->logResponse($event->getRequest(), $event->getResponse());
57 450
    }
58
59
    /**
60
     * Logs terminate on event KernelEvents::TERMINATE.
61
     *
62
     * @param PostResponseEvent $event
63
     */
64 447
    public function onKernelTerminate(PostResponseEvent $event)
65
    {
66 447
        $this->logTerminate($event->getRequest(), $event->getResponse());
67 447
    }
68
69
    /**
70
     * Logs uncaught exceptions on event KernelEvents::EXCEPTION.
71
     *
72
     * @param GetResponseForExceptionEvent $event
73
     */
74 17
    public function onKernelException(GetResponseForExceptionEvent $event)
75
    {
76 17
        $this->logException($event->getException());
77 17
    }
78
79
    /**
80
     * Logs a request
81
     *
82
     * @param Request $request
83
     */
84 462
    protected function logRequest(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

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

Loading history...
85
    {
86
        // このイベントのタイミングでは、ルーティングは確定していない.
87 462
        \EccubeLog::info('PRCESS START');
88 462
    }
89
90
    /**
91
     * Logs a controller
92
     *
93
     * @param Request $request
94
     */
95 457
    protected function logController(Request $request)
96
    {
97 457
        $route = $request->attributes->get('_route');
98 457
        \EccubeLog::info('LOGIC START', array($route));
99 457
    }
100
101
    /**
102
     * Logs a response.
103
     *
104
     * @param Response $response
105
     */
106 450
    protected function logResponse(Request $request, Response $response)
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed.

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

Loading history...
107
    {
108 450
        $route = $request->attributes->get('_route');
109 450
        \EccubeLog::info('LOGIC END', array($route));
110 450
    }
111
112 447
    protected function logTerminate(Request $request, Response $response)
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed.

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

Loading history...
113
    {
114 447
        $route = $request->attributes->get('_route');
115 447
        \EccubeLog::info('PRCESS END', array($route));
116 447
    }
117
118
    /**
119
     * Logs an exception.
120
     *
121
     * @param \Exception $e
122
     */
123 17
    protected function logException(\Exception $e)
124
    {
125 17
        if ($e instanceof HttpExceptionInterface && $e->getStatusCode() < 500) {
126 13
            \EccubeLog::info($e->getMessage(), array($e->getStatusCode()));
127
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
128 13
        } else {
129 4
            $message = sprintf(
130 4
                '%s: %s (uncaught exception) at %s line %s',
131 4
                get_class($e),
132 4
                $e->getMessage(),
133 4
                $e->getFile(),
134 4
                $e->getLine()
135 4
            );
136 4
            \EccubeLog::error($message, array('exception' => $e));
137
        }
138 17
    }
139
140 1062
    public static function getSubscribedEvents()
0 ignored issues
show
introduced by
Declare public methods first, then protected ones and finally private ones
Loading history...
introduced by
Missing function doc comment
Loading history...
141
    {
142
        return array(
143 1062
            KernelEvents::REQUEST => array('onKernelRequest', 500),
144 1062
            KernelEvents::RESPONSE => array('onKernelResponse', 0),
145 1062
            KernelEvents::CONTROLLER => array('onKernelController', 0),
146 1062
            KernelEvents::TERMINATE => array('onKernelTerminate', 0),
147
            /*
148
             * Priority -4 is used to come after those from SecurityServiceProvider (0)
149
             * but before the error handlers added with Silex\Application::error (defaults to -8)
150
             */
151 1062
            KernelEvents::EXCEPTION => array('onKernelException', -4),
152 1062
        );
153
    }
154
}
155