KernelExceptionListener   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 41
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSubscribedEvents() 0 6 1
A onKernelHttpNotFoundException() 0 22 5
1
<?php
2
3
namespace RewriteUrl\EventListeners;
4
5
use RewriteUrl\Model\RewriteurlRule;
6
use RewriteUrl\Model\RewriteurlRuleQuery;
7
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8
use Symfony\Component\HttpFoundation\RequestStack;
9
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
10
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
11
use Symfony\Component\HttpKernel\KernelEvents;
12
use Thelia\Core\HttpKernel\Exception\RedirectException;
13
use Thelia\Core\HttpFoundation\Request as TheliaRequest;
14
use Thelia\Tools\URL;
15
16
class KernelExceptionListener implements EventSubscriberInterface
17
{
18
    /** @var RequestStack */
19
    private $requestStack;
20
21
    public function __construct(RequestStack $requestStack)
22
    {
23
        $this->requestStack = $requestStack;
24
    }
25
26
27
    public static function getSubscribedEvents()
28
    {
29
        return [
30
            KernelEvents::EXCEPTION => ['onKernelHttpNotFoundException', 300],
31
        ];
32
    }
33
34
    public function onKernelHttpNotFoundException(GetResponseForExceptionEvent $event)
35
    {
36
        if ($event->getException() instanceof NotFoundHttpException) {
0 ignored issues
show
Bug introduced by
The class Symfony\Component\HttpKe...n\NotFoundHttpException does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
37
            $urlTool = URL::getInstance();
38
39
            $ruleCollection = RewriteurlRuleQuery::create()
40
                ->filterByOnly404(1)
41
                ->orderByPosition()
42
                ->find();
43
44
            $request = $this->requestStack->getCurrentRequest();
45
            $pathInfo = $request instanceof TheliaRequest ? $request->getRealPathInfo() : $request->getPathInfo();
0 ignored issues
show
Bug introduced by
The class Thelia\Core\HttpFoundation\Request does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
46
47
            /** @var RewriteurlRule $rule */
48
            foreach ($ruleCollection as $rule) {
49
                if ($rule->isMatching($pathInfo, $request->query->all())) {
50
                    $event->setException(new RedirectException($urlTool->absoluteUrl($rule->getRedirectUrl()), 301));
51
                    return;
52
                }
53
            }
54
        }
55
    }
56
}
57