AuthorizationCacheWarmer   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 29
dl 0
loc 97
ccs 33
cts 33
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A isOptional() 0 3 1
A __construct() 0 8 1
A clear() 0 5 1
A commit() 0 5 1
A saveDeferred() 0 7 1
A warmUp() 0 14 2
1
<?php
2
3
/*
4
 *
5
 * (c) Yaroslav Honcharuk <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Yarhon\RouteGuardBundle\Cache;
12
13
use Psr\Cache\CacheItemPoolInterface;
14
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
15
use Symfony\Component\Routing\RouterInterface;
16
use Symfony\Component\Routing\RouteCollection;
17
use Yarhon\RouteGuardBundle\Cache\DataCollector\RouteCollectionDataCollector;
18
19
/**
20
 * @author Yaroslav Honcharuk <[email protected]>
21
 */
22
class AuthorizationCacheWarmer implements CacheWarmerInterface
23
{
24
    /**
25
     * @var RouteCollectionDataCollector
26
     */
27
    private $dataCollector;
28
29
    /**
30
     * @var RouteCollection
31
     */
32
    private $routeCollection;
33
34
    /**
35
     * @var CacheItemPoolInterface
36
     */
37
    private $testsCache;
38
39
    /**
40
     * @var CacheItemPoolInterface
41
     */
42
    private $controllerMetadataCache;
43
44
    /**
45
     * @var CacheItemPoolInterface
46
     */
47
    private $routeMetadataCache;
48
49
    /**
50
     * @param RouteCollectionDataCollector $dataCollector
51
     * @param RouterInterface              $router
52
     * @param CacheItemPoolInterface       $testsCache
53
     * @param CacheItemPoolInterface       $controllerMetadataCache
54
     * @param CacheItemPoolInterface       $routeMetadataCache
55
     */
56 21
    public function __construct(RouteCollectionDataCollector $dataCollector, RouterInterface $router, CacheItemPoolInterface $testsCache, CacheItemPoolInterface $controllerMetadataCache, CacheItemPoolInterface $routeMetadataCache)
57
    {
58 21
        $this->dataCollector = $dataCollector;
59 21
        $this->routeCollection = $router->getRouteCollection();
60
61 21
        $this->testsCache = $testsCache;
62 21
        $this->controllerMetadataCache = $controllerMetadataCache;
63 21
        $this->routeMetadataCache = $routeMetadataCache;
64 21
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 19
    public function isOptional()
70
    {
71 19
        return false;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 20
    public function warmUp($cacheDir)
78
    {
79 20
        $this->clear();
80
81 20
        $data = $this->dataCollector->collect($this->routeCollection);
82
83 20
        foreach ($data as $routeName => list($tests, $controllerMetadata, $routeMetadata)) {
84 19
            $this->saveDeferred($this->testsCache, $routeName, $tests);
85 19
            $this->saveDeferred($this->controllerMetadataCache, $routeName, $controllerMetadata);
86 19
            $this->saveDeferred($this->routeMetadataCache, $routeName, $routeMetadata);
87
        }
88
89
        // Note: CacheAdapter saves cache items automatically on destruct, even without explicit "commit" call.
90 20
        $this->commit();
91 20
    }
92
93 20
    private function clear()
94
    {
95 20
        $this->testsCache->clear();
96 20
        $this->controllerMetadataCache->clear();
97 20
        $this->routeMetadataCache->clear();
98 20
    }
99
100 20
    private function commit()
101
    {
102 20
        $this->testsCache->commit();
103 20
        $this->controllerMetadataCache->commit();
104 20
        $this->routeMetadataCache->commit();
105 20
    }
106
107
    /**
108
     * @param CacheItemPoolInterface $cache
109
     * @param string                 $routeName
110
     * @param mixed                  $item
111
     */
112 19
    private function saveDeferred(CacheItemPoolInterface $cache, $routeName, $item)
113
    {
114 19
        $cacheKey = CacheFactory::getValidCacheKey($routeName);
115
116 19
        $cacheItem = $cache->getItem($cacheKey);
117 19
        $cacheItem->set($item);
118 19
        $cache->saveDeferred($cacheItem);
119 19
    }
120
}
121