Completed
Push — V6 ( 927ad1...3487d5 )
by Georges
03:17
created

ItemExtendedTrait::setEventManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 1
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 mixed
38
     */
39
    public function getUncommittedData()
40
    {
41
        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...
42
    }
43
44
    /**
45
     * @return \DateTimeInterface
46
     */
47
    public function getExpirationDate()
48
    {
49
        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...
50
    }
51
52
    /**
53
     * Alias of expireAt() with forced $expiration param
54
     *
55
     * @param \DateTimeInterface $expiration
56
     *   The point in time after which the item MUST be considered expired.
57
     *   If null is passed explicitly, a default value MAY be used. If none is set,
58
     *   the value should be stored permanently or for as long as the
59
     *   implementation allows.
60
     *
61
     * @return static
62
     *   The called object.
63
     */
64
    public function setExpirationDate(\DateTimeInterface $expiration)
65
    {
66
        return $this->expiresAt($expiration);
67
    }
68
69
70
    /**
71
     * @return \DateTimeInterface
72
     * @throws \LogicException
73
     */
74
    public function getCreationDate()
75
    {
76
        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...
77
            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...
78
        }else{
79
            throw new \LogicException('Cannot access to the creation date when the "itemDetailedDate" configuration is disabled.');
80
        }
81
    }
82
83
    /**
84
     * @param \DateTimeInterface $date
85
     * @return $this
86
     * @throws \LogicException
87
     */
88 View Code Duplication
    public function setCreationDate(\DateTimeInterface $date)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
    {
90
        if($this->driver->getConfig()['itemDetailedDate']){
91
            $this->creationDate = $date;
92
            return $this;
93
        }else{
94
            throw new \LogicException('Cannot access to the creation date when the "itemDetailedDate" configuration is disabled.');
95
        }
96
    }
97
98
    /**
99
     * @return \DateTimeInterface
100
     * @throws \LogicException
101
     */
102
    public function getModificationDate()
103
    {
104
        if($this->driver->getConfig()['itemDetailedDate']){
105
            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...
106
        }else{
107
            throw new \LogicException('Cannot access to the modification date when the "itemDetailedDate" configuration is disabled.');
108
        }
109
    }
110
111
    /**
112
     * @param \DateTimeInterface $date
113
     * @return $this
114
     * @throws \LogicException
115
     */
116 View Code Duplication
    public function setModificationDate(\DateTimeInterface $date)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
    {
118
        if($this->driver->getConfig()['itemDetailedDate']){
119
            $this->modificationDate = $date;
120
            return $this;
121
        }else{
122
            throw new \LogicException('Cannot access to the creation date when the "itemDetailedDate" configuration is disabled.');
123
        }
124
    }
125
126
    /**
127
     * @return int
128
     */
129
    public function getTtl()
130
    {
131
        $ttl = $this->expirationDate->getTimestamp() - time();
132
        if ($ttl > 2592000) {
133
            $ttl = time() + $ttl;
134
        }
135
136
        return $ttl;
137
    }
138
139
    /**
140
     * @return bool
141
     */
142
    public function isExpired()
143
    {
144
        return $this->expirationDate->getTimestamp() < (new \DateTime())->getTimestamp();
145
    }
146
147
    /**
148
     * @param int $step
149
     * @return $this
150
     * @throws \InvalidArgumentException
151
     */
152 View Code Duplication
    public function increment($step = 1)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
153
    {
154
        if (is_int($step)) {
155
            $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...
156
            $this->data += $step;
157
        } else {
158
            throw new \InvalidArgumentException('$step must be numeric.');
159
        }
160
161
        return $this;
162
    }
163
164
    /**
165
     * @param int $step
166
     * @return $this
167
     * @throws \InvalidArgumentException
168
     */
169 View Code Duplication
    public function decrement($step = 1)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
170
    {
171
        if (is_int($step)) {
172
            $this->fetched = true;
173
            $this->data -= $step;
174
        } else {
175
            throw new \InvalidArgumentException('$step must be numeric.');
176
        }
177
178
        return $this;
179
    }
180
181
    /**
182
     * @param array|string $data
183
     * @return $this
184
     * @throws \InvalidArgumentException
185
     */
186 View Code Duplication
    public function append($data)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
