CachedManifestLoader   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 50
ccs 14
cts 15
cp 0.9333
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A load() 0 13 2
A getPath() 0 4 1
1
<?php
2
3
namespace Rj\FrontendBundle\Manifest\Loader;
4
5
use Rj\FrontendBundle\Manifest\Manifest;
6
use Symfony\Component\Config\ConfigCache;
7
use Symfony\Component\Config\Resource\FileResource;
8
9
class CachedManifestLoader implements ManifestLoaderInterface
10
{
11
    /**
12
     * @var ManifestLoaderInterface
13
     */
14
    private $loader;
15
16
    /**
17
     * @var ConfigCache
18
     */
19
    private $cache;
20
21
    /**
22
     * @param string                  $cacheDir
23
     * @param bool                    $debug
24
     * @param ManifestLoaderInterface $loader
25
     */
26 3
    public function __construct($cacheDir, $debug, ManifestLoaderInterface $loader)
27
    {
28 3
        $cachePath = $cacheDir.'/'.hash('sha1', $loader->getPath()).'.php.cache';
29
30 3
        $this->cache = new ConfigCache($cachePath, $debug);
31 3
        $this->loader = $loader;
32 3
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 3
    public function load()
38
    {
39 3
        if (!$this->cache->isFresh()) {
40 3
            $resource = new FileResource($this->getPath());
41 3
            $entries = $this->loader->load()->all();
42
43 3
            $this->cache->write(sprintf('<?php return %s;', var_export($entries, true)), [$resource]);
44
        } else {
45
            $entries = include $this->cache->getPath();
46
        }
47
48 3
        return new Manifest($entries);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 3
    public function getPath()
55
    {
56 3
        $this->loader->getPath();
57 3
    }
58
}
59