setCacheContorlHeader()   F
last analyzed

Complexity

Conditions 11
Paths 1024

Size

Total Lines 45
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 22
nc 1024
nop 1
dl 0
loc 45
rs 3.15
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace Spryker\Zed\Http\Communication\Plugin\EventDispatcher;
9
10
use Spryker\Service\Container\ContainerInterface;
11
use Spryker\Shared\EventDispatcher\EventDispatcherInterface;
12
use Spryker\Shared\EventDispatcherExtension\Dependency\Plugin\EventDispatcherPluginInterface;
13
use Spryker\Zed\Kernel\Communication\AbstractPlugin;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\HttpKernel\Event\ResponseEvent;
16
use Symfony\Component\HttpKernel\KernelEvents;
17
18
/**
19
 * @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
20
 *
21
 * @method \Spryker\Zed\Http\HttpConfig getConfig()
22
 * @method \Spryker\Zed\Http\Communication\HttpCommunicationFactory getFactory()
23
 */
24
class CacheControlHeaderEventDispatcherPlugin extends AbstractPlugin implements EventDispatcherPluginInterface
25
{
26
    /**
27
     * @var string
28
     */
29
    protected const CONFIG_CACHE_CONTROL_DIRECTIVE_MAX_AGE = 'max-age';
30
31
    /**
32
     * @var string
33
     */
34
    protected const CONFIG_CACHE_CONTROL_DIRECTIVE_S_MAX_AGE = 's-maxage';
35
36
    /**
37
     * @var string
38
     */
39
    protected const CONFIG_CACHE_CONTROL_DIRECTIVE_NO_CACHE = 'no-cache';
40
41
    /**
42
     * @var string
43
     */
44
    protected const CONFIG_CACHE_CONTROL_DIRECTIVE_MUST_REVALIDATE = 'must-revalidate';
45
46
    /**
47
     * @var string
48
     */
49
    protected const CONFIG_CACHE_CONTROL_DIRECTIVE_NO_STORE = 'no-store';
50
51
    /**
52
     * @var string
53
     */
54
    protected const CONFIG_CACHE_CONTROL_DIRECTIVE_PUBLIC = 'public';
55
56
    /**
57
     * @var string
58
     */
59
    protected const CONFIG_CACHE_CONTROL_DIRECTIVE_PRIVATE = 'private';
60
61
    /**
62
     * @var string
63
     */
64
    protected const CONFIG_CACHE_CONTROL_DIRECTIVE_NO_TRANSFORM = 'no-transform';
65
66
    /**
67
     * @var string
68
     */
69
    protected const CONFIG_CACHE_CONTROL_DIRECTIVE_IMMUTABLE = 'immutable';
70
71
    /**
72
     * @var string
73
     */
74
    protected const CONFIG_CACHE_CONTROL_STALE_WHILE_REVALIDATE = 'stale-while-revalidate';
75
76
    /**
77
     * @var string
78
     */
79
    protected const HEADER_CACHE_CONTROL = 'Cache-Control';
80
81
    /**
82
     * {@inheritDoc}
83
     * - Sets `Cache-Control` header to response.
84
     *
85
     * @api
86
     *
87
     * @param \Spryker\Shared\EventDispatcher\EventDispatcherInterface $eventDispatcher
88
     * @param \Spryker\Service\Container\ContainerInterface $container
89
     *
90
     * @return \Spryker\Shared\EventDispatcher\EventDispatcherInterface
91
     */
92
    public function extend(EventDispatcherInterface $eventDispatcher, ContainerInterface $container): EventDispatcherInterface
93
    {
94
        $eventDispatcher->addListener(KernelEvents::RESPONSE, function (ResponseEvent $event): void {
95
            if (!$this->isMainRequest($event)) {
96
                return;
97
            }
98
99
            $event->setResponse($this->setCacheContorlHeader($event->getResponse()));
100
        });
101
102
        return $eventDispatcher;
103
    }
104
105
    /**
106
     * @param \Symfony\Component\HttpKernel\Event\ResponseEvent $event
107
     *
108
     * @return bool
109
     */
110
    protected function isMainRequest(ResponseEvent $event): bool
111
    {
112
        if (method_exists($event, 'isMasterRequest')) {
113
            return $event->isMasterRequest();
114
        }
115
116
        return $event->isMainRequest();
117
    }
118
119
    /**
120
     * @param \Symfony\Component\HttpFoundation\Response $response
121
     *
122
     * @return \Symfony\Component\HttpFoundation\Response
123
     */
124
    protected function setCacheContorlHeader(Response $response): Response
125
    {
126
        $cacheControlConfig = $this->getConfig()->getCacheControlConfig();
127
128
        if (!empty($cacheControlConfig[static::CONFIG_CACHE_CONTROL_DIRECTIVE_PUBLIC])) {
129
            $response->setPublic();
130
        }
131
132
        if (!empty($cacheControlConfig[static::CONFIG_CACHE_CONTROL_DIRECTIVE_PRIVATE])) {
133
            $response->setPrivate();
134
        }
135
136
        if (!empty($cacheControlConfig[static::CONFIG_CACHE_CONTROL_DIRECTIVE_MAX_AGE])) {
137
            $response->setMaxAge($cacheControlConfig[static::CONFIG_CACHE_CONTROL_DIRECTIVE_MAX_AGE]);
138
        }
139
140
        if (!empty($cacheControlConfig[static::CONFIG_CACHE_CONTROL_DIRECTIVE_S_MAX_AGE])) {
141
            $response->setSharedMaxAge($cacheControlConfig[static::CONFIG_CACHE_CONTROL_DIRECTIVE_S_MAX_AGE]);
142
        }
143
144
        if (!empty($cacheControlConfig[static::CONFIG_CACHE_CONTROL_DIRECTIVE_NO_CACHE])) {
145
            $response->headers->addCacheControlDirective(static::CONFIG_CACHE_CONTROL_DIRECTIVE_NO_CACHE, true);
146
        }
147
148
        if (!empty($cacheControlConfig[static::CONFIG_CACHE_CONTROL_DIRECTIVE_MUST_REVALIDATE])) {
149
            $response->headers->addCacheControlDirective(static::CONFIG_CACHE_CONTROL_DIRECTIVE_MUST_REVALIDATE, true);
150
        }
151
152
        if (!empty($cacheControlConfig[static::CONFIG_CACHE_CONTROL_DIRECTIVE_NO_STORE])) {
153
            $response->headers->addCacheControlDirective(static::CONFIG_CACHE_CONTROL_DIRECTIVE_NO_STORE, true);
154
        }
155
156
        if (!empty($cacheControlConfig[static::CONFIG_CACHE_CONTROL_DIRECTIVE_NO_TRANSFORM])) {
157
            $response->headers->addCacheControlDirective(static::CONFIG_CACHE_CONTROL_DIRECTIVE_NO_TRANSFORM, true);
158
        }
159
160
        if (!empty($cacheControlConfig[static::CONFIG_CACHE_CONTROL_DIRECTIVE_IMMUTABLE])) {
161
            $response->headers->addCacheControlDirective(static::CONFIG_CACHE_CONTROL_DIRECTIVE_IMMUTABLE, true);
162
        }
163
164
        if (!empty($cacheControlConfig[static::CONFIG_CACHE_CONTROL_STALE_WHILE_REVALIDATE])) {
165
            $response->headers->addCacheControlDirective(static::CONFIG_CACHE_CONTROL_STALE_WHILE_REVALIDATE, true);
166
        }
167
168
        return $response;
169
    }
170
}
171