Completed
Pull Request — master (#34)
by
unknown
01:06
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 RewriteUrl\RewriteUrl;
6
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
7
use Symfony\Component\HttpFoundation\RedirectResponse;
8
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
9
use Symfony\Component\HttpKernel\KernelEvents;
10
use Thelia\Model\ConfigQuery;
11
12
13
class RequestListener implements EventSubscriberInterface
14
{
15
    public function redirect(GetResponseEvent $event)
16
    {
17
        $fullPath = $event->getRequest()->getUri();
18
        $regexToMatch = "^\/index.php^";
19
20
        /** permanently redirect request if url contain $regexToMatch string */
21
22
        if (RewriteUrl::getConfigValue("index_redirection_enable"))
23
        {
24
            if (preg_match($regexToMatch, $fullPath)) 
25
            {
26
                $newPath = preg_replace($regexToMatch, "", $fullPath) ;
27
                $event->setResponse(new RedirectResponse(
28
                    $newPath,
29
                    301
30
                ));                
31
            }
32
        }
33
34
        /** permanently redirect http to https protocol */
35
36
        if (RewriteUrl::getConfigValue("https_redirection_enable"))
37
        {
38
            if (!$event->getRequest()->isSecure()) 
39
            {
40
                $securePath = preg_replace("/^http:/i", "https:", $fullPath) ;
41
                $event->setResponse(new RedirectResponse(
42
                    $securePath,
43
                    301
44
                ));                
45
            }
46
        }
47
    }
48
49
    public static function getSubscribedEvents()
50
    {       
51
        return 
52
        [
53
            KernelEvents::REQUEST => ["redirect"],
54
        ];    
55
    }
56
}