Passed
Pull Request — master (#520)
by
unknown
02:47
created

EventDispatchingHttpCache   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Test Coverage

Coverage 96.88%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 25
dl 0
loc 109
ccs 31
cts 32
cp 0.9688
rs 10
c 4
b 0
f 0
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A store() 0 5 1
A invalidate() 0 7 2
A addSubscriber() 0 3 1
A getEventDispatcher() 0 11 3
A addListener() 0 3 1
A handle() 0 12 2
A dispatch() 0 10 2
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\EventDispatcher\LegacyEventDispatcherProxy;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\HttpKernel\HttpKernelInterface;
21
use Symfony\Component\HttpKernel\Kernel;
22
23
/**
24
 * Trait for enhanced Symfony reverse proxy based on the symfony kernel component.
25
 *
26
 * Your kernel needs to implement CacheInvalidatorInterface and redeclare the
27
 * fetch method as public. (The latter is needed because the trait declaring it
28
 * public does not satisfy the interface for whatever reason. See also
29
 * http://stackoverflow.com/questions/31877844/php-trait-exposing-a-method-and-interfaces )
30
 *
31
 * CacheInvalidator kernels support event listeners that can act on the
32
 * events defined in FOS\HttpCache\SymfonyCache\Events and may alter the
33
 * request flow.
34
 *
35
 * If your kernel overwrites any of the methods defined in this trait, make
36
 * sure to also call the trait method. You might get into issues with the order
37
 * of events, in which case you will need to copy event triggering into your
38
 * kernel.
39
 *
40
 * @author Jérôme Vieilledent <[email protected]> (courtesy of eZ Systems AS)
41
 * @author David Buchmann <[email protected]>
42
 *
43
 * {@inheritdoc}
44
 */
45
trait EventDispatchingHttpCache
46
{
47
    /**
48
     * @var EventDispatcherInterface
49
     */
50
    private $eventDispatcher;
51
52
    /**
53
     * Get event dispatcher.
54
     *
55
     * @return EventDispatcherInterface
56
     */
57 10
    public function getEventDispatcher()
58
    {
59 10
        if (!$this->eventDispatcher) {
60 10
            if (class_exists(LegacyEventDispatcherProxy::class)) {
61 10
                $this->eventDispatcher = LegacyEventDispatcherProxy::decorate(new EventDispatcher());
62
            } else {
63
                $this->eventDispatcher = new EventDispatcher();
64
            }
65
        }
66
67 10
        return $this->eventDispatcher;
68
    }
69
70
    /**
71
     * Add an event subscriber.
72
     *
73
     * @see EventDispatcherInterface::addSubscriber
74
     */
75 9
    public function addSubscriber(EventSubscriberInterface $subscriber)
76
    {
77 9
        $this->getEventDispatcher()->addSubscriber($subscriber);
78 9
    }
79
80
    /**
81
     * Add an event listener to this HttpCache.
82
     *
83
     * @see EventDispatcherInterface::addListener
84
     */
85 1
    public function addListener($eventName, $listener, $priority = 0)
86
    {
87 1
        $this->getEventDispatcher()->addListener($eventName, $listener, $priority);
88 1
    }
89
90
    /**
91
     * {@inheritdoc}
92
     *
93
     * Adding the Events::PRE_HANDLE and Events::POST_HANDLE events.
94
     */
95 6
    public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true): Response
0 ignored issues
show
Deprecated Code introduced by
The constant Symfony\Component\HttpKe...terface::MASTER_REQUEST has been deprecated: since symfony/http-kernel 5.3, use MAIN_REQUEST instead. To ease the migration, this constant won't be removed until Symfony 7.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

95
    public function handle(Request $request, $type = /** @scrutinizer ignore-deprecated */ HttpKernelInterface::MASTER_REQUEST, $catch = true): Response

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
96
    {
97
        // trigger loading the CacheEvent to avoid fatal error when HttpKernel::loadClassCache is used.
98 6
        class_exists(CacheEvent::class);
99
100 6
        if ($response = $this->dispatch(Events::PRE_HANDLE, $request, null, $type)) {
101 2
            return $this->dispatch(Events::POST_HANDLE, $request, $response, $type);
102
        }
103
104 4
        $response = parent::handle($request, $type, $catch);
105
106 4
        return $this->dispatch(Events::POST_HANDLE, $request, $response, $type);
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     *
112
     * Trigger event to alter response before storing it in the cache.
113
     */
114 2
    protected function store(Request $request, Response $response)
115
    {
116 2
        $response = $this->dispatch(Events::PRE_STORE, $request, $response);
117
118 2
        parent::store($request, $response);
119 2
    }
120
121
    /**
122
     * {@inheritdoc}
123
     *
124
     * Adding the Events::PRE_INVALIDATE event.
125
     */
126 2
    protected function invalidate(Request $request, $catch = false): Response
127
    {
128 2
        if ($response = $this->dispatch(Events::PRE_INVALIDATE, $request)) {
129 1
            return $response;
130
        }
131
132 1
        return parent::invalidate($request, $catch);
133
    }
134
135
    /**
136
     * Dispatch an event if needed.
137
     *
138
     * @param string        $name        Name of the event to trigger. One of the constants in FOS\HttpCache\SymfonyCache\Events
139
     * @param Response|null $response    If already available
140
     * @param int           $requestType The request type (default HttpKernelInterface::MASTER_REQUEST)
141
     *
142
     * @return Response|null The response to return, which might be provided/altered by a listener
143
     */
144 10
    protected function dispatch($name, Request $request, Response $response = null, $requestType = HttpKernelInterface::MASTER_REQUEST): ?Response
0 ignored issues
show
Deprecated Code introduced by
The constant Symfony\Component\HttpKe...terface::MASTER_REQUEST has been deprecated: since symfony/http-kernel 5.3, use MAIN_REQUEST instead. To ease the migration, this constant won't be removed until Symfony 7.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

144
    protected function dispatch($name, Request $request, Response $response = null, $requestType = /** @scrutinizer ignore-deprecated */ HttpKernelInterface::MASTER_REQUEST): ?Response

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
145
    {
146 10
        if ($this->getEventDispatcher()->hasListeners($name)) {
147 10
            $event = new CacheEvent($this, $request, $response, $requestType);
0 ignored issues
show
Bug introduced by
$this of type FOS\HttpCache\SymfonyCac...entDispatchingHttpCache is incompatible with the type FOS\HttpCache\SymfonyCache\CacheInvalidation expected by parameter $kernel of FOS\HttpCache\SymfonyCac...cheEvent::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

147
            $event = new CacheEvent(/** @scrutinizer ignore-type */ $this, $request, $response, $requestType);
Loading history...
148 10
            $this->getEventDispatcher()->dispatch($event, $name);
149
150 10
            $response = $event->getResponse();
151
        }
152
153 10
        return $response;
154
    }
155
}
156