Completed
Pull Request — master (#205)
by
unknown
01:24
created

ApcCache::getUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
    <?php
0 ignored issues
show
Security Bug introduced by
It is not recommended to output anything before PHP's opening tag in non-template files.
Loading history...
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 21 and the first side effect is on line 1.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Namespace declaration statement has to be the very first statement in the script
Loading history...
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