Completed
Push — master ( d7e3a7...9cdf20 )
by Gjero
02:31
created

Storage::offsetExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Pimf
4
 *
5
 * @copyright Copyright (c)  Gjero Krsteski (http://krsteski.de)
6
 * @license   http://opensource.org/licenses/MIT MIT License
7
 */
8
9
namespace Pimf\Cache\Storages;
10
11
/**
12
 * @package Cache_Storages
13
 * @author  Gjero Krsteski <[email protected]>
14
 */
15
abstract class Storage implements \ArrayAccess
16
{
17
    /**
18
     * Determine if an item exists in the cache.
19
     *
20
     * @param $key
21
     *
22
     * @return bool
23
     */
24
    public function has($key)
25
    {
26
        return ($this->get($key) !== null);
27
    }
28
29
    /**
30
     * Determine if an item exists in the cache.
31
     *
32
     * Enables you to use: isset($storage[$key])
33
     *
34
     * @param $offset
35
     *
36
     * @return bool
37
     */
38
    public function offsetExists($offset)
39
    {
40
        return $this->has($offset);
41
    }
42
43
    /**
44
     * Get an item from the cache.
45
     *
46
     * <code>
47
     *    // Get an item from the cache storage
48
     *    $name = Cache::storage('name');
49
     *
50
     *    // Return a default value if the requested item isn't cached
51
     *    $name = Cache::get('name', 'Robin');
52
     * </code>
53
     *
54
     * @param      $key
55
     * @param null $default
56
     *
57
     * @return mixed|null
58
     */
59
    public function get($key, $default = null)
60
    {
61
        return (!is_null($item = $this->retrieve($key))) ? $item : $default;
62
    }
63
64
    /**
65
     * Get an item from the cache.
66
     *
67
     * Enables you to use: $storage[$key]
68
     *
69
     * <code>
70
     *    // Get an item from the cache storage
71
     *    $name = Cache::storage()['name'];
72
     * </code>
73
     *
74
     * @param      $offset
75
     *
76
     * @return mixed|null
77
     */
78
    public function offsetGet($offset)
79
    {
80
        return $this->get($offset);
81
    }
82
83
    /**
84
     * Retrieve an item from the cache storage.
85
     *
86
     * @param string $key
87
     *
88
     * @return mixed
89
     */
90
    abstract protected function retrieve($key);
91
92
    /**
93
     * Write an item to the cache for a given number of minutes.
94
     *
95
     * <code>
96
     *    // Put an item in the cache for 15 minutes
97
     *    Cache::put('name', 'Robin', 15);
98
     * </code>
99
     *
100
     * @param string $key
101
     * @param mixed  $value
102
     * @param int    $minutes
103
     *
104
     * @return void
105
     */
106
    abstract public function put($key, $value, $minutes);
107
108
    /**
109
     * Write an item to the cache for indefinite-term storage.
110
     *
111
     * Enables you to use: $storage[$key] = $value;
112
     *
113
     * @param $key
114
     * @param $value
115
     */
116
    public function offsetSet($key, $value)
117
    {
118
        $this->forever($key, value);
119
    }
120
121
    /**
122
     * Get an item from the cache, or cache and return the default value.
123
     *
124
     * <code>
125
     *    // Get an item from the cache, or cache a value for 15 minutes
126
     *    $name = Cache::remember('name', 'Robin', 15);
127
     *
128
     *    // Use a closure for deferred execution
129
     *    $count = Cache::remember('count', function () { return User::count(); }, 15);
130
     * </code>
131
     *
132
     * @param string $key
133
     * @param mixed  $default
134
     * @param int    $minutes
135
     * @param string $function
136
     *
137
     * @return mixed
138
     */
139
    public function remember($key, $default, $minutes, $function = 'put')
140
    {
141
        if (!is_null($item = $this->get($key, null))) {
142
            return $item;
143
        }
144
145
        $this->$function($key, $default, $minutes);
146
147
        return $default;
148
    }
149
150
    /**
151
     * Write an item to the cache for indefinite-term storage.
152
     *
153
     * @param $key
154
     * @param $value
155
     *
156
     * @return mixed Depends on implementation
157
     */
158
    abstract public function forever($key, $value);
159
160
    /**
161
     * Get an item from the cache, or cache the default value forever.
162
     *
163
     * @param string $key
164
     * @param mixed  $default
165
     *
166
     * @return mixed
167
     */
168
    public function sear($key, $default)
169
    {
170
        return $this->remember($key, $default, null, 'forever');
171
    }
172
173
    /**
174
     * Delete an item from the cache.
175
     *
176
     * @param string $key
177
     *
178
     * @return boolean
179
     */
180
    abstract public function forget($key);
181
182
    /**
183
     * Delete an item from the cache.
184
     *
185
     * Enables you to use: unset($storage[$key]);
186
     *
187
     * @param string $key
188
     */
189
    public function offsetUnset($key)
190
    {
191
        $this->forget($key);
192
    }
193
194
    /**
195
     * Get the expiration time as a UNIX timestamp.
196
     *
197
     * @param int $minutes
198
     *
199
     * @return int
200
     */
201
    protected static function expiration($minutes)
202
    {
203
        return time() + ($minutes * 60);
204
    }
205
}
206