CacheItem   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 2
cbo 1
dl 0
loc 110
ccs 31
cts 31
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getKey() 0 4 1
A get() 0 4 1
A set() 0 6 1
A isHit() 0 4 1
A exists() 0 4 1
A setExpiration() 0 12 3
A getExpiration() 0 4 1
A setHit() 0 10 2
1
<?php
2
/*
3
 * This file is part of the Egils\Component\Cache package.
4
 *
5
 * (c) Egidijus Lukauskas <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Egils\Component\Cache;
12
13
use Psr\Cache\CacheItemInterface;
14
use DateTime;
15
16
class CacheItem implements CacheItemInterface
17
{
18
    /** @var mixed */
19
    private $value;
20
21
    /** @var string */
22
    private $key;
23
24
    /** @var DateTime */
25
    private $expiration;
26
27
    /** @var boolean */
28
    private $hit;
29
30
    /**
31
     * @param string $key
32
     * @param DateTime|integer|null $ttl
33
     * @param boolean $hit
34
     * @throws InvalidArgumentException
35
     */
36 18
    public function __construct($key, $ttl = null, $hit = false)
37
    {
38 18
        $this->key = $key;
39 18
        $this->setExpiration($ttl);
40 18
        $this->setHit($hit);
41 18
    }
42
43
    /**
44
     * @inheritdoc
45
     */
46 2
    public function getKey()
47
    {
48 2
        return $this->key;
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54 1
    public function get()
55
    {
56 1
        return $this->value;
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62 1
    public function set($value)
63
    {
64 1
        $this->value = $value;
65
66 1
        return $this;
67
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72 3
    public function isHit()
73
    {
74 3
        return $this->hit;
75
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80 1
    public function exists()
81
    {
82 1
        return $this->isHit();
83
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88 8
    public function setExpiration($ttl = null)
89
    {
90 8
        if (true === is_numeric($ttl)) {
91 1
            $this->expiration = new DateTime('now +' . $ttl . ' seconds');
92 8
        } elseif ($ttl instanceof DateTime) {
93 1
            $this->expiration = $ttl;
94 1
        } else {
95 8
            $this->expiration = new DateTime('now +10 years');
96
        }
97
98 8
        return $this;
99
    }
100
101
    /**
102
     * @inheritdoc
103
     */
104 3
    public function getExpiration()
105
    {
106 3
        return $this->expiration;
107
    }
108
109
    /**
110
     * @param boolean $hit
111
     *
112
     * @return CacheItem
113
     * @throws InvalidArgumentException not a boolean value given
114
     */
115 8
    public function setHit($hit)
116
    {
117 8
        if (false === is_bool($hit)) {
118 1
            throw InvalidArgumentException::typeMismatch($hit, 'Boolean');
119
        }
120
121 8
        $this->hit = $hit;
122
123 8
        return $this;
124
    }
125
}
126