Completed
Push — master ( 4d65fa...1a0d8a )
by
unknown
12s
created

BypassCacheListener::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace MovingImage\Bundle\VMProApiBundle\EventListener;
4
5
use MovingImage\Bundle\VMProApiBundle\Decorator\BlackholeCacheItemPoolDecorator;
6
use MovingImage\Client\VMPro\ApiClient;
7
use MovingImage\Client\VMPro\ApiClient\AbstractApiClient;
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 ApiClient
23
     */
24
    private $apiClient;
25
26
    /**
27
     * @var string|null
28
     */
29
    private $cacheBypassArgument;
30
31
    /**
32
     * @param ApiClient $apiClient
33
     * @param string    $cacheBypassArgument
34
     */
35
    public function __construct(ApiClient $apiClient, $cacheBypassArgument = null)
36
    {
37
        $this->apiClient = $apiClient;
38
        $this->cacheBypassArgument = $cacheBypassArgument;
39
    }
40
41
    /**
42
     * @param GetResponseEvent $event
43
     */
44
    public function onKernelRequest(GetResponseEvent $event)
45
    {
46
        if (is_null($this->cacheBypassArgument)) {
47
            return;
48
        }
49
50
        $request = $event->getRequest();
51
        if ($request->get($this->cacheBypassArgument) || $request->cookies->get($this->cacheBypassArgument)) {
52
            /** @var AbstractApiClient $apiClient */
53
            $cachePool = new BlackholeCacheItemPoolDecorator($this->apiClient->getCacheItemPool());
0 ignored issues
show
Bug introduced by
The method getCacheItemPool() does not seem to exist on object<MovingImage\Client\VMPro\ApiClient>.

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...
54
            $this->apiClient->setCacheItemPool($cachePool);
0 ignored issues
show
Bug introduced by
The method setCacheItemPool() does not seem to exist on object<MovingImage\Client\VMPro\ApiClient>.

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...
55
        }
56
    }
57
58
    /**
59
     * @return array
60
     */
61
    public static function getSubscribedEvents()
62
    {
63
        return [
64
            KernelEvents::REQUEST => 'onKernelRequest',
65
        ];
66
    }
67
}
68