ApcCache   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 59
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A cacheAction() 0 19 5
A getUrl() 0 4 1
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\Adapter\Cache\ApcCache as BaseApcCache;
17
use Symfony\Component\HttpFoundation\Response;
18
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
19
use Symfony\Component\Routing\RouterInterface;
20
21
class ApcCache extends BaseApcCache
22
{
23
    /**
24
     * @var RouterInterface
25
     */
26
    protected $router;
27
28
    /**
29
     * @var string
30
     */
31
    protected $token;
32
33
    /**
34
     * @param string   $token   A token to clear the related cache
35
     * @param string   $prefix  A prefix to avoid clash between instances
36
     * @param string[] $servers An array of servers
37
     * @param array    $timeout An array of timeout options
38
     */
39
    public function __construct(
40
        RouterInterface $router,
41
        string $token,
42
        string $prefix,
43
        array $servers,
44
        array $timeout = []
45
    ) {
46
        parent::__construct('', $prefix, $servers, $timeout);
47
48
        $this->router = $router;
49
        $this->token = $token;
50
    }
51
52
    /**
53
     * @throws AccessDeniedHttpException
54
     */
55
    public function cacheAction(string $token): Response
56
    {
57
        if ($this->token === $token) {
58
            if (\function_exists('opcache_reset')) {
59
                opcache_reset();
60
            }
61
62
            if (\extension_loaded('apcu') && ini_get('apcu.enabled')) {
63
                apcu_clear_cache();
64
            }
65
66
            return new Response('ok', 200, [
67
                'Cache-Control' => 'no-cache, must-revalidate',
68
                'Content-Length' => 2, // to prevent chunked transfer encoding
69
            ]);
70
        }
71
72
        throw new AccessDeniedHttpException('Invalid token.');
73
    }
74
75
    protected function getUrl(): ?string
76
    {
77
        return $this->router->generate('sonata_cache_apc', ['token' => $this->token]);
78
    }
79
}
80