Completed
Pull Request — 3.x (#257)
by
unknown
01:32
created

SymfonyCache::cacheAction()   B

Complexity

Conditions 9
Paths 10

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 7.5789
c 0
b 0
f 0
cc 9
nc 10
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\CacheBundle\Adapter;
15
16
use Sonata\Cache\CacheAdapterInterface;
17
use Sonata\Cache\CacheElementInterface;
18
use Sonata\Cache\Exception\UnsupportedException;
19
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
20
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
21
use Symfony\Component\Filesystem\Filesystem;
22
use Symfony\Component\HttpFoundation\Response;
23
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
24
use Symfony\Component\Routing\RouterInterface;
25
26
/**
27
 * Handles Symfony cache.
28
 *
29
 * @author Vincent Composieux <[email protected]>
30
 */
31
class SymfonyCache implements CacheAdapterInterface
32
{
33
    /**
34
     * @var RouterInterface
35
     */
36
    protected $router;
37
38
    /**
39
     * @var string
40
     */
41
    protected $cacheDir;
42
43
    /**
44
     * @var string
45
     */
46
    protected $token;
47
48
    /**
49
     * @var string[]
50
     */
51
    protected $types;
52
53
    /**
54
     * @var bool
55
     */
56
    protected $phpCodeCacheEnabled;
57
58
    /**
59
     * @var array
60
     */
61
    protected $servers;
62
63
    /**
64
     * @var array
65
     */
66
    protected $timeouts;
67
68
    /**
69
     * @var Filesystem
70
     */
71
    protected $filesystem;
72
73
    /**
74
     * @var EventDispatcherInterface|null
75
     */
76
    private $eventDispatcher;
77
78
    public function __construct(
79
        RouterInterface $router,
80
        Filesystem $filesystem,
81
        string $cacheDir,
82
        string $token,
83
        bool $phpCodeCacheEnabled,
84
        array $types,
85
        array $servers,
86
        array $timeouts,
87
        EventDispatcherInterface $eventDispatcher = null
88
    ) {
89
        $this->router = $router;
90
        $this->filesystem = $filesystem;
91
        $this->cacheDir = $cacheDir;
92
        $this->token = $token;
93
        $this->types = $types;
94
        $this->phpCodeCacheEnabled = $phpCodeCacheEnabled;
95
        $this->servers = $servers;
96
        $this->timeouts = $timeouts;
97
        $this->eventDispatcher = $eventDispatcher;
98
    }
99
100
    public function flushAll(): bool
101
    {
102
        return $this->flush(['all']);
103
    }
104
105
    /**
106
     * @throws \InvalidArgumentException
107
     * @throws \UnexpectedValueException
108
     */
109
    public function flush(array $keys = ['all']): bool
110
    {
111
        $result = true;
112
113
        foreach ($this->servers as $server) {
114
            foreach ($keys as $type) {
115
                $ip = $server['ip'];
116
117
                if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
118
                    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
119
                } elseif (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
120
                    $socket = socket_create(AF_INET6, SOCK_STREAM, SOL_TCP);
121
                } else {
122
                    throw new \InvalidArgumentException(sprintf('"%s" is not a valid ip address', $ip));
123
                }
124
125
                // generate the raw http request
126
                $command = sprintf("GET %s HTTP/1.1\r\n", $this->getUrl($type));
127
                $command .= sprintf("Host: %s\r\n", $server['domain']);
128
129
                if ($server['basic']) {
130
                    $command .= sprintf("Authorization: Basic %s\r\n", $server['basic']);
131
                }
132
133
                $command .= "Connection: Close\r\n\r\n";
134
135
                // setup the default timeout (avoid max execution time)
136
                socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, $this->timeouts['SND']);
137
                socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, $this->timeouts['RCV']);
138
139
                socket_connect($socket, $server['ip'], $server['port']);
140
                socket_write($socket, $command);
141
142
                $content = '';
143
144
                do {
145
                    $buffer = socket_read($socket, 1024);
146
                    $content .= $buffer;
147
                } while (!empty($buffer));
148
149
                if ($result) {
150
                    $result = 'ok' === substr($content, -2);
151
                } else {
152
                    throw new \UnexpectedValueException(sprintf(
153
                        'Server answered with "%s"',
154
                        $content
155
                    ));
156
                }
157
            }
158
        }
