Passed
Pull Request — 2.4 (#2650)
by Han Hui
03:13
created

CachePoolClearerCacheWarmer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 30
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A warmUp() 0 5 3
A isOptional() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\CacheWarmer;
15
16
use Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer;
17
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
18
19
/**
20
 * Clears the cache pools when warming up the cache.
21
 *
22
 * Do not use in production!
23
 *
24
 * @internal
25
 */
26
final class CachePoolClearerCacheWarmer implements CacheWarmerInterface
27
{
28
    private $poolClearer;
29
    private $pools;
30
31
    public function __construct(Psr6CacheClearer $poolClearer, array $pools = [])
32
    {
33
        $this->poolClearer = $poolClearer;
34
        $this->pools = $pools;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function warmUp($cacheDirectory): void
41
    {
42
        foreach ($this->pools as $pool) {
43
            if ($this->poolClearer->hasPool($pool)) {
44
                $this->poolClearer->clearPool($pool);
45
            }
46
        }
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function isOptional(): bool
53
    {
54
        // optional cache warmers are not run when handling the request
55
        return false;
56
    }
57
}
58