187
    {
188
        if (is_array($this->data)) {
189
            array_push($this->data, $data);
190
        } else if (is_string($data)) {
191
            $this->data .= (string) $data;
192
        } else {
193
            throw new \InvalidArgumentException('$data must be either array nor string.');
194
        }
195
196
        return $this;
197
    }
198
199
200
    /**
201
     * @param array|string $data
202
     * @return $this
203
     * @throws \InvalidArgumentException
204
     */
205 View Code Duplication
    public function prepend($data)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
206
    {
207
        if (is_array($this->data)) {
208
            array_unshift($this->data, $data);
209
        } else if (is_string($data)) {
210
            $this->data = (string) $data . $this->data;
211
        } else {
212
            throw new \InvalidArgumentException('$data must be either array nor string.');
213
        }
214
215
        return $this;
216
    }
217
218
    /**
219
     * @param $tagName
220
     * @return $this
221
     * @throws \InvalidArgumentException
222
     */
223
    public function addTag($tagName)
224
    {
225
        if (is_string($tagName)) {
226
            $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...
227
228
            return $this;
229
        } else {
230
            throw new \InvalidArgumentException('$tagName must be a string');
231
        }
232
    }
233
234
    /**
235
     * @param array $tagNames
236
     * @return $this
237
     */
238
    public function addTags(array $tagNames)
239
    {
240
        foreach ($tagNames as $tagName) {
241
            $this->addTag($tagName);
242
        }
243
244
        return $this;
245
    }
246
247
    /**
248
     * @param array $tags
249
     * @return $this
250
     * @throws \InvalidArgumentException
251
     */
252
    public function setTags(array $tags)
253
    {
254
        if (count($tags)) {
255
            if (array_filter($tags, 'is_string')) {
256
                $this->tags = $tags;
257
            } else {
258
                throw new \InvalidArgumentException('$tagName must be an array of string');
259
            }
260
        }
261
262
        return $this;
263
    }
264
265
    /**
266
     * @return array
267
     */
268
    public function getTags()
269
    {
270
        return $this->tags;
271
    }
272
273
    /**
274
     * @return string
275
     */
276
    public function getTagsAsString($separator = ', ')
277
    {
278
        return implode($separator, $this->tags);
279
    }
280
281
    /**
282
     * @param $tagName
283
     * @return $this
284
     */
285
    public function removeTag($tagName)
286
    {
287
        if (($key = array_search($tagName, $this->tags)) !== false) {
288
            unset($this->tags[ $key ]);
289
            $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...
290
        }
291
292
        return $this;
293
    }
294
295
    /**
296
     * @param array $tagNames
297
     * @return $this
298
     */
299
    public function removeTags(array $tagNames)
300
    {
301
        foreach ($tagNames as $tagName) {
302
            $this->removeTag($tagName);
303
        }
304
305
        return $this;
306
    }
307
308
    /**
309
     * @return array
310
     */
311
    public function getRemovedTags()
312
    {
313
        return array_diff($this->removedTags, $this->tags);
314
    }
315
316
    /**
317
     * Return the data as a well-formatted string.
318
     * Any scalar value will be casted to an array
319
     * @param int $option json_encode() options
320
     * @param int $depth json_encode() depth
321
     * @return string
322
     */
323
    public function getDataAsJsonString($option = 0, $depth = 512)
324
    {
325
        $data = $this->get();
326
327
        if (is_object($data) || is_array($data)) {
328
            $data = json_encode($data, $option, $depth);
329
        } else {
330
            $data = json_encode([$data], $option, $depth);
331
        }
332
333
        return json_encode($data, $option, $depth);
334
    }
335
336
    /**
337
     * Implements \JsonSerializable interface
338
     * @return mixed
339
     */
340
    public function jsonSerialize()
341
    {
342
        return $this->get();
343
    }
344
345
346
    /**
347
     * Set the EventManager instance
348
     *
349
     * @param EventManager $em
350
     */
351
    public function setEventManager(EventManager $em)
352
    {
353
        $this->eventManager = $em;
354
    }
355
356
357
    /**
358
     * Prevent recursions for Debug (php 5.6+)
359
     * @return array
360
     */
361
    final public function __debugInfo()
362
    {
363
        $info = get_object_vars($this);
364
        $info[ 'driver' ] = 'object(' . get_class($info[ 'driver' ]) . ')';
365
366
        return (array) $info;
367
    }
368
}