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

RequestListener::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
}