CachedDereferencer   A
last analyzed

Coupling/Cohesion

Components 1
Dependencies 5

Complexity

Total Complexity 13

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Test Coverage

Coverage 88.57%

Importance

Changes 0
Metric Value
dl 0
loc 101
ccs 31
cts 35
cp 0.8857
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 5

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A dereference() 0 8 1
A remember() 0 12 2
A key() 0 8 3
A cacheLoaders() 0 7 2
A getLoaderManager() 0 4 1
A getScopeResolver() 0 4 1
A getReferenceSerializer() 0 4 1
1
<?php
2
3
namespace League\JsonReference;
4
5
use Closure;
6
use League\JsonReference\Loader\CachedLoader;
7
use Psr\SimpleCache\CacheInterface;
8
9
final class CachedDereferencer implements DereferencerInterface
10
{
11
    /**
12
     * @var Dereferencer
13
     */
14
    private $dereferencer;
15
16
    /**
17
     * @var CacheInterface
18
     */
19
    private $cache;
20
21
    /**
22
     * @param Dereferencer   $dereferencer
23
     * @param CacheInterface $cache
24
     * @param bool           $cacheLoaders
25
     */
26 8
    public function __construct(Dereferencer $dereferencer, CacheInterface $cache, $cacheLoaders = true)
27
    {
28 8
        $this->dereferencer = $dereferencer;
29 8
        $this->cache        = $cache;
30
31 8
        if ($cacheLoaders) {
32 8
            $this->cacheLoaders();
33 4
        }
34
35 8
        Reference::setDereferencerInstance($this);
36 8
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 8
    public function dereference($schema, $uri = '')
42
    {
43 8
        $key = self::key($schema, $uri);
44
45 8
        return $this->remember($key, function () use ($schema, $uri) {
46 6
            return $this->dereferencer->dereference($schema, $uri);
47 8
        });
48
    }
49
50
    /**
51
     * @param string  $key
52
     * @param Closure $callback
53
     *
54
     * @return object
55
     */
56 8
    private function remember($key, Closure $callback)
57
    {
58 8
        $value = $this->cache->get($key);
59
60 8
        if ($value !== null) {
61 2
            return $value;
62
        }
63
64 6
        $this->cache->set($key, $value = $callback());
65
66 6
        return $value;
67
    }
68
69
    /**
70
     * @param string|object $schema
71
     * @param string        $uri
72
     *
73
     * @return string
74
     */
75 8
    private static function key($schema, $uri)
76
    {
77 8
        if ($uri !== '') {
78 2
            return sha1($uri);
79
        }
80
81 6
        return sha1(is_string($schema) ? $schema : json_encode($schema));
82
    }
83
84
    /**
85
     * @return void
86
     */
87 8
    private function cacheLoaders()
88
    {
89 8
        $loaderManager = $this->dereferencer->getLoaderManager();
90 8
        foreach ($loaderManager->getLoaders() as $prefix => $loader) {
91 8
            $loaderManager->registerLoader($prefix, new CachedLoader($this->cache, $loader));
92 4
        }
93 8
    }
94
95
    public function getLoaderManager()
96
    {
97
        return $this->dereferencer->getLoaderManager();
98
    }
99
100
    public function getScopeResolver()
101
    {
102
        return $this->dereferencer->getScopeResolver();
103
    }
104
105 2
    public function getReferenceSerializer()
106
    {
107 2
        return $this->dereferencer->getReferenceSerializer();
108
    }
109
}
110