Passed
Push — master ( 7106e3...456ee0 )
by Menno
02:12
created

DefinitionChain::setForgetConditions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Codefocus\ManagedCache;
4
5
use Closure;
6
use Illuminate\Cache\TaggedCache;
7
use Illuminate\Contracts\Cache\Store as StoreContract;
8
9
class DefinitionChain implements StoreContract
10
{
11
    protected $managedCache = null;
12
13
    protected $store = null;
14
15
    /**
16
     * @var ConditionBuilder
17
     */
18
    protected $conditions = null;
19
20
    protected $conditionTags = null;
21
22
    /**
23
     * Constructor.
24
     *
25
     * @param ManagedCache $managedCache
26
     */
27
    public function __construct(ManagedCache $managedCache)
28
    {
29
        $this->managedCache = $managedCache;
30
        $this->store = $managedCache->getStore();
31
        $this->conditions = new ConditionBuilder();
32
    }
33
34
    /**
35
     * Sets the array of Conditions that trigger the cache key to get flushed.
36
     *
37
     * @param Closure $closure
38
     *
39
     * @return self
40
     */
41
    public function setForgetConditions(Closure $closure): self
42
    {
43
        $this->conditions = new ConditionBuilder();
44
        $closure($this->conditions);
45
        $this->conditionTags = null;
46
47
        return $this;
48
    }
49
50
    // /**
51
    //  * Sets the array of Conditions that trigger the cache key to get flushed.
52
    //  *
53
    //  * @param array $conditions An array of Condition instances
54
    //  *
55
    //  * @return self
56
    //  */
57
    // public function forgetWhen(array $conditions): self
58
    // {
59
    //     $this->conditions = $conditions;
60
    //     $this->conditionTags = null;
61
    //
62
    //     // debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 4);
63
    //
64
    //     return $this;
65
    // }
66
67
    /**
68
     * Adds one or more Conditions that triggers the cache key to get flushed.
69
     *
70
     * @param Closure $closure
71
     *
72
     * @return self
73
     */
74
    public function addForgetConditions(Closure $closure): self
75
    {
76
        $this->conditions = $closure($this->conditions);
77
        $this->conditionTags = null;
78
79
        return $this;
80
    }
81
82
    /**
83
     * Returns the ConditionBuilder.
84
     *
85
     * @return ConditionBuilder
86
     */
87
    public function getConditions(): ConditionBuilder
88
    {
89
        return $this->conditions;
90
    }
91
92
    /**
93
     * Return an array of cache tags generated from our Conditions.
94
     *
95
     * @return array
96
     */
97
    public function getConditionTags(): array
98
    {
99
        if (empty($this->conditionTags)) {
100
            $tags = [];
101
102
            dump($this->conditions);
103
            foreach ($this->conditions as $condition) {
104
                $tags[] = (string) $condition;
105
            }
106
            //  @TODO:  Remove this.
107
            //          Potentially replace with a call to ManagedCache::log()
108
            if ($this->managedCache->isDebugModeEnabled()) {
109
                dump($tags);
110
            }
111
            $this->conditionTags = $tags;
112
        }
113
114
        return $this->conditionTags;
115
    }
116
117
    /**
118
     * Return the cache store, after applying our conditions to it, as tags.
119
     *
120
     * @param array|null $tags (default: null)
121
     *
122
     * @return TaggedCache
123
     */
124
    public function getTaggedStore(?array $tags = null): TaggedCache
125
    {
126
        if ($tags !== null) {
127
            return $this->store->tags($tags);
128
        }
129
130
        return $this->store->tags($this->getConditionTags());
131
    }
132
133
    /**
134
     * @inheritdoc
135
     */
136
    public function get($key, $default = null)
137
    {
138
        $value = $this->getTaggedStore($this->managedCache->getTagsForKey($key))->get($key);
139
        // If we could not find the cache value, we will get the default value
140
        // for this cache key. This default may be a callback.
141
        if (is_null($value)) {
142
            $value = value($default);
143
        }
144
145
        return $value;
146
    }
147
148
    /**
149
     * @inheritdoc
150
     */
151
    public function many(array $keys)
152
    {
153
        //  @TODO:  $this->managedCache->getTagsForKeys plural
154
        return $this->getTaggedStore()->many($keys);
155
    }
156
157
    /**
158
     * @inheritdoc
159
     */
160
    public function put($key, $value, $minutes)
161
    {
162
        //  Store the cache tags for this key,
163
        //  so that we can GET it without specifying the tags.
164
        $this->managedCache->setTagsForKey($key, $this->getConditionTags());
165
        $this->getTaggedStore()->put($key, $value, $minutes);
166
    }
167
168
    /**
169
     * @inheritdoc
170
     */
171
    public function putMany(array $values, $minutes)
172
    {
173
        //  @TODO:Store tags for keys plural.
174
        $this->getTaggedStore()->putMany($values, $minutes);
175
    }
176
177
    /**
178
     * @inheritdoc
179
     */
180
    public function increment($key, $value = 1)
181
    {
182
        $this->getTaggedStore()->increment($key, $value);
183
    }
184
185
    /**
186
     * @inheritdoc
187
     */
188
    public function decrement($key, $value = 1)
189
    {
190
        $this->getTaggedStore()->decrement($key, $value);
191
    }
192
193
    /**
194
     * @inheritdoc
195
     */
196
    public function forever($key, $value)
197
    {
198
        $this->getTaggedStore()->forever($key, $value);
199
    }
200
201
    /**
202
     * @inheritdoc
203
     */
204
    public function forget($key)
205
    {
206
        $this->managedCache->deleteTagsForKey($key);
207
        $this->getTaggedStore()->forget($key);
208
    }
209
210
    /**
211
     * @inheritdoc
212
     */
213
    public function flush()
214
    {
215
        $this->getTaggedStore()->flush();
216
    }
217
218
    /**
219
     * @inheritdoc
220
     */
221
    public function getPrefix(): string
222
    {
223
        return (string) $this->getTaggedStore()->getPrefix();
224
    }
225
}
226