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

RequestListener   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 0
dl 0
loc 48
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A redirect() 0 33 5
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::isIndexRedirectionEnable())
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
        if (ConfigQuery::isHttpsRedirectionEnable())
36
        {
37
            $request = $event->getRequest();
38
            $fullPath = $request->getUri();
39
40
            if (!$request->isSecure()) 
41
            {
42
                $securePath = str_replace('http', 'https', $fullPath) ;
43
                $event->setResponse(new RedirectResponse(
44
                    $securePath,
45
                    301
46
                ));                
47
            }
48
        }
49
    }
50
51
    // event to listen to
52
    public static function getSubscribedEvents()
53
    {       
54
        return 
55
        [
56
            KernelEvents::REQUEST => ["redirect"],
57
        ];    
58
    }
59
}