CachedConfigCollection::getFlush()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace SilverStripe\Config\Collections;
4
5
use BadMethodCallException;
6
use Psr\SimpleCache\CacheInterface;
7
use SilverStripe\Config\Middleware\MiddlewareAware;
8
9
class CachedConfigCollection implements ConfigCollectionInterface
10
{
11
    use MiddlewareAware;
12
13
    /**
14
     * @const string
15
     */
16
    const CACHE_KEY = '__CACHE__';
17
18
    /**
19
     * @var CacheInterface
20
     */
21
    protected $cache;
22
23
    /**
24
     * Nested config to delegate to
25
     *
26
     * @var ConfigCollectionInterface
27
     */
28
    protected $collection;
29
30
    /**
31
     * @var callable
32
     */
33
    protected $collectionCreator;
34
35
    /**
36
     * @var bool
37
     */
38
    protected $flush = false;
39
40
    /**
41
     * Set to true while building config.
42
     * Used to protect against infinite loops.
43
     *
44
     * @var bool
45
     */
46
    protected $building = false;
47
48
    /**
49
     * Injectable factory for nesting config.
50
     * This callback will be passed the inner ConfigCollection
51
     *
52
     * @var callable
53
     */
54
    protected $nestFactory = null;
55
56
    /**
57
     * @return static
58
     */
59
    public static function create()
60
    {
61
        return new static();
62
    }
63
64
    /**
65
     * Get callback for nesting the inner collection
66
     *
67
     * @return callable
68
     */
69
    public function getNestFactory()
70
    {
71
        return $this->nestFactory;
72
    }
73
74
    /**
75
     * Set callback for nesting the inner collection
76
     *
77
     * @param callable $factory
78
     * @return $this
79
     */
80
    public function setNestFactory(callable $factory)
81
    {
82
        $this->nestFactory = $factory;
83
        return $this;
84
    }
85
86 2
    public function get($class, $name = null, $excludeMiddleware = 0)
87
    {
88 2
        return $this->getCollection()->get($class, $name, $excludeMiddleware);
89
    }
90
91
    public function getAll()
92
    {
93
        return $this->getCollection()->getAll();
94
    }
95
96 2
    public function exists($class, $name = null, $excludeMiddleware = 0)
97
    {
98 2
        return $this->getCollection()->exists($class, $name, $excludeMiddleware);
99
    }
100
101
    public function getMetadata()
102
    {
103
        return $this->getCollection()->getMetadata();
104
    }
105
106
    public function getHistory()
107
    {
108
        return $this->getCollection()->getHistory();
109
    }
110
111
    /**
112
     * Get or build collection
113
     *
114
     * @return ConfigCollectionInterface
115
     */
116 3
    public function getCollection()
117
    {
118
        // Get current collection
119 3
        if ($this->collection) {
120 2
            return $this->collection;
121
        }
122
123
        // Load from cache (unless flushing)
124 3
        if (!$this->flush) {
125 3
            $this->collection = $this->cache->get(self::CACHE_KEY);
126 3
            if ($this->collection) {
127 1
                return $this->collection;
128
            }
129 2
        }
130
131
        // Protect against infinity loop
132 2
        if ($this->building) {
133 1
            throw new BadMethodCallException("Infinite loop detected. Config could not be bootstrapped.");
134
        }
135 2
        $this->building = true;
136
137
        // Cache missed
138
        try {
139 2
            $this->collection = call_user_func($this->collectionCreator);
140 1
        } finally {
141 2
            $this->building = false;
142 2
        }
143
144
        // Save immediately.
145
        // Note additional deferred save will occur in _destruct()
146 1
        $this->cache->set(self::CACHE_KEY, $this->collection);
147 1
        return $this->collection;
148
    }
149
150
    /**
151
     * Commits the cache
152
     */
153 2
    public function __destruct()
154
    {
155
        // Ensure back-end cache is updated
156 2
        if ($this->collection) {
157 2
            $this->cache->set(self::CACHE_KEY, $this->collection);
158
159
            // Prevent double-destruct
160 2
            $this->collection = null;
161 2
        }
162 2
    }
163
164
    public function nest()
165
    {
166
        $collection = $this->getCollection();
167
        $factory = $this->getNestFactory();
168
        if ($factory) {
169
            return $factory($collection);
170
        }
171
172
        // Fall back to regular nest
173
        return $collection->nest();
174
    }
175
176
    /**
177
     * Set a PSR-16 cache
178
     *
179
     * @param CacheInterface $cache
180
     * @return $this
181
     */
182 3
    public function setCache(CacheInterface $cache)
183
    {
184 3
        $this->cache = $cache;
185 3
        if ($this->flush) {
186
            $cache->clear();
187
        }
188 3
        return $this;
189
    }
190
191
    /**
192
     * @param callable $collectionCreator
193
     * @return $this
194
     */
195 3
    public function setCollectionCreator($collectionCreator)
196
    {
197 3
        $this->collectionCreator = $collectionCreator;
198 3
        return $this;
199
    }
200
201
    /**
202
     * @return callable
203
     */
204
    public function getCollectionCreator()
205
    {
206
        return $this->collectionCreator;
207
    }
208
209
    /**
210
     * @return CacheInterface
211
     */
212
    public function getCache()
213
    {
214
        return $this->cache;
215
    }
216
217
    /**
218
     * @param bool $flush
219
     * @return $this
220
     */
221
    public function setFlush($flush)
222
    {
223
        $this->flush = $flush;
224
        if ($flush && $this->cache) {
225
            $this->cache->clear();
226
        }
227
        return $this;
228
    }
229
230
    /**
231
     * @return bool
232
     */
233
    public function getFlush()
234
    {
235
        return $this->flush;
236
    }
237
238
    public function setMiddlewares($middlewares)
239
    {
240
        throw new BadMethodCallException(
241
            "Please apply middleware to collection factory via setCollectionCreator()"
242
        );
243
    }
244
245
    /**
246
     * @deprecated 4.0...5.0 Please use YAML configuration, ::modify()->set() or ::modify()->merge()
247
     * @throws BadMethodCallException
248
     */
249
    public function update($class, $name, $value)
0 ignored issues
show
Unused Code introduced by
The parameter $class is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
250
    {
251
        throw new BadMethodCallException(
252
            'Config::inst()->update() is deprecated. Please use YAML configuration, Config::modify()->merge() or ->set()'
253
        );
254
    }
255
}
256