CachedLoader::load()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace League\JsonReference\Loader;
4
5
use League\JsonReference\LoaderInterface;
6
use Psr\SimpleCache\CacheInterface;
7
8
final class CachedLoader implements LoaderInterface
9
{
10
    /**
11
     * @var CacheInterface
12
     */
13
    private $cache;
14
15
    /**
16
     * @var LoaderInterface
17
     */
18
    private $loader;
19
20
    /**
21
     * @param CacheInterface  $cache
22
     * @param LoaderInterface $loader
23
     */
24 12
    public function __construct(CacheInterface $cache, LoaderInterface $loader)
25
    {
26 12
        $this->cache  = $cache;
27 12
        $this->loader = $loader;
28 12
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 6
    public function load($path)
34
    {
35 6
        $key   = sha1($path);
36 6
        $value = $this->cache->get($key);
37
38 6
        if ($value !== null) {
39 2
            return $value;
40
        }
41
42 4
        $this->cache->set($key, $value = $this->loader->load($path));
43
44 4
        return $value;
45
    }
46
}
47