MaintenanceSubscriber   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 0
loc 94
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getSubscribedEvents() 0 6 1
B onKernelRequest() 0 19 8
A setLogger() 0 4 1
A isInMentenance() 0 8 2
A isDueDate() 0 13 3
1
<?php
2
3
namespace Iulyanp\MaintenanceBundle\EventListener;
4
5
use Psr\Log\LoggerInterface;
6
use Symfony\Bundle\FrameworkBundle\Routing\Router;
7
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8
use Symfony\Component\HttpFoundation\RedirectResponse;
9
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
10
use Symfony\Component\HttpKernel\KernelEvents;
11
12
/**
13
 * Class MaintenanceSubscriber.
14
 */
15
class MaintenanceSubscriber implements EventSubscriberInterface
16
{
17
    /**
18
     * @var LoggerInterface
19
     */
20
    private $logger;
21
22
    /**
23
     * @var string
24
     */
25
    private $maintenance;
26
27
    /**
28
     * MaintenanceSubscriber constructor.
29
     *
30
     * @param Router $router
31
     * @param string $maintenance
32
     */
33
    public function __construct(Router $router, $maintenance)
34
    {
35
        $this->router = $router;
0 ignored issues
show
Bug introduced by
The property router does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
36
        $this->maintenance = $maintenance;
37
    }
38
39
    /**
40
     * @return array
41
     */
42
    public static function getSubscribedEvents()
43
    {
44
        return [
45
            KernelEvents::REQUEST => 'onKernelRequest',
46
        ];
47
    }
48
49
    /**
50
     * @param GetResponseEvent $event
51
     */
52
    public function onKernelRequest(GetResponseEvent $event)
53
    {
54
        if (!$event->isMasterRequest()) {
55
            return;
56
        }
57
58
        $request = $event->getRequest();
59
        $requestUri = $request->getRequestUri();
60
        $currentRoute = $request->get('_route');
61
        $maintenanceRouteUri = $this->router->generate($this->maintenance['maintenance_route']);
62
63
        if ($this->isInMentenance() && !$this->isDueDate() && $this->maintenance['maintenance_route'] != $currentRoute) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
64
            $response = new RedirectResponse($maintenanceRouteUri);
65
            $event->setResponse($response);
66
        } elseif ((!$this->isInMentenance() || $this->isDueDate()) && $maintenanceRouteUri == $requestUri) {
67
            $response = new RedirectResponse('/');
68
            $event->setResponse($response);
69
        }
70
    }
71
72
    /**
73
     * @param LoggerInterface $logger
74
     */
75
    public function setLogger(LoggerInterface $logger)
76
    {
77
        $this->logger = $logger;
78
    }
79
80
    /**
81
     * @return bool
82
     */
83
    private function isInMentenance()
84
    {
85
        if (!$this->maintenance['enabled']) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return (bool) $this->maintenance['enabled'];.
Loading history...
86
            return false;
87
        }
88
89
        return true;
90
    }
91
92
    /**
93
     * @return bool
94
     */
95
    private function isDueDate()
96
    {
97
        if (!$this->maintenance['due_date']) {
98
            return false;
99
        }
100
101
        $currentDateTime = new \DateTime();
102
        if ($currentDateTime->getTimestamp() > strtotime($this->maintenance['due_date'])) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return $currentDateTime-...intenance['due_date']);.
Loading history...
103
            return true;
104
        }
105
106
        return false;
107
    }
108
}
109