Completed
Pull Request — master (#328)
by David
05:29
created

EventDispatchingHttpCache::addListener()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 3
crap 1
1
<?php
2
3
/*
4
 * This file is part of the FOSHttpCache package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\HttpCache\SymfonyCache;
13
14
use Symfony\Component\EventDispatcher\EventDispatcher;
15
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\HttpKernel\HttpKernelInterface;
20
21
/**
22
 * Trait for enhanced Symfony reverse proxy based on the symfony kernel component.
23
 *
24
 * Your kernel needs to implement CacheInvalidatorInterface and redeclare the
25
 * fetch method as public. (The latter is needed because the trait declaring it
26
 * public does not satisfy the interface for whatever reason. See also
27
 * http://stackoverflow.com/questions/31877844/php-trait-exposing-a-method-and-interfaces )
28
 *
29
 * CacheInvalidator kernels support event listeners that can act on the
30
 * events defined in FOS\HttpCache\SymfonyCache\Events and may alter the
31
 * request flow.
32
 *
33
 * If your kernel overwrites any of the methods defined in this trait, make
34
 * sure to also call the trait method. You might get into issues with the order
35
 * of events, in which case you will need to copy event triggering into your
36
 * kernel.
37
 *
38
 * @author Jérôme Vieilledent <[email protected]> (courtesy of eZ Systems AS)
39
 * @author David Buchmann <[email protected]>
40
 *
41
 * {@inheritdoc}
42
 */
43
trait EventDispatchingHttpCache
44
{
45
    /**
46
     * @var EventDispatcherInterface
47
     */
48
    private $eventDispatcher;
49
50
    /**
51
     * Get event dispatcher.
52
     *
53
     * @return EventDispatcherInterface
54
     */
55 10
    public function getEventDispatcher()
56
    {
57 10
        if (null === $this->eventDispatcher) {
58 10
            $this->eventDispatcher = new EventDispatcher();
59 10
        }
60
61 10
        return $this->eventDispatcher;
62
    }
63
64
    /**
65
     * Add an event subscriber.
66
     *
67
     * @see EventDispatcherInterface::addSubscriber
68
     */
69 9
    public function addSubscriber(EventSubscriberInterface $subscriber)
70
    {
71 9
        $this->getEventDispatcher()->addSubscriber($subscriber);
72 9
    }
73
74
    /**
75
     * Add an event listener to this HttpCache.
76
     *
77
     * @see EventDispatcherInterface::addListener
78
     */
79 1
    public function addListener($eventName, $listener, $priority = 0)
80
    {
81 1
        $this->getEventDispatcher()->addListener($eventName, $listener, $priority);
82 1
    }
83
84
    /**
85
     * {@inheritdoc}
86
     *
87
     * Adding the Events::PRE_HANDLE and Events::POST_HANDLE events.
88
     */
89 4
    public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
90
    {
91
        // trigger loading the CacheEvent to avoid fatal error when HttpKernel::loadClassCache is used.
92
        class_exists(CacheEvent::class);
93
94
        if ($response = $this->dispatch(Events::PRE_HANDLE, $request)) {
95
            return $this->dispatch(Events::POST_HANDLE, $request, $response);
96
        }
97
98 4
        $response = parent::handle($request, $type, $catch);
99
100 4
        return $this->dispatch(Events::POST_HANDLE, $request, $response);
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     *
106
     * Trigger event to alter response before storing it in the cache.
107
     */
108 2
    protected function store(Request $request, Response $response)
109
    {
110 2
        $response = $this->dispatch(Events::PRE_STORE, $request, $response);
111
112 2
        parent::store($request, $response);
113 2
    }
114
115
    /**
116
     * {@inheritdoc}
117
     *
118
     * Adding the Events::PRE_INVALIDATE event.
119
     */
120 2
    protected function invalidate(Request $request, $catch = false)
121
    {
122 2
        if ($response = $this->dispatch(Events::PRE_INVALIDATE, $request)) {
123 1
            return $response;
124
        }
125
126 1
        return parent::invalidate($request, $catch);
127
    }
128
129
    /**
130
     * Dispatch an event if needed.
131
     *
132
     * @param string        $name     Name of the event to trigger. One of the constants in FOS\HttpCache\SymfonyCache\Events
133
     * @param Request       $request
134
     * @param Response|null $response If already available
135
     *
136
     * @return Response The response to return, which might be provided/altered by a listener
137
     */
138 10
    protected function dispatch($name, Request $request, Response $response = null)
139
    {
140 10
        if ($this->getEventDispatcher()->hasListeners($name)) {
141 10
            $event = new CacheEvent($this, $request, $response);
142 10
            $this->getEventDispatcher()->dispatch($name, $event);
143 10
            $response = $event->getResponse();
144 10
        }
145
146 10
        return $response;
147
    }
148
}
149