Completed
Push — V6 ( 0098cc...760f65 )
by Georges
02:58
created

ItemBaseTrait::getKey()   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\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
     * @param \DateInterval|int $time
169
     * @return $this
170
     * @throws \InvalidArgumentException
171
     */
172
    public function expiresAfter($time)
173
    {
174
        if (is_numeric($time)) {
175
            if ($time <= 0) {
176
                /**
177
                 * 5 years, however memcached or memory cached will gone when u restart it
178
                 * just recommended for sqlite. files
179
                 */
180
                $time = 30 * 24 * 3600 * 5;
181
            }
182
183
            /**
184
             * @eventName CacheItemExpireAt
185
             * @param ExtendedCacheItemInterface $this
186
             * @param \DateTimeInterface $expiration
187
             */
188
            $this->eventManager->dispatch('CacheItemExpireAfter', $this, $time);
189
190
            $this->expirationDate = (new \DateTime())->add(new \DateInterval(sprintf('PT%dS', $time)));
191
        } else if ($time instanceof \DateInterval) {
192
            /**
193
             * @eventName CacheItemExpireAt
194
             * @param ExtendedCacheItemInterface $this
195
             * @param \DateTimeInterface $expiration
196
             */
197
            $this->eventManager->dispatch('CacheItemExpireAfter', $this, $time);
198
199
            $this->expirationDate = (new \DateTime())->add($time);
200
        } else {
201
            throw new \InvalidArgumentException('Invalid date format');
202
        }
203
204
        return $this;
205
    }
206
}
207