159
160
        return $result;
161
    }
162
163
    /**
164
     * @throws AccessDeniedHttpException
165
     * @throws \RuntimeException
166
     */
167
    public function cacheAction(string $token, string $type): Response
168
    {
169
        if ($this->token !== $token) {
170
            throw new AccessDeniedHttpException('Invalid token.');
171
        }
172
173
        if (!\in_array($type, $this->types, true)) {
174
            throw new \RuntimeException(
175
                sprintf('Type "%s" is not defined, allowed types are: "%s"', $type, implode(', ', $this->types))
176
            );
177
        }
178
179
        $path = 'all' === $type ? $this->cacheDir : sprintf('%s/%s', $this->cacheDir, $type);
180
181
        /*
182
         * NEXT_MAJOR: Remove this if condition and else block.
183
         * clean up event dispatcher just before clearing the cache to prevent further events execution
184
         */
185
        if (null !== $this->eventDispatcher) {
186
            foreach ($this->eventDispatcher->getListeners() as $eventName => $eventListenerList) {
187
                foreach ($eventListenerList as $listener) {
188
                    $listener[0] instanceof EventSubscriberInterface ?
189
                        $this->eventDispatcher->removeSubscriber($listener[0]) :
190
                        $this->eventDispatcher->removeListener($eventName, [$listener[0], $listener[1]]);
191
                }
192
            }
193
        } else {
194
            @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
195
                'Passing no 9th argument is deprecated since version 3.1 and will be mandatory in 4.0.
196
                Pass Symfony\Component\EventDispatcher\EventDispatcherInterface as 9th argument.',
197
                E_USER_DEPRECATED
198
            );
199
        }
200
201
        if ($this->filesystem->exists($path)) {
202
            $movedPath = $path.'_old_'.uniqid();
203
204
            $this->filesystem->rename($path, $movedPath);
205
            $this->filesystem->remove($movedPath);
206
207
            $this->clearPHPCodeCache();
208
        }
209
210
        return new Response('ok', 200, [
211
            'Cache-Control' => 'no-cache, must-revalidate',
212
            'Content-Length' => 2, // to prevent chunked transfer encoding
213
        ]);
214
    }
215
216
    /**
217
     * @throws UnsupportedException
218
     */
219
    public function has(array $keys): bool
220
    {
221
        throw new UnsupportedException('SymfonyCache has() method does not exist.');
222
    }
223
224
    /**
225
     * @throws UnsupportedException
226
     */
227
    public function set(array $keys, $data, int $ttl = 84600, array $contextualKeys = []): CacheElementInterface
228
    {
229
        throw new UnsupportedException('SymfonyCache set() method does not exist.');
230
    }
231
232
    /**
233
     * @throws UnsupportedException
234
     */
235
    public function get(array $keys): CacheElementInterface
236
    {
237
        throw new UnsupportedException('SymfonyCache get() method does not exist.');
238
    }
239
240
    public function isContextual(): bool
241
    {
242
        return false;
243
    }
244
245
    protected function getUrl(string $type): ?string
246
    {
247
        return $this->router->generate('sonata_cache_symfony', [
248
            'token' => $this->token,
249
            'type' => $type,
250
        ]);
251
    }
252
253
    protected function clearPHPCodeCache(): void
254
    {
255
        if (!$this->phpCodeCacheEnabled) {
256
            return;
257
        }
258
259
        if (\function_exists('opcache_reset')) {
260
            opcache_reset();
261
        }
262
    }
263
}
264