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\Yves\Http\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\Yves\Kernel\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\Yves\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
     * @param \Spryker\Shared\EventDispatcher\EventDispatcherInterface $eventDispatcher
85
     * @param \Spryker\Service\Container\ContainerInterface $container
86
     *
87
     * @return \Spryker\Shared\EventDispatcher\EventDispatcherInterface
88
     */
89
    public function extend(EventDispatcherInterface $eventDispatcher, ContainerInterface $container): EventDispatcherInterface
90
    {
91
        $eventDispatcher->addListener(KernelEvents::RESPONSE, function (ResponseEvent $event): void {
92
            if (!$this->isMainRequest($event)) {
93
                return;
94
            }
95
96
            $event->setResponse($this->setCacheContorlHeader($event->getResponse()));
97
        });
98
99
        return $eventDispatcher;
100
    }
101
102
    /**
103
     * @param \Symfony\Component\HttpKernel\Event\ResponseEvent $event
104
     *
105
     * @return bool
106
     */
107
    protected function isMainRequest(ResponseEvent $event): bool
108
    {
109
        if (method_exists($event, 'isMasterRequest')) {
110
            return $event->isMasterRequest();
111
        }
112
113
        return $event->isMainRequest();
114
    }
115
116
    /**
117
     * @param \Symfony\Component\HttpFoundation\Response $response
118
     *
119
     * @return \Symfony\Component\HttpFoundation\Response
120
     */
121
    protected function setCacheContorlHeader(Response $response): Response
122
    {
123
        $cacheControlConfig = $this->getConfig()->getCacheControlConfig();
124
125
        if (!empty($cacheControlConfig[static::CONFIG_CACHE_CONTROL_DIRECTIVE_PUBLIC])) {
126
            $response->setPublic();
127
        }
128
129
        if (!empty($cacheControlConfig[static::CONFIG_CACHE_CONTROL_DIRECTIVE_PRIVATE])) {
130
            $response->setPrivate();
131
        }
132
133
        if (!empty($cacheControlConfig[static::CONFIG_CACHE_CONTROL_DIRECTIVE_MAX_AGE])) {
134
            $response->setMaxAge($cacheControlConfig[static::CONFIG_CACHE_CONTROL_DIRECTIVE_MAX_AGE]);
135
        }
136
137
        if (!empty($cacheControlConfig[static::CONFIG_CACHE_CONTROL_DIRECTIVE_S_MAX_AGE])) {
138
            $response->setSharedMaxAge($cacheControlConfig[static::CONFIG_CACHE_CONTROL_DIRECTIVE_S_MAX_AGE]);
139
        }
140
141
        if (!empty($cacheControlConfig[static::CONFIG_CACHE_CONTROL_DIRECTIVE_NO_CACHE])) {
142
            $response->headers->addCacheControlDirective(static::CONFIG_CACHE_CONTROL_DIRECTIVE_NO_CACHE, true);
143
        }
144
145
        if (!empty($cacheControlConfig[static::CONFIG_CACHE_CONTROL_DIRECTIVE_MUST_REVALIDATE])) {
146
            $response->headers->addCacheControlDirective(static::CONFIG_CACHE_CONTROL_DIRECTIVE_MUST_REVALIDATE, true);
147
        }
148
149
        if (!empty($cacheControlConfig[static::CONFIG_CACHE_CONTROL_DIRECTIVE_NO_STORE])) {
150
            $response->headers->addCacheControlDirective(static::CONFIG_CACHE_CONTROL_DIRECTIVE_NO_STORE, true);
151
        }
152
153
        if (!empty($cacheControlConfig[static::CONFIG_CACHE_CONTROL_DIRECTIVE_NO_TRANSFORM])) {
154
            $response->headers->addCacheControlDirective(static::CONFIG_CACHE_CONTROL_DIRECTIVE_NO_TRANSFORM, true);
155
        }
156
157
        if (!empty($cacheControlConfig[static::CONFIG_CACHE_CONTROL_DIRECTIVE_IMMUTABLE])) {
158
            $response->headers->addCacheControlDirective(static::CONFIG_CACHE_CONTROL_DIRECTIVE_IMMUTABLE, true);
159
        }
160
161
        if (!empty($cacheControlConfig[static::CONFIG_CACHE_CONTROL_STALE_WHILE_REVALIDATE])) {
162
            $response->headers->addCacheControlDirective(static::CONFIG_CACHE_CONTROL_STALE_WHILE_REVALIDATE, true);
163
        }
164
165
        return $response;
166
    }
167
}
168