Completed
Pull Request — master (#34)
by
unknown
01:16
created

RequestListener   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 34
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A redirect() 0 18 3
A getSubscribedEvents() 0 7 1
1
<?php
2
3
namespace RewriteUrl\EventListeners;
4
5
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
6
use Symfony\Component\HttpFoundation\RedirectResponse;
7
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
8
use Symfony\Component\HttpKernel\KernelEvents;
9
use Thelia\Model\ConfigQuery;
10
11
12
class RequestListener implements EventSubscriberInterface
13
{
14
15
    // permanently redirect request if url contain $regexToMatch string
16
17
    public function redirect(GetResponseEvent $event)
18
    {
19
        if (ConfigQuery::isRedirectionEnable())
20
        {
21
            $request = $event->getRequest();
22
            $fullPath = $request->getUri();
23
            $regexToMatch = "^\/index.php^";
24
25
            if (preg_match($regexToMatch, $fullPath)) 
26
            {
27
                $newPath = preg_replace($regexToMatch, "", $fullPath) ;
28
                $event->setResponse(new RedirectResponse(
29
                    $newPath,
30
                    301
31
                ));                
32
            }
33
        }
34
    }
35
36
    // event to listen to
37
    public static function getSubscribedEvents()
38
    {       
39
        return 
40
        [
41
            KernelEvents::REQUEST => ["redirect"],
42
        ];    
43
    }
44
45
}