Completed
Push — master ( fb3e81...9a96e3 )
by Oskar
04:01
created

CCacheFile   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 218
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 95.83%

Importance

Changes 4
Bugs 2 Features 0
Metric Value
wmc 19
c 4
b 2
f 0
lcom 1
cbo 1
dl 0
loc 218
ccs 46
cts 48
cp 0.9583
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getKey() 0 4 1
A getValue() 0 4 1
A __construct() 0 11 3
A filename() 0 9 2
A get() 0 9 2
A isHit() 0 13 4
A set() 0 5 1
A setTimeZone() 0 4 1
A expiresAt() 0 10 2
A expiresAfter() 0 13 2
1
<?php
2
3
namespace Anax\Cache;
4
5
/**
6
 *
7
 */
8
class CCacheFile implements \Psr\Cache\CacheItemInterface
9
{
10
    /**
11
     * The key associated with the object
12
     * @var string
13
     */
14
    private $key;
15
16
    /**
17
     * The expiration time
18
     * @var \DateTime
19
     */
20
    public $expiration;
21
22
    /**
23
     * The default expiration time
24
     * @var string
25
     */
26
    public $defaultExpiration;
27
28
    /**
29
     * The value associated with the object
30
     * @var mixed
31
     */
32
    private $value;
33
34
    /**
35
     * Determines the timezone used
36
     * @var \DateTimeZone
37
     */
38
    public $timeZone;
39
40
41
    /**
42
     * Constructor
43
     *
44
     * @param string       $key        The object key
45
     * @param mixed        $value      The object key
46
     * @param DateTime     $expiration The expiration date
47
     * @param DateTimeZone $timeZone   The timezone used with the date
48
     */
49 5
    public function __construct($key, $value = null, $expiration = null, $timeZone = null)
50
    {
51 5
        $this->key = $key;
52 5
        $this->value = $value;
53
54 5
        $this->timeZone = is_null($timeZone) ?  new \DateTimeZone('Europe/London') : $timeZone;
0 ignored issues
show
Documentation Bug introduced by
It seems like is_null($timeZone) ? new...pe/London') : $timeZone can also be of type object<Anax\Cache\DateTimeZone>. However, the property $timeZone is declared as type object<DateTimeZone>. Maybe add an additional type 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 mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
55 5
        $this->defaultExpiration = '2999-12-12';
56
57 5
        $this->expiration = is_null($expiration) ?
0 ignored issues
show
Documentation Bug introduced by
It seems like is_null($expiration) ? n...timeZone) : $expiration can also be of type object<Anax\Cache\DateTime>. However, the property $expiration is declared as type object<DateTime>. Maybe add an additional type 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 mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
58 5
            new \DateTime($this->defaultExpiration, $this->timeZone) : $expiration;
59 5
    }
60
61
    /**
62
     * Returns the key for the current cache item.
63
     *
64
     * The key is loaded by the Implementing Library, but should be available to
65
     * the higher level callers when needed.
66
     *
67
     * @return string
68
     *   The key string for this cache item.
69
     */
70 5
    public function getKey()
71
    {
72 5
        return $this->key;
73
    }
74
75
    /**
76
     * Returns the value for the current cache item.
77
     *
78
     * @return mixed The value for the current cache item
79
     */
80 2
    public function getValue()
81
    {
82 2
        return $this->value;
83
    }
84
85
    /**
86
     * Generate a filename for the cached object.
87
     *
88
     * @param string $key to the cached object.
89
     *
90
     * @return string The filename.
91
     */
92 2
    public function filename($key, $expiration = null)
93
    {
94 2
        if (is_null($expiration)) {
95 2
            return \Anax\Cache\CCachePool::getPath() . '/' . $key . '.val';
96
        }
97
98
        // return \Anax\Cache\CCachePool::getPath() . '/'. $expiration .'/' . $key;
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
99 2
        return \Anax\Cache\CCachePool::getPath() . '/' . $key . '.meta';
100
    }
