Completed
Push — V6 ( c5111c...9b134e )
by Georges
02:19
created

ItemExtendedTrait::getEncodedKey()   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
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 *
4
 * This file is part of phpFastCache.
5
 *
6
 * @license MIT License (MIT)
7
 *
8
 * For full copyright and license information, please see the docs/CREDITS.txt file.
9
 *
10
 * @author Khoa Bui (khoaofgod)  <[email protected]> http://www.phpfastcache.com
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 *
13
 */
14
15
namespace phpFastCache\Core\Item;
16
17
use phpFastCache\EventManager;
18
19
/**
20
 * Class ItemExtendedTrait
21
 * @package phpFastCache\Core\Item
22
 */
23
trait ItemExtendedTrait
24
{
25
    /********************
26
     *
27
     * PSR-6 Extended Methods
28
     *
29
     *******************/
30
31
    /**
32
     * @var EventManager
33
     */
34
    protected $eventManager;
35
36
    /**
37
     * @return string
38
     */
39
    public function getEncodedKey()
40
    {
41
        return md5($this->getKey());
42
    }
43
44
    /**
45
     * @return mixed
46
     */
47
    public function getUncommittedData()
48
    {
49
        return $this->data;
0 ignored issues
show
Bug introduced by
The property data does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
50
    }
51
52
    /**
53
     * @return \DateTimeInterface
54
     */
55
    public function getExpirationDate()
56
    {
57
        return $this->expirationDate;
0 ignored issues
show
Bug introduced by
The property expirationDate does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
58
    }
59
60
    /**
61
     * Alias of expireAt() with forced $expiration param
62
     *
63
     * @param \DateTimeInterface $expiration
64
     *   The point in time after which the item MUST be considered expired.
65
     *   If null is passed explicitly, a default value MAY be used. If none is set,
66
     *   the value should be stored permanently or for as long as the
67
     *   implementation allows.
68
     *
69
     * @return static
70
     *   The called object.
71
     */
72
    public function setExpirationDate(\DateTimeInterface $expiration)
73
    {
74
        return $this->expiresAt($expiration);
75
    }
76
77
78
    /**
79
     * @return \DateTimeInterface
80
     * @throws \LogicException
81
     */
82
    public function getCreationDate()
83
    {
84
        if($this->driver->getConfig()['itemDetailedDate']){
0 ignored issues
show
Bug introduced by
The property driver does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
85
            return $this->creationDate;
0 ignored issues
show
Bug introduced by
The property creationDate does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
86
        }else{
87
            throw new \LogicException('Cannot access to the creation date when the "itemDetailedDate" configuration is disabled.');
88
        }
89
    }
90
91
    /**
92
     * @param \DateTimeInterface $date
93
     * @return $this
94
     * @throws \LogicException
95
     */
96 View Code Duplication
    public function setCreationDate(\DateTimeInterface $date)
97
    {
98
        if($this->driver->getConfig()['itemDetailedDate']){
99
            $this->creationDate = $date;
100
            return $this;
101
        }else{
102
            throw new \LogicException('Cannot access to the creation date when the "itemDetailedDate" configuration is disabled.');
103
        }
104
    }
105
106
    /**
107
     * @return \DateTimeInterface
108
     * @throws \LogicException
109
     */
110
    public function getModificationDate()
111
    {
112
        if($this->driver->getConfig()['itemDetailedDate']){
113
            return $this->modificationDate;
0 ignored issues
show
Bug introduced by
The property modificationDate does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
114
        }else{
115
            throw new \LogicException('Cannot access to the modification date when the "itemDetailedDate" configuration is disabled.');
116
        }
117
    }
118
119
    /**
120
     * @param \DateTimeInterface $date
121
     * @return $this
122
     * @throws \LogicException
123
     */
124 View Code Duplication
    public function setModificationDate(\DateTimeInterface $date)
125
    {
126
        if($this->driver->getConfig()['itemDetailedDate']){
127
            $this->modificationDate = $date;
128
            return $this;
129
        }else{
130
            throw new \LogicException('Cannot access to the modification date when the "itemDetailedDate" configuration is disabled.');
131
        }
132
    }
133
134
    /**
135
     * @return int
136
     */
137
    public function getTtl()
138
    {
139
        $ttl = $this->expirationDate->getTimestamp() - time();
140
        if ($ttl > 2592000) {
141
            $ttl = time() + $ttl;
142
        }
143
144
        return $ttl;
145
    }
146
147
    /**
148
     * @return bool
149
     */
150
    public function isExpired()
151
    {
152
        return $this->expirationDate->getTimestamp() < (new \DateTime())->getTimestamp();
153
    }
154
155
    /**
156
     * @param int $step
157
     * @return $this
158
     * @throws \InvalidArgumentException
159
     */
160 View Code Duplication
    public function increment($step = 1)
161
    {
162
        if (is_int($step)) {
163
            $this->fetched = true;
0 ignored issues
show
Bug introduced by
The property fetched does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
164
            $this->data += $step;
165
        } else {
166
            throw new \InvalidArgumentException('$step must be numeric.');
167
        }
168
169
        return $this;
170
    }
171
172
    /**
173
     * @param int $step
174
     * @return $this
175
     * @throws \InvalidArgumentException
176
     */
177 View Code Duplication
    public function decrement($step = 1)
178
    {
179
        if (is_int($step)) {
180
            $this->fetched = true;
181
            $this->data -= $step;
182
        } else {
183
            throw new \InvalidArgumentException('$step must be numeric.');
184
        }
185
186
        return $this;
187
    }
188
189
    /**
190
     * @param array|string $data
191
     * @return $this
192
     * @throws \InvalidArgumentException
193
     */
194 View Code Duplication
    public function append($data)
195
    {
196
        if (is_array($this->data)) {
197
            array_push($this->data, $data);
198
        } else if (is_string($data)) {
199
            $this->data .= (string) $data;
200
        } else {
201
            throw new \InvalidArgumentException('$data must be either array nor string.');
202
        }
203
204
        return $this;
205
    }
206
207
208
    /**
209
     * @param array|string $data
210
     * @return $this
211
     * @throws \InvalidArgumentException
212
     */
213 View Code Duplication
    public function prepend($data)
214
    {
215
        if (is_array($this->data)) {
216
            array_unshift($this->data, $data);
217
        } else if (is_string($data)) {
218
            $this->data = (string) $data . $this->data;
219
        } else {
220
            throw new \InvalidArgumentException('$data must be either array nor string.');
221
        }
222
223
        return $this;
224
    }
225
226
    /**
227
     * @param $tagName
228
     * @return $this
229
     * @throws \InvalidArgumentException
230
     */
231
    public function addTag($tagName)
232
    {
233
        if (is_string($tagName)) {
234
            $this->tags = array_unique(array_merge($this->tags, [$tagName]));
0 ignored issues
show
Bug introduced by
The property tags does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
235
236
            return $this;
237
        } else {
238
            throw new \InvalidArgumentException('$tagName must be a string');
239
        }
240
    }
241
242
    /**
243
     * @param array $tagNames
244
     * @return $this
245
     */
246
    public function addTags(array $tagNames)
247
    {
248
        foreach ($tagNames as $tagName) {
249
            $this->addTag($tagName);
250
        }
251
252
        return $this;
253
    }
254
255
    /**
256
     * @param array $tags
257
     * @return $this
258
     * @throws \InvalidArgumentException
259
     */
260
    public function setTags(array $tags)
261
    {
262
        if (count($tags)) {
263
            if (array_filter($tags, 'is_string')) {
264
                $this->tags = $tags;
265
            } else {
266
                throw new \InvalidArgumentException('$tagName must be an array of string');
267
            }
268
        }
269
270
        return $this;
271
    }
272
273
    /**
274
     * @return array
275
     */
276
    public function getTags()
277
    {
278
        return $this->tags;
279
    }
280
281
    /**
282
     * @return string
283
     */
284
    public function getTagsAsString($separator = ', ')
285
    {
286
        return implode($separator, $this->tags);
287
    }
288
289
    /**
290
     * @param $tagName
291
     * @return $this
292
     */
293
    public function removeTag($tagName)
294
    {
295
        if (($key = array_search($tagName, $this->tags)) !== false) {
296
            unset($this->tags[ $key ]);
297
            $this->removedTags[] = $tagName;
0 ignored issues
show
Bug introduced by
The property removedTags does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
298
        }
299
300
        return $this;
301
    }
302
303
    /**
304
     * @param array $tagNames
305
     * @return $this
306
     */
307
    public function removeTags(array $tagNames)
308
    {
309
        foreach ($tagNames as $tagName) {
310
            $this->removeTag($tagName);
311
        }
312
313
        return $this;
314
    }
315
316
    /**
317
     * @return array
318
     */
319
    public function getRemovedTags()
320
    {
321
        return array_diff($this->removedTags, $this->tags);
322
    }
323
324
    /**
325
     * Return the data as a well-formatted string.
326
     * Any scalar value will be casted to an array
327
     * @param int $option json_encode() options
328
     * @param int $depth json_encode() depth
329
     * @return string
330
     */
331
    public function getDataAsJsonString($option = 0, $depth = 512)
332
    {
333
        $data = $this->get();
334
335
        if (is_object($data) || is_array($data)) {
336
            $data = json_encode($data, $option, $depth);
337
        } else {
338
            $data = json_encode([$data], $option, $depth);
339
        }
340
341
        return json_encode($data, $option, $depth);
342
    }
343
344
    /**
345
     * Implements \JsonSerializable interface
346
     * @return mixed
347
     */
348
    public function jsonSerialize()
349
    {
350
        return $this->get();
351
    }
352
353
354
    /**
355
     * Set the EventManager instance
356
     *
357
     * @param EventManager $em
358
     */
359
    public function setEventManager(EventManager $em)
360
    {
361
        $this->eventManager = $em;
362
    }
363
364
365
    /**
366
     * Prevent recursions for Debug (php 5.6+)
367
     * @return array
368
     */
369
    final public function __debugInfo()
370
    {
371
        $info = get_object_vars($this);
372
        $info[ 'driver' ] = 'object(' . get_class($info[ 'driver' ]) . ')';
373
374
        return (array) $info;
375
    }
376
}