Passed
Branch master (448315)
by Antarès
03:12 queued 23s
created

Configuration   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 5

Test Coverage

Coverage 95.92%

Importance

Changes 7
Bugs 0 Features 0
Metric Value
wmc 18
c 7
b 0
f 0
lcom 3
cbo 5
dl 0
loc 190
ccs 47
cts 49
cp 0.9592
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getAnnotationReader() 0 8 2
A setAnnotationReader() 0 4 1
A getConstraintsValidator() 0 10 2
A setConstraintsValidator() 0 4 1
A isInitializeValuesValidationEnabled() 0 4 1
A setInitializeValuesValidationEnabled() 0 8 2
A setCache() 0 14 4
A getCacheDriver() 0 4 1
A setCacheDriver() 0 4 1
A getArrayCache() 0 8 2
A setArrayCache() 0 3 1
1
<?php
2
3
namespace Accessible;
4
5
use Doctrine\Common\Annotations\Reader;
6
use Doctrine\Common\Annotations\AnnotationReader;
7
use Doctrine\Common\Annotations\CachedReader;
8
use Doctrine\Common\Cache\Cache;
9
use Doctrine\Common\Cache\ArrayCache;
10
use Symfony\Component\Validator\Validation;
11
use Symfony\Component\Validator\Validator\ValidatorInterface;
12
13
class Configuration
14
{
15
    /**
16
     * The annotations reader used to know the access.
17
     *
18
     * @var Doctrine\Common\Annotations\Reader
19
     */
20
    private static $annotationReader;
21
22
    /**
23
     * The constraints validator.
24
     *
25
     * @var Symfony\Component\Validator\ConstraintValidator
26
     */
27
    private static $constraintsValidator;
28
29
    /**
30
     * Says if the Initialize and InitializeObject values have to
31
     * be validated with constraints.
32
     *
33
     * @var bool
34
     */
35
    private static $initializeValuesValidationEnabled = true;
36
37
    /**
38
     * The cache driver the library will use.
39
     *
40
     * @var Cache
41
     */
42
    private static $cacheDriver;
43
44
    /**
45
     * The namespace of the cache driver.
46
     *
47
     * @var string
48
     */
49
    private static $cacheDefaultNamespace = 'antares_accessible_';
50
51
    /**
52
     * The in-memory cache that will be used.
53
     * @var ArrayCache
54
     */
55
    private static $arrayCache;
56
57
    /**
58
     * Get the annotation reader that is used.
59
     * Initializes it if it doesn't already exists.
60
     *
61
     * @return Reader The annotation reader.
62
     */
63 30
    public static function getAnnotationReader()
64
    {
65 30
        if (self::$annotationReader === null) {
66 1
            self::$annotationReader = new CachedReader(new AnnotationReader(), new ArrayCache());
67 1
        }
68
69 30
        return self::$annotationReader;
70
    }
71
72
    /**
73
     * Set the annotation reader that will be used.
74
     *
75
     * @param Reader $annotationReader The annotation reader.
76
     */
77 1
    public static function setAnnotationReader(Reader $annotationReader)
78
    {
79 1
        self::$annotationReader = $annotationReader;
80 1
    }
81
82
    /**
83
     * Get the constraints validator that is used.
84
     * Initializes it if it doesn't already exists.
85
     *
86
     * @return ConstraintValidator The annotation reader.
87
     */
88 30
    public static function getConstraintsValidator()
89
    {
90 30
        if (self::$constraintsValidator === null) {
91 1
            self::$constraintsValidator = Validation::createValidatorBuilder()
92 1
                ->enableAnnotationMapping(self::getAnnotationReader())
93 1
                ->getValidator();
94 1
        }
95
96 30
        return self::$constraintsValidator;
97
    }
98
99
    /**
100
     * Set the constraints validator that will be used.
101
     *
102
     * @param ValidatorInterface $constraintsValidator The annotation reader.
103
     */
104 1
    public static function setConstraintsValidator(ValidatorInterface $constraintsValidator)
105
    {
106 1
        self::$constraintsValidator = $constraintsValidator;
107 1
    }
108
109
    /**
110
     * Indicates if the constraints validation for Initialize and
111
     * InitializeObject values is enabled or not.
112
     *
113
     * @return boolean True if enabled, else false.
114
     */
115 30
    public static function isInitializeValuesValidationEnabled()
116
    {
117 30
        return self::$initializeValuesValidationEnabled;
118
    }
119
120
    /**
121
     * Enable or disable the constraints validation for Initialize and
122
     * InitializeObject values.
123
     *
124
     * @param bool $enabled True for enable, false for disable.
125
     */
126 1
    public static function setInitializeValuesValidationEnabled($enabled)
127
    {
128 1
        if (!is_bool($enabled)) {
129
            throw new \InvalidArgumentException("This value must be a boolean.");
130
        }
131
132 1
        self::$initializeValuesValidationEnabled = $enabled;
133 1
    }
134
135
    /**
136
     * Set a cache driver.
137
     *
138
     * @param Cache  $cacheToChange The cache to change.
139
     * @param Cache  $cache The cache driver.
140
     * @param string $namespace The cache namespace.
141
     */
142 3
    private static function setCache(Cache &$cacheToChange = null, Cache $cache = null, $namespace = null)
143
    {
144 3
        if ($namespace === null) {
145 3
            $namespace = self::$cacheDefaultNamespace;
146 3
        }
147 3
        if (!is_string($namespace)) {
148
            throw new \InvalidArgumentException("The namespace must be a string.");
149
        }
150
151 3
        $cacheToChange = $cache;
152 3
        if ($cache !== null) {
153 3
            $cacheToChange->setNamespace($namespace);
154 3
        }
155 3
    }
156
157
    /**
158
    * Get the cache driver that will be used.
159
    *
160
    * @return Cache The cache driver.
161
    */
162 29
    public static function getCacheDriver()
163
    {
164 29
        return self::$cacheDriver;
165
    }
166
167
    /**
168
     * Set the cache driver that will be used.
169
     *
170
     * @param Cache  $cache The cache driver.
171
     * @param string $namespace The cache namespace.
172
     */
173 1
    public static function setCacheDriver(Cache $cache = null, $namespace = null)
174
    {
175 1
        self::setCache(self::$cacheDriver, $cache, $namespace);
176 1
    }
177
178
    /**
179
     * Get the array cache that will be used.
180
     * Initialize it if it doesn't already exist.
181
     *
182
     * @return Cache The cache driver.
183
     */
184 30
    public static function getArrayCache()
185
    {
186 30
        if (self::$arrayCache === null) {
187 1
            self::setArrayCache(new ArrayCache());
188 1
        }
189
190 30
        return self::$arrayCache;
191
    }
192
193
    /**
194
     * Set the array cache that will be used.
195
     *
196
     * @param Cache  $cache The cache driver.
197
     * @param string $namespace The cache namespace.
198
     */
199 2
    public static function setArrayCache(Cache $cache = null, $namespace = null) {
200 2
        self::setCache(self::$arrayCache, $cache, $namespace);
201 2
    }
202
}
203