Passed
Push — master ( 2182b2...873fd5 )
by Pavel
05:21
created

Configuration::hasCacheConfigurationFor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Bankiru\Api\Doctrine;
4
5
use Bankiru\Api\Doctrine\Cache\CacheConfiguration;
6
use Bankiru\Api\Doctrine\Cache\CacheConfigurationInterface;
7
use Bankiru\Api\Doctrine\Type\TypeRegistryInterface;
8
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
9
use Psr\Cache\CacheItemPoolInterface;
10
use Psr\Log\LoggerInterface;
11
use Psr\Log\NullLogger;
12
13
class Configuration
14
{
15
    /** @var  EntityMetadataFactory */
16
    private $metadataFactory;
17
    /** @var  MappingDriver */
18
    private $driver;
19
    /** @var  ClientRegistryInterface */
20
    private $clientRegistry;
21
    /** @var  ApiFactoryRegistryInterface */
22
    private $factoryRegistry;
23
    /** @var  string */
24
    private $proxyDir;
25
    /** @var  string */
26
    private $proxyNamespace;
27
    /** @var bool */
28
    private $autogenerateProxies = true;
29
    /** @var  TypeRegistryInterface */
30
    private $typeRegistry;
31
    /** @var  array */
32
    private $cacheConfiguration = [];
33
    /** @var  CacheItemPoolInterface */
34
    private $apiCache;
35
    /** @var  LoggerInterface */
36
    private $apiCacheLogger;
37
    /** @var  CacheConfigurationInterface[] */
38
    private $cacheConfigurationCache = [];
39
40
    /**
41
     * Configuration constructor.
42
     */
43 28
    public function __construct()
44
    {
45 28
        $this->apiCacheLogger = new NullLogger();
46 28
    }
47
48
    /**
49
     * @return ClientRegistryInterface
50
     */
51 27
    public function getClientRegistry()
52
    {
53 27
        return $this->clientRegistry;
54
    }
55
56
    /**
57
     * @param ClientRegistryInterface $clientRegistry
58
     */
59 28
    public function setClientRegistry($clientRegistry)
60
    {
61 28
        $this->clientRegistry = $clientRegistry;
62 28
    }
63
64
    /**
65
     * @return ApiFactoryRegistryInterface
66
     */
67 26
    public function getFactoryRegistry()
68
    {
69 26
        return $this->factoryRegistry;
70
    }
71
72
    /**
73
     * @param ApiFactoryRegistryInterface $factoryRegistry
74
     */
75 28
    public function setFactoryRegistry(ApiFactoryRegistryInterface $factoryRegistry)
76
    {
77 28
        $this->factoryRegistry = $factoryRegistry;
78 28
    }
79
80
    /**
81
     * @return LoggerInterface
82
     */
83 1
    public function getApiCacheLogger()
84
    {
85 1
        return $this->apiCacheLogger;
86
    }
87
88
    /**
89
     * @param LoggerInterface $apiCacheLogger
90
     */
91 1
    public function setApiCacheLogger(LoggerInterface $apiCacheLogger = null)
92
    {
93 1
        $this->apiCacheLogger = $apiCacheLogger ?: new NullLogger();
94 1
    }
95
96
    /**
97
     * @return CacheItemPoolInterface|null
98
     */
99 1
    public function getApiCache()
100
    {
101 1
        return $this->apiCache;
102
    }
103
104
    /**
105
     * @param CacheItemPoolInterface|null $apiCache
106
     */
107 1
    public function setApiCache(CacheItemPoolInterface $apiCache = null)
108
    {
109 1
        $this->apiCache = $apiCache;
110 1
    }
111
112
    /**
113
     * Returns class cache configuration.
114
     *
115
     * Checks for parent classes recursively
116
     *
117
     * @param $class
118
     *
119
     * @return CacheConfigurationInterface
120
     */
121 26
    public function getCacheConfiguration($class)
122
    {
123 26
        if ($this->getMetadataFactory()->isTransient($class)) {
124
            return CacheConfiguration::disabled();
125
        }
126
127 26
        if (array_key_exists($class, $this->cacheConfigurationCache)) {
128 1
            return $this->cacheConfigurationCache[$class];
129
        }
130
131 26
        if ($this->hasCacheConfigurationFor($class)) {
132 1
            return $this->cacheConfigurationCache[$class] =
133 1
                CacheConfiguration::create($this->cacheConfiguration[$class]);
134
        }
135
136 26
        $metadata = $this->getMetadataFactory()->getMetadataFor($class);
137 26
        $parent   = $metadata->getReflectionClass()->getParentClass();
138 26
        while ($parent) {
139 13
            if ($this->hasCacheConfigurationFor($parent->getName())) {
140 1
                return $this->cacheConfigurationCache[$class] = $this->getCacheConfiguration($parent->getName());
141
            }
142
143 12
            $parent = $parent->getParentClass();
144 12
        }
145
146 25
        return $this->cacheConfigurationCache[$class] = CacheConfiguration::disabled();
147
    }
148
149
    /**
150
     * @param string $class
151
     * @param array  $options
152
     */
153 1
    public function setCacheConfiguration($class, array $options = null)
154
    {
155 1
        $this->cacheConfiguration[$class] = $options;
156
157 1
        if (null === $this->cacheConfiguration[$class]) {
158
            unset($this->cacheConfiguration[$class]);
159
        }
160 1
    }
161
162 26
    public function hasCacheConfigurationFor($class)
163
    {
164 26
        return array_key_exists($class, $this->cacheConfiguration);
165
    }
166
167
    /**
168
     *
169
     *
170
     * @param string                      $class
171
     * @param CacheConfigurationInterface $configuration
172
     */
173
    public function setCacheConfigurationInstance($class, CacheConfigurationInterface $configuration)
174
    {
175
        $this->cacheConfigurationCache[$class] = $configuration;
176
    }
177
178
    /**
179
     * @return TypeRegistryInterface
180
     */
181 26
    public function getTypeRegistry()
182
    {
183 26
        return $this->typeRegistry;
184
    }
185
186
    /**
187
     * @param TypeRegistryInterface $typeRegistry
188
     */
189 28
    public function setTypeRegistry(TypeRegistryInterface $typeRegistry)
190
    {
191 28
        $this->typeRegistry = $typeRegistry;
192 28
    }
193
194
    /**
195
     * @return string
196
     */
197 28
    public function getProxyDir()
198
    {
199 28
        return $this->proxyDir;
200
    }
201
202
    /**
203
     * @param string $proxyDir
204
     */
205 28
    public function setProxyDir($proxyDir)
206
    {
207 28
        $this->proxyDir = $proxyDir;
208 28
    }
209
210
    /**
211
     * @return string
212
     */
213 28
    public function getProxyNamespace()
214
    {
215 28
        return $this->proxyNamespace;
216
    }
217
218
    /**
219
     * @param string $proxyNamespace
220
     */
221 28
    public function setProxyNamespace($proxyNamespace)
222
    {
223 28
        $this->proxyNamespace = $proxyNamespace;
224 28
    }
225
226
    /**
227
     * @return boolean
228
     */
229 28
    public function isAutogenerateProxies()
230
    {
231 28
        return $this->autogenerateProxies;
232
    }
233
234
    /**
235
     * @param boolean $autogenerateProxies
236
     */
237
    public function setAutogenerateProxies($autogenerateProxies)
238
    {
239
        $this->autogenerateProxies = $autogenerateProxies;
240
    }
241
242
    /**
243
     * @return MappingDriver
244
     */
245 28
    public function getDriver()
246
    {
247 28
        return $this->driver;
248
    }
249
250
    /**
251
     * @param MappingDriver $driver
252
     */
253 28
    public function setDriver($driver)
254
    {
255 28
        $this->driver = $driver;
256 28
    }
257
258
    /**
259
     * @return EntityMetadataFactory
260
     */
261 28
    public function getMetadataFactory()
262
    {
263 28
        return $this->metadataFactory;
264
    }
265
266
    /**
267
     * @param string $metadataFactory
268
     */
269 28
    public function setMetadataFactory($metadataFactory)
270
    {
271 28
        $this->metadataFactory = $metadataFactory;
0 ignored issues
show
Documentation Bug introduced by
It seems like $metadataFactory of type string is incompatible with the declared type object<Bankiru\Api\Doctr...\EntityMetadataFactory> of property $metadataFactory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
272 28
    }
273
}
274