101
102
    /**
103
     * Retrieves the value of the item from the cache associated with this object's key.
104
     *
105
     * The value returned must be identical to the value originally stored by set().
106
     *
107
     * If isHit() returns false, this method MUST return null. Note that null
108
     * is a legitimate cached value, so the isHit() method SHOULD be used to
109
     * differentiate between "null value was found" and "no value was found."
110
     *
111
     * @return mixed
112
     *   The value corresponding to this cache item's key, or null if not found.
113
     */
114 1
    public function get()
115
    {
116 1
        if ($this->isHit()) {
117 1
            $file = $this->filename($this->key);
118 1
            return unserialize(file_get_contents($file));
119
        } else {
120 1
            return null;
121
        }
122
    }
123
124
    /**
125
     * Confirms if the cache item lookup resulted in a cache hit.
126
     *
127
     * Note: This method MUST NOT have a race condition between calling isHit()
128
     * and calling get().
129
     *
130
     * @return bool
131
     *   True if the request resulted in a cache hit. False otherwise.
132
     */
133 1
    public function isHit()
134
    {
135 1
        $file = $this->filename($this->key);
136
137 1
        $now = new \DateTime("now", $this->timeZone);
138 1
        $hasExpired = ($now > $this->expiration) ? true : false;
139
140 1
        if (is_file($file) && $hasExpired === false) {
141 1
            return true;
142
        } else {
143 1
            return false;
144
        }
145
    }
146
147
    /**
148
     * Sets the value represented by this cache item.
149
     *
150
     * The $value argument may be any item that can be serialized by PHP,
151
     * although the method of serialization is left up to the Implementing
152
     * Library.
153
     *
154
     * @param mixed $value
155
     *   The serializable value to be stored.
156
     *
157
     * @return static
158
     *   The invoked object.
159
     */
160 1
    public function set($value)
161
    {
162 1
        $this->value = $value;
163 1
        return $this;
164
    }
165
166
    /**
167
     * Sets the timezone that should be used for the expiration time
168
     *
169
     * @param DateTimeZone $timeZone The timezone used for the expiration time
170
     */
171 3
    public function setTimeZone($timeZone)
172
    {
173 3
        $this->timeZone = $timeZone;
0 ignored issues
show
Documentation Bug introduced by
It seems like $timeZone of type object<Anax\Cache\DateTimeZone> is incompatible with the declared type object<DateTimeZone> of property $timeZone.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
174 3
    }
175
176
    /**
177
     * Sets the expiration time for this cache item.
178
     *
179
     * @param \DateTimeInterface $expiration
180
     *   The point in time after which the item MUST be considered expired.
181
     *   If null is passed explicitly, a default value MAY be used. If none is set,
182
     *   the value should be stored permanently or for as long as the
183
     *   implementation allows.
184
     *
185
     * @return static
186
     *   The called object.
187
     */
188 1
    public function expiresAt($expiration)
189
    {
190 1
        $this->expiration = $expiration;
0 ignored issues
show
Documentation Bug introduced by
It seems like $expiration of type object<DateTimeInterface> is incompatible with the declared type object<DateTime> of property $expiration.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
191
192 1
        if (is_null($expiration)) {
193 1
            $this->expiration = new \DateTime($this->defaultExpiration, $this->timeZone);
194 1
        }
195
196 1
        return $this;
197
    }
198
199
    /**
200
     * Sets the expiration time for this cache item.
201
     *
202
     * @param int|\DateInterval $time
203
     *   The period of time from the present after which the item MUST be considered
204
     *   expired. An integer parameter is understood to be the time in seconds until
205
     *   expiration. If null is passed explicitly, a default value MAY be used.
206
     *   If none is set, the value should be stored permanently or for as long as the
207
     *   implementation allows.
208
     *
209
     * @return static
210
     *   The called object.
211
     */
212 1
    public function expiresAfter($time)
213
    {
214 1
        $now = new \DateTime("now", $this->timeZone);
215
216 1
        if (is_null($time)) {
217
            $this->expiration = new \DateTime($this->defaultExpiration, $this->timeZone);
218
        } else {
219 1
            $dateInterval = 'PT' . $time . 'S';
220 1
            $this->expiration = $now->add(new \DateInterval($dateInterval));
221
        }
222
223 1
        return $this;
224
    }
225
}
226