Completed
Push — dev/recommend-plugins ( b4eeb7...acd8c2 )
by Kiyotaka
08:44
created

ExceptionListener::onKernelException()   C

Complexity

Conditions 11
Paths 78

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
nc 78
nop 1
dl 0
loc 51
rs 6.9224
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.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\Request\Context;
17
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
20
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
21
use Symfony\Component\HttpKernel\KernelEvents;
22
23
class ExceptionListener implements EventSubscriberInterface
24
{
25
    /**
26
     * @var \Twig_Environment
27
     */
28
    private $twig;
29
30
    /**
31
     * @var Context
32
     */
33
    protected $requestContext;
34
35
    /**
36
     * ExceptionListener constructor.
37
     */
38
    public function __construct(\Twig_Environment $twig, Context $requestContext)
39
    {
40
        $this->twig = $twig;
41
        $this->requestContext = $requestContext;
42
    }
43
44
    public function onKernelException(GetResponseForExceptionEvent $event)
45
    {
46
        $title = 'システムエラーが発生しました。';
47
        $message = '大変お手数ですが、サイト管理者までご連絡ください。';
48
        $statusCode = Response::HTTP_INTERNAL_SERVER_ERROR;
49
50
        $exception = $event->getException();
51
52
        if ($exception instanceof HttpExceptionInterface) {
53
            $statusCode = $exception->getStatusCode();
54
            switch ($statusCode) {
55
                case 400:
56
                case 401:
57
                case 403:
58
                case 405:
59
                case 406:
60
                    $title = 'アクセスできません。';
61
                    if ($exception->getMessage()) {
62
                        $message = $exception->getMessage();
63
                    } else {
64
                        $message = 'お探しのページはアクセスができない状況にあるか、移動もしくは削除された可能性があります。';
65
                    }
66
                    break;
67
                case 404:
68
                    $title = 'ページがみつかりません。';
69
                    $message = 'URLに間違いがないかご確認ください。';
70
                    break;
71
                default:
72
                    break;
73
            }
74
        }
75
76
        log_error('システムエラーが発生しました。', [
77
            $exception->getMessage(),
78
            $exception->getFile(),
79
            $exception->getLine(),
80
            $exception->getTraceAsString()
81
        ]);
82
83
        try {
84
            $file = $this->requestContext->isAdmin() ? '@admin/error.twig' : 'error.twig';
85
            $content = $this->twig->render($file, [
86
                'error_title' => $title,
87
                'error_message' => $message,
88
            ]);
89
        } catch (\Exception $ignore) {
90
            $content = $title;
91
        }
92
93
        $event->setResponse(Response::create($content, $statusCode));
94
    }
95
96
    /**
97
     * Returns an array of event names this subscriber wants to listen to.
98
     *
99
     * The array keys are event names and the value can be:
100
     *
101
     *  * The method name to call (priority defaults to 0)
102
     *  * An array composed of the method name to call and the priority
103
     *  * An array of arrays composed of the method names to call and respective
104
     *    priorities, or 0 if unset
105
     *
106
     * For instance:
107
     *
108
     *  * array('eventName' => 'methodName')
109
     *  * array('eventName' => array('methodName', $priority))
110
     *  * array('eventName' => array(array('methodName1', $priority), array('methodName2')))
111
     *
112
     * @return array The event names to listen to
113
     */
114
    public static function getSubscribedEvents()
115
    {
116
        return [
117
            KernelEvents::EXCEPTION => ['onKernelException'],
118
        ];
119
    }
120
}
121