ClockSubscriber   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 51
ccs 0
cts 27
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSubscribedEvents() 0 7 1
A onKernelRequest() 0 9 2
A onConsoleCommand() 0 9 2
1
<?php
2
3
namespace Majora\Bundle\FrameworkExtraBundle\Event;
4
5
use Majora\Framework\Date\Clock;
6
use Symfony\Component\Console\ConsoleEvents;
7
use Symfony\Component\Console\Event\ConsoleCommandEvent;
8
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
10
use Symfony\Component\HttpKernel\KernelEvents;
11
12
/**
13
 * Framework event subscriber, looking for a mocked date
14
 * If guessed, define it as current one for Clock service
15
 *
16
 * @example
17
 *      /app_dev.php/article/1?_date_mock=2015-01-01
18
 */
19
class ClockSubscriber extends Clock implements EventSubscriberInterface
20
{
21
    protected $mockParamName;
22
23
    /**
24
     * construct
25
     *
26
     * @param string $mockParamName
27
     */
28
    public function __construct($mockParamName)
29
    {
30
        $this->mockParamName = $mockParamName;
31
    }
32
33
    /**
34
     * @see EventSubscriberInterface::getSubscribedEvents()
35
     */
36
    static public function getSubscribedEvents()
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
37
    {
38
        return array(
39
            KernelEvents::REQUEST => array('onKernelRequest', 100),
40
            ConsoleEvents::COMMAND => array('onConsoleCommand', 100),
41
        );
42
    }
43
44
    /**
45
     * kernel request event handler
46
     */
47
    public function onKernelRequest(GetResponseEvent $event)
48
    {
49
        $request = $event->getRequest();
50
        if (!$strMockedDate = $request->query->get($this->mockParamName)) {
51
            return;
52
        }
53
54
        $this->mock($strMockedDate);
55
    }
56
57
    /**
58
     * console command event handler
59
     */
60
    public function onConsoleCommand(ConsoleCommandEvent $event)
61
    {
62
        $input = $event->getInput();
63
        if (!$input->hasOption($this->mockParamName)) {
64
            return;
65
        }
66
67
        $this->mock($input->getOption($this->mockParamName));
68
    }
69
}
70