Completed
Push — 4.0 ( 268f2c...88f012 )
by Hideki
05:48 queued 10s
created

src/Eccube/EventListener/LogListener.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.ec-cube.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\EventListener;
15
16
use Eccube\Log\Logger;
17
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
18
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
19
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
20
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
21
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
22
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
23
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
24
use Symfony\Component\HttpKernel\KernelEvents;
25
26
/**
27
 * ログ出力リスナー
28
 */
29
class LogListener implements EventSubscriberInterface
30
{
31
    /**
32
     * @var Logger
33
     */
34
    protected $logger;
35
36 436
    public function __construct(Logger $logger)
37
    {
38 436
        $this->logger = $logger;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 1
    public static function getSubscribedEvents()
45
    {
46
        return [
47 1
            KernelEvents::REQUEST => [
48
                // Application::initRenderingで、フロント/管理画面の判定が行われた後に実行
49
                ['onKernelRequestEarly', 500],
50
                // SecurityServiceProviderで、認証処理が完了した後に実行.
51
                ['onKernelRequest', 6],
52
            ],
53
            KernelEvents::RESPONSE => ['onKernelResponse', 0],
54
            KernelEvents::CONTROLLER => ['onKernelController', 0],
55
            KernelEvents::TERMINATE => ['onKernelTerminate', 0],
56
            /*
57
             * Priority -4 is used to come after those from SecurityServiceProvider (0)
58
             * but before the error handlers added with Silex\Application::error (defaults to -8)
59
             */
60
            KernelEvents::EXCEPTION => ['onKernelException', -4],
61
        ];
62
    }
63
64
    /**
65
     * @param GetResponseEvent $event
66
     */
67 436
    public function onKernelRequestEarly(GetResponseEvent $event)
68
    {
69 436
        if (!$event->isMasterRequest()) {
70 118
            return;
71
        }
72
73 436
        $this->logger->info('INIT');
74
    }
75
76
    /**
77
     * @param GetResponseEvent $event
78
     */
79 434 View Code Duplication
    public function onKernelRequest(GetResponseEvent $event)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
    {
81 434
        if (!$event->isMasterRequest()) {
82 118
            return;
83
        }
84
85 433
        $route = $this->getRoute($event->getRequest());
86 433
        $this->logger->info('PROCESS START', [$route]);
87
    }
88
89
    /**
90
     * ルーティング名を取得する.
91
     *
92
     * @param $request
93
     *
94
     * @return string
95
     */
96 436
    private function getRoute($request)
97
    {
98 436
        return $request->attributes->get('_route');
99
    }
100
101
    /**
102
     * @param FilterControllerEvent $event
103
     */
104 424 View Code Duplication
    public function onKernelController(FilterControllerEvent $event)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    {
106 424
        if (!$event->isMasterRequest()) {
107 118
            return;
108
        }
109
110 423
        $route = $this->getRoute($event->getRequest());
111 423
        $this->logger->info('LOGIC START', [$route]);
112
    }
113
114
    /**
115
     * @param FilterResponseEvent $event
116
     */
117 436 View Code Duplication
    public function onKernelResponse(FilterResponseEvent $event)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
    {
119 436
        if (!$event->isMasterRequest()) {
120 118
            return;
121
        }
122
123 436
        $route = $this->getRoute($event->getRequest());
124 436
        $this->logger->info('LOGIC END', [$route]);
125
    }
126
127
    /**
128
     * @param PostResponseEvent $event
129
     */
130 436
    public function onKernelTerminate(PostResponseEvent $event)
131
    {
132 436
        $route = $this->getRoute($event->getRequest());
133 436
        $this->logger->info('PROCESS END', [$route]);
134
    }
135
136
    /**
137
     * @param GetResponseForExceptionEvent $event
138
     */
139
    public function onKernelException(GetResponseForExceptionEvent $event)
140
    {
141
        $e = $event->getException();
142
        if ($e instanceof HttpExceptionInterface && $e->getStatusCode() < 500) {
143
            $this->logger->info($e->getMessage(), [$e->getStatusCode()]);
144
        } else {
145
            $message = sprintf(
146
                '%s: %s (uncaught exception) at %s line %s',
147
                get_class($e),
148
                $e->getMessage(),
149
                $e->getFile(),
150
                $e->getLine()
151
            );
152
            $this->logger->error($message, ['exception' => $e]);
153
        }
154
    }
155
}
156