Passed
Push — master ( 176af1...83d554 )
by Alexander
02:16
created

MetadataManager::getMetadata()   C

Complexity

Conditions 10
Paths 16

Size

Total Lines 48
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 10.003

Importance

Changes 0
Metric Value
dl 0
loc 48
ccs 31
cts 32
cp 0.9688
rs 5.3454
c 0
b 0
f 0
cc 10
eloc 32
nc 16
nop 1
crap 10.003

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Flying\Struct\Metadata;
4
5
use Doctrine\Common\Cache\Cache;
6
use Flying\Struct\ConfigurationManager;
7
use Flying\Struct\StructInterface;
8
9
/**
10
 * Structures metadata manager
11
 */
12
class MetadataManager implements MetadataManagerInterface
13
{
14
    /**
15
     * Structures metadata parser
16
     *
17
     * @var MetadataParserInterface
18
     */
19
    private $parser;
20
    /**
21
     * Structures metadata cache
22
     *
23
     * @var Cache
24
     */
25
    private $cache;
26
    /**
27
     * Structures metadata
28
     *
29
     * @var array
30
     */
31
    private $metadata = [];
32
    /**
33
     * Prefix for cache entries for structure metadata
34
     *
35
     * @var string
36
     */
37
    private $cachePrefix = 'StructMetadata_';
38
39
    /**
40
     * Get structure metadata information for given structure
41
     *
42
     * @param string|StructInterface $struct    Either structure class name or instance of structure object
43
     *                                          to get metadata for
44
     * @throws \InvalidArgumentException
45
     * @return StructMetadata|null
46
     */
47 188
    public function getMetadata($struct)
48
    {
49
        // Get class name of given structure
50 188
        if (is_object($struct)) {
51 163
            if ($struct instanceof StructInterface) {
52 162
                $class = get_class($struct);
53 162
            } else {
54 1
                throw new \InvalidArgumentException('Structure class must implement StructInterface interface');
55
            }
56 187
        } elseif (is_string($struct)) {
57
            try {
58 183
                $reflection = new \ReflectionClass($struct);
59 183
            } catch (\ReflectionException $e) {
60
                throw new \InvalidArgumentException('Invalid structure class "' . $struct . '"');
61
            }
62 183
            if (in_array(StructInterface::class, $reflection->getInterfaceNames(), true)) {
63 182
                $class = $reflection->name;
64 182
            } else {
65 1
                throw new \InvalidArgumentException('Structure class must implement StructInterface interface');
66
            }
67 182
        } else {
68 4
            throw new \InvalidArgumentException('Invalid structure information is given');
69
        }
70
        // Check local metadata storage - fastest possible way
71 182
        if (array_key_exists($class, $this->metadata)) {
72 67
            return clone $this->metadata[$class];
73
        }
74
        // Check metadata cache
75 182
        $cacheKey = $this->getCacheKey($class);
76 182
        if ($this->getCache()->contains($cacheKey)) {
77 2
            $cachedMetadata = $this->getCache()->fetch($cacheKey);
78 2
            if ($cachedMetadata instanceof StructMetadata) {
79 1
                $this->metadata[$class] = $cachedMetadata;
80 1
                return clone $cachedMetadata;
81
            }
82
            // Cache has incorrect or corrupted entry
83 1
            $this->getCache()->delete($cacheKey);
84 1
        }
85
        // Get metadata from parser
86 182
        $metadata = $this->getParser()->getMetadata($class);
87 182
        if ($metadata instanceof StructMetadata) {
88 181
            $this->metadata[$class] = $metadata;
89 181
            $this->getCache()->save($cacheKey, $metadata);
90 181
            return clone $metadata;
91
        }
92
        // No metadata is found for structure
93 1
        return null;
94
    }
95
96
    /**
97
     * Get cache key for given class
98
     *
99
     * @param string $class
100
     * @return string
101
     */
102 182
    protected function getCacheKey($class)
103
    {
104 182
        return $this->cachePrefix . str_replace('\\', '_', $class);
105
    }
106
107
    /**
108
     * Get metadata cache
109
     *
110
     * @return Cache
111
     */
112 184
    public function getCache()
113
    {
114 184
        if (!$this->cache) {
115 182
            $this->cache = ConfigurationManager::getConfiguration()->getCache();
116 182
        }
117 184
        return $this->cache;
118
    }
119
120
    /**
121
     * Set metadata cache
122
     *
123
     * @param Cache $cache
124
     * @return $this
125
     */
126 6
    public function setCache(Cache $cache)
127
    {
128 6
        $this->cache = $cache;
129 6
        return $this;
130
    }
131
132
    /**
133
     * Get metadata parser
134
     *
135
     * @return MetadataParserInterface
136
     */
137 185
    public function getParser()
138
    {
139 185
        if (!$this->parser) {
140 183
            $this->parser = ConfigurationManager::getConfiguration()->getMetadataParser();
141 183
            if ($this->parser instanceof AbstractMetadataParser) {
142 183
                $this->parser->setMetadataManager($this);
143 183
            }
144 183
        }
145 185
        return $this->parser;
146
    }
147
148
    /**
149
     * Set metadata parser
150
     *
151
     * @param MetadataParserInterface $parser
152
     * @return $this
153
     */
154 7
    public function setParser(MetadataParserInterface $parser)
155
    {
156 7
        $this->parser = $parser;
157 7
        if ($this->parser instanceof AbstractMetadataParser) {
158 2
            $this->parser->setMetadataManager($this);
159 2
        }
160 7
        return $this;
161
    }
162
}
163