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

ItemBaseTrait::expiresAfter()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 34
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
cc 4
eloc 12
c 3
b 1
f 1
nc 4
nop 1
dl 0
loc 34
rs 8.5806
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\Proxy\phpFastCacheAbstractProxy;
18
19
trait ItemBaseTrait
20
{
21
    use ItemExtendedTrait;
22
23
    /**
24
     * @var bool
25
     */
26
    protected $fetched = false;
27
28
    /**
29
     * @var phpFastCacheAbstractProxy
30
     */
31
    protected $driver;
32
33
    /**
34
     * @var string
35
     */
36
    protected $key;
37
38
    /**
39
     * @var mixed
40
     */
41
    protected $data;
42
43
    /**
44
     * @var \DateTime
45
     */
46
    protected $expirationDate;
47
48
    /**
49
     * @var \DateTime
50
     */
51
    protected $creationDate;
52
53
    /**
54
     * @var \DateTime
55
     */
56
    protected $modificationDate;
57
58
    /**
59
     * @var array
60
     */
61
    protected $tags = [];
62
63
    /**
64
     * @var array
65
     */
66
    protected $removedTags = [];
67
68
    /**
69
     * @var bool
70
     */
71
    protected $isHit = false;
72
73
    /********************
74
     *
75
     * PSR-6 Methods
76
     *
77
     *******************/
78
79
    /**
80
     * @return string
81
     */
82
    public function getKey()
83
    {
84
        return $this->key;
85
    }
86
87
    /**
88
     * @return mixed
89
     */
90
    public function get()
91
    {
92
        return $this->data;
93
    }
94
95
    /**
96
     * @param mixed $value
97
     * @return $this
98
     */
99
    public function set($value)
100
    {
101
        /**
102
         * The user set a value,
103
         * therefore there is no need to
104
         * fetch from source anymore
105
         */
106
        $this->fetched = true;
107
        $this->data = $value;
108
109
        /**
110
         * @eventName CacheSaveDeferredItem
111
         * @param ExtendedCacheItemInterface $this
112
         * @param mixed $value
113
         *
114
         */
115
        $this->eventManager->dispatch('CacheItemSet', $this, $value);
116
117
        return $this;
118
    }
119
120
    /**
121
     * @return bool
122
     * @throws \InvalidArgumentException
123
     */
124
    public function isHit()
125
    {
126
        return $this->isHit;
127
    }
128
129
    /**
130
     * @param bool $isHit
131
     * @return $this
132
     * @throws \InvalidArgumentException
133
     */
134
    public function setHit($isHit)
135
    {
136
        if (is_bool($isHit)) {
137
            $this->isHit = $isHit;
138
139
            return $this;
140
        } else {
141
            throw new \InvalidArgumentException('$isHit must be a boolean');
142
        }
143
    }
144
145
    /**
146
     * @param \DateTimeInterface $expiration
147
     * @return $this
148
     * @throws \InvalidArgumentException
149
     */
150
    public function expiresAt($expiration)
151
    {
152
        if ($expiration instanceof \DateTimeInterface) {
153
            /**
154
             * @eventName CacheItemExpireAt
155
             * @param ExtendedCacheItemInterface $this
156
             * @param \DateTimeInterface $expiration
157
             */
158
            $this->eventManager->dispatch('CacheItemExpireAt', $this, $expiration);
159
            $this->expirationDate = $expiration;
0 ignored issues
show
Documentation Bug introduced by
$expiration is of type object<DateTimeInterface>, but the property $expirationDate was declared to be of type object<DateTime>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
160
        } else {
161
            throw new \InvalidArgumentException('$expiration must be an object implementing the DateTimeInterface');
162
        }
163
164
        return $this;
165
    }
166
167
    /**
168
     * Sets the expiration time for this cache item.
169
     *
170
     * @param int|\DateInterval $time
171
     *   The period of time from the present after which the item MUST be considered
172
     *   expired. An integer parameter is understood to be the time in seconds until
173
     *   expiration. If null is passed explicitly, a default value MAY be used.
174
     *   If none is set, the value should be stored permanently or for as long as the
175
     *   implementation allows.
176
     *
177
     * @return static
178
     *   The called object.
179
     *
180
     * @deprecated Use CacheItemInterface::expiresAfter() instead
181
     */
182
    public function touch($time)
183
    {
184
        trigger_error('touch() is deprecated and will be removed in the next major release, use CacheItemInterface::expiresAfter() instead');
185
186
        return $this->expiresAfter($time);
187
    }
188
189
    /**
190
     * @param \DateInterval|int $time
191
     * @return $this
192
     * @throws \InvalidArgumentException
193
     */
194
    public function expiresAfter($time)
195
    {
196
        if (is_numeric($time)) {
197
            if ($time <= 0) {
198
                /**
199
                 * 5 years, however memcached or memory cached will gone when u restart it
200
                 * just recommended for sqlite. files
201
                 */
202
                $time = 30 * 24 * 3600 * 5;
203
            }
204
205
            /**
206
             * @eventName CacheItemExpireAt
207
             * @param ExtendedCacheItemInterface $this
208
             * @param \DateTimeInterface $expiration
209
             */
210
            $this->eventManager->dispatch('CacheItemExpireAfter', $this, $time);
211
212
            $this->expirationDate = (new \DateTime())->add(new \DateInterval(sprintf('PT%dS', $time)));
213
        } else if ($time instanceof \DateInterval) {
214
            /**
215
             * @eventName CacheItemExpireAt
216
             * @param ExtendedCacheItemInterface $this
217
             * @param \DateTimeInterface $expiration
218
             */
219
            $this->eventManager->dispatch('CacheItemExpireAfter', $this, $time);
220
221
            $this->expirationDate = (new \DateTime())->add($time);
222
        } else {
223
            throw new \InvalidArgumentException('Invalid date format');
224
        }
225
226
        return $this;
227
    }
228
}
229