ClockSubscriber::getSubscribedEvents()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 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