Passed
Branch master (6b6e1c)
by Menno
02:03
created

DefinitionChain::getPrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Codefocus\ManagedCache;
4
5
use Illuminate\Cache\TaggedCache;
6
use Illuminate\Contracts\Cache\Store as StoreContract;
7
8
class DefinitionChain implements StoreContract
9
{
10
    protected $managedCache = null;
11
12
    protected $store = null;
13
14
    protected $conditions = [];
15
16
    protected $conditionTags = null;
17
18
    /**
19
     * Constructor.
20
     *
21
     * @param ManagedCache $managedCache
22
     */
23
    public function __construct(ManagedCache $managedCache)
24
    {
25
        $this->managedCache = $managedCache;
26
        $this->store = $managedCache->getStore();
27
    }
28
29
    /**
30
     * Sets the array of Conditions that trigger the cache key to get flushed.
31
     *
32
     * @param array $conditions An array of Condition instances
33
     *
34
     * @return self
35
     */
36
    public function forgetWhen(array $conditions): self
37
    {
38
        $this->conditions = $conditions;
39
        $this->conditionTags = null;
40
41
        // debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 4);
42
43
        return $this;
44
    }
45
46
    /**
47
     * Adds a Condition that triggers the cache key to get flushed.
48
     *
49
     * @param Condition $condition
50
     *
51
     * @return self
52
     */
53
    public function addCondition(Condition $condition): self
54
    {
55
        $this->conditions[] = $condition;
56
        $this->conditionTags = null;
57
58
        return $this;
59
    }
60
61
    /**
62
     * Adds an array of Conditions that trigger the cache key to get flushed.
63
     *
64
     * @param array $conditions An array of Condition instances
65
     *
66
     * @return self
67
     */
68
    public function addConditions(array $conditions): self
69
    {
70
        $this->conditions += $conditions;
71
        $this->conditionTags = null;
72
73
        return $this;
74
    }
75
76
    /**
77
     * Returns an array of Condition instances.
78
     *
79
     * @return array
80
     */
81
    public function getConditions(): array
82
    {
83
        return $this->conditions;
84
    }
85
86
    /**
87
     * Return an array of cache tags generated from our Conditions.
88
     *
89
     * @return array
90
     */
91
    public function getConditionTags(): array
92
    {
93
        if (empty($this->conditionTags)) {
94
            $tags = [];
95
            foreach ($this->conditions as $condition) {
96
                $tags[] = (string) $condition;
97
            }
98
            //  @TODO:  Remove this.
99
            //          Potentially replace with a call to ManagedCache::log()
100
            if ($this->managedCache->isDebugModeEnabled()) {
101
                dump($tags);
102
            }
103
            $this->conditionTags = $tags;
104
        }
105
106
        return $this->conditionTags;
107
    }
108
109
    /**
110
     * Return the cache store, after applying our conditions to it, as tags.
111
     *
112
     * @param ?array $tags
113
     *
114
     * @return TaggedCache
115
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment ?array at position 0 could not be parsed: Unknown type name '?array' at position 0 in ?array.
Loading history...
116
    public function getTaggedStore(?array $tags = null): TaggedCache
117
    {
118
        if ($tags !== null) {
119
            return $this->store->tags($tags);
120
        }
121
122
        return $this->store->tags($this->getConditionTags());
123
    }
124
125
    /**
126
     * @inheritdoc
127
     */
128
    public function get($key, $default = null)
129
    {
130
        $value = $this->getTaggedStore($this->managedCache->getTagsForKey($key))->get($key);
131
        // If we could not find the cache value, we will get the default value
132
        // for this cache key. This default may be a callback.
133
        if (is_null($value)) {
134
            $value = value($default);
135
        }
136
137
        return $value;
138
    }
139
140
    /**
141
     * @inheritdoc
142
     */
143
    public function many(array $keys)
144
    {
145
        //  @TODO:  $this->managedCache->getTagsForKeys plural
146
        return $this->getTaggedStore()->many($keys);
147
    }
148
149
    /**
150
     * @inheritdoc
151
     */
152
    public function put($key, $value, $minutes)
153
    {
154
        //  Store the cache tags for this key,
155
        //  so that we can GET it without specifying the tags.
156
        $this->managedCache->setTagsForKey($key, $this->getConditionTags());
157
158
        return $this->getTaggedStore()->put($key, $value, $minutes);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getTaggedStore()-...$key, $value, $minutes) targeting Illuminate\Cache\Repository::put() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
159
    }
160
161
    /**
162
     * @inheritdoc
163
     */
164
    public function putMany(array $values, $minutes)
165
    {
166
        //  @TODO:Store tags for keys plural.
167
        return $this->getTaggedStore()->putMany($values, $minutes);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getTaggedStore()-...Many($values, $minutes) targeting Illuminate\Cache\TaggedCache::putMany() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
168
    }
169
170
    /**
171
     * @inheritdoc
172
     */
173
    public function increment($key, $value = 1)
174
    {
175
        return $this->getTaggedStore()->increment($key, $value);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getTaggedS...increment($key, $value) returns the type void which is incompatible with the return type mandated by Illuminate\Contracts\Cache\Store::increment() of integer|boolean.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
Bug introduced by
Are you sure the usage of $this->getTaggedStore()->increment($key, $value) targeting Illuminate\Cache\TaggedCache::increment() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
176
    }
177
178
    /**
179
     * @inheritdoc
180
     */
181
    public function decrement($key, $value = 1)
182
    {
183
        return $this->getTaggedStore()->decrement($key, $value);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getTaggedS...decrement($key, $value) returns the type void which is incompatible with the return type mandated by Illuminate\Contracts\Cache\Store::decrement() of integer|boolean.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
Bug introduced by
Are you sure the usage of $this->getTaggedStore()->decrement($key, $value) targeting Illuminate\Cache\TaggedCache::decrement() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
184
    }
185
186
    /**
187
     * @inheritdoc
188
     */
189
    public function forever($key, $value)
190
    {
191
        return $this->getTaggedStore()->forever($key, $value);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getTaggedStore()->forever($key, $value) targeting Illuminate\Cache\Repository::forever() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
192
    }
193
194
    /**
195
     * @inheritdoc
196
     */
197
    public function forget($key)
198
    {
199
        $this->managedCache->deleteTagsForKey($key);
200
201
        return $this->getTaggedStore()->forget($key);
202
    }
203
204
    /**
205
     * @inheritdoc
206
     */
207
    public function flush()
208
    {
209
        return $this->getTaggedStore()->flush();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getTaggedStore()->flush() targeting Illuminate\Cache\TaggedCache::flush() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug Best Practice introduced by
The expression return $this->getTaggedStore()->flush() returns the type void which is incompatible with the return type mandated by Illuminate\Contracts\Cache\Store::flush() of boolean.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
210
    }
211
212
    /**
213
     * @inheritdoc
214
     */
215
    public function getPrefix()
216
    {
217
        return $this->getTaggedStore()->getPrefix();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getTaggedStore()->getPrefix() also could return the type mixed which includes types incompatible with the return type mandated by Illuminate\Contracts\Cache\Store::getPrefix() of string. Consider adding a type-check to rule them out.
Loading history...
218
    }
219
}
220