Completed
Push — master ( ede4af...ce4315 )
by Chauncey
02:23
created

CachePoolFacade::setDefaultTtl()   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 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Charcoal\Cache\Facade;
4
5
use InvalidArgumentException;
6
7
// From PSR-6
8
use Psr\Cache\CacheItemInterface;
9
10
// From 'charcoal-cache'
11
use Charcoal\Cache\CacheConfig;
12
use Charcoal\Cache\CachePoolAwareTrait;
13
14
/**
15
 * Cache Pool Facade
16
 *
17
 * The facade provides a simpler interface to work with the cache pool.
18
 */
19
class CachePoolFacade
20
{
21
    use CachePoolAwareTrait;
22
23
    /**
24
     * Default maximum time an item will be cached.
25
     *
26
     * @var mixed
27
     */
28
    private $defaultTtl = CacheConfig::HOUR_IN_SECONDS;
29
30
    /**
31
     * Create a cache pool facade.
32
     *
33
     * @param array $data The facade dependencies.
34
     */
35
    public function __construct(array $data)
36
    {
37
        $this->setCachePool($data['cache']);
38
39
        if (isset($data['default_ttl'])) {
40
            $this->setDefaultTtl($data['default_ttl']);
41
        }
42
    }
43
44
    /**
45
     * Retrieve the value associated with the specified key from the pool.
46
     *
47
     * This method will call $lambada if the cache item representing $key resulted in a cache miss.
48
     *
49
     * @param  string        $key     The key for which to return the associated value.
50
     * @param  callable|null $resolve The function to execute if the cached value does not exist
51
     *     or is considered expired. The function must return a value which will be stored
52
     *     in the cache before being returned by the method.
53
     * @param  integer|null  $ttl     The time-to-live, in seconds, after which the cached value
54
     *     must be considered expired.
55
     *     If NULL, the facade's default time-to-live is used.
56
     * @return mixed The value corresponding to this cache item's $key, or NULL if not found.
57
     */
58
    public function get($key, callable $resolve = null, $ttl = null)
59
    {
60
        $pool = $this->cachePool();
61
        $item = $pool->getItem($key);
62
        $data = $item->get();
63
64
        if ($item->isHit()) {
65
            return $data;
66
        }
67
68
        if (is_callable($resolve)) {
69
            $item->lock();
0 ignored issues
show
Bug introduced by
The method lock() does not exist on Psr\Cache\CacheItemInterface. It seems like you code against a sub-type of Psr\Cache\CacheItemInterface such as Stash\Interfaces\ItemInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
            $item->/** @scrutinizer ignore-call */ 
70
                   lock();
Loading history...
70
            $data = call_user_func($resolve);
71
            $this->save($item, $data, $ttl);
72
            return $data;
73
        }
74
75
        return null;
76
    }
77
78
    /**
79
     * Determine if the specified key results in a cache hit.
80
     *
81
     * @param  string $key The key for which to check existence.
82
     * @return boolean TRUE if item exists in the cache, FALSE otherwise.
83
     */
84
    public function has($key)
85
    {
86
        return $this->cachePool()->getItem($key)->isHit();
87
    }
88
89
    /**
90
     * Store a value with the specified key to be saved immediately.
91
     *
92
     * @param  string       $key   The key to save the value on.
93
     * @param  mixed        $value The serializable value to be stored.
94
     * @param  integer|null $ttl   The time-to-live, in seconds.
95
     * @return boolean TRUE if the item was successfully persisted. FALSE if there was an error.
96
     */
97
    public function set($key, $value, $ttl = null)
98
    {
99
        $item = $this->cachePool()->getItem($key);
100
101
        return $this->save($item, $value, $ttl);
102
    }
103
104
    /**
105
     * Set a value on a cache item to be saved immediately.
106
     *
107
     * @param  CacheItemInterface $item  The cache item to save.
108
     * @param  mixed              $value The serializable value to be stored.
109
     * @param  integer|null       $ttl   The time-to-live, in seconds.
110
     * @return boolean TRUE if the item was successfully persisted. FALSE if there was an error.
111
     */
112
    protected function save(CacheItemInterface $item, $value, $ttl = null)
113
    {
114
        if (!$ttl) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $ttl of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
115
            $ttl = $this->defaultTtl();
116
        }
117
118
        return $item->setTTL($ttl)
0 ignored issues
show
Bug introduced by
The method setTTL() does not exist on Psr\Cache\CacheItemInterface. It seems like you code against a sub-type of Psr\Cache\CacheItemInterface such as Stash\Interfaces\ItemInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

118
        return $item->/** @scrutinizer ignore-call */ setTTL($ttl)
Loading history...
119
                    ->set($value)
120
                    ->save();
121
    }
122
123
    /**
124
     * Removes one or more items from the pool.
125
     *
126
     * @param  string[] ...$keys One or many keys to delete.
127
     * @return bool TRUE if the item was successfully removed. FALSE if there was an error.
128
     */
129
    public function delete(...$keys)
130
    {
131
        $pool = $this->cachePool();
132
133
        $results = true;
134
        foreach ($keys as $key) {
135
            $results = $pool->deleteItem($key) && $results;
136
        }
137
138
        return $results;
139
    }
140
141
    /**
142
     * Retrieve the facade's default time-to-live for cached items.
143
     *
144
     * @return mixed An integer, date interval, or date.
145
     */
146
    public function defaultTtl()
147
    {
148
        return $this->defaultTtl;
149
    }
150
151
    /**
152
     * Set the facade's default time-to-live for cached items.
153
     *
154
     * @see \Stash\Item::setTTL()
155
     *
156
     * @param  mixed $ttl An integer, date interval, or date.
157
     * @return void
158
     */
159
    public function setDefaultTtl($ttl)
160
    {
161
        $this->defaultTtl = $ttl;
162
    }
163
}
164