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