Completed
Pull Request — master (#10)
by
unknown
12:33
created

BypassCacheListener::onKernelRequest()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 12
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
crap 12
1
<?php
2
3
namespace MovingImage\Bundle\VMProApiBundle\EventListener;
4
5
use MovingImage\Bundle\VMProApiBundle\Decorator\BlackholeCacheItemPoolDecorator;
6
use MovingImage\Client\VMPro\ApiClient\AbstractApiClient;
7
use Symfony\Component\DependencyInjection\ContainerInterface;
8
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
9
use Symfony\Component\HttpKernel\KernelEvents;
10
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11
12
/**
13
 * This listener kicks in only if the `cache_bypass_argument` bundle config option is set.
14
 * If the request contains an argument matching the value configured in the aforementioned config option
15
 * and the value of that argument evaluates to true, this listener will modify the cache pool implementation
16
 * used by the VMPro API client, by decorating it with a blackhole cache implementation:
17
 * one that stores responses to cache, but never returns a hit.
18
 */
19
class BypassCacheListener implements EventSubscriberInterface
20
{
21
    /**
22
     * @var ContainerInterface
23
     */
24
    private $container;
25
26
    /**
27
     * @var string|null
28
     */
29
    private $cacheBypassArgument;
30
31
    /**
32
     * @param ContainerInterface $container
33
     * @param string             $cacheBypassArgument
34
     */
35
    public function __construct(ContainerInterface $container, $cacheBypassArgument = null)
36
    {
37
        $this->container = $container;
38
        $this->cacheBypassArgument = $cacheBypassArgument;
39
    }
40
41
    /**
42
     * @param GetResponseEvent $event
43
     */
44
    public function onKernelRequest(GetResponseEvent $event)
45
    {
46
        if (!$this->cacheBypassArgument) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->cacheBypassArgument of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
47
            return;
48
        }
49
50
        $request = $event->getRequest();
51
52
        if ($request->get($this->cacheBypassArgument)) {
53
            /** @var AbstractApiClient $apiClient */
54
            $apiClient = $this->container->get('vmpro_api.client');
55
            $cachePool = new BlackholeCacheItemPoolDecorator($apiClient->getCacheItemPool());
0 ignored issues
show
Bug introduced by
The method getCacheItemPool() does not seem to exist on object<MovingImage\Clien...ient\AbstractApiClient>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
            $apiClient->setCacheItemPool($cachePool);
0 ignored issues
show
Bug introduced by
The method setCacheItemPool() does not seem to exist on object<MovingImage\Clien...ient\AbstractApiClient>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
        }
58
    }
59
60
    /**
61
     * @return array
62
     */
63
    public static function getSubscribedEvents()
64
    {
65
        return [
66
            KernelEvents::REQUEST => 'onKernelRequest',
67
        ];
68
    }
69
}
70