Passed
Branch support-laravel-6 (ddd0b3)
by Pieter
06:42
created

CacheItem   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 112
rs 10
wmc 13

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getExpiresAt() 0 3 1
A expiresAfter() 0 17 3
A __construct() 0 5 2
A expiresAt() 0 11 3
A get() 0 3 1
A getKey() 0 3 1
A set() 0 5 1
A isHit() 0 3 1
1
<?php
2
namespace W2w\Laravel\Apie\Services\Psr6;
3
4
use DateInterval;
5
use DateTimeImmutable;
6
use DateTimeInterface;
7
use Psr\Cache\CacheItemInterface;
8
9
class CacheItem implements CacheItemInterface
10
{
11
    /**
12
     * @var string
13
     */
14
    private $key;
15
16
    /**
17
     * @var mixed|null
18
     */
19
    private $value;
20
21
    /**
22
     * @var bool
23
     */
24
    private $hit;
25
26
    /**
27
     * @var \DateTimeInterface
28
     */
29
    private $expires;
30
31
    /**
32
     * @param string $key
33
     * @param mixed $value
34
     * @param bool $hit
35
     */
36
    public function __construct($key, $value = null, $hit = false)
37
    {
38
        $this->key = $key;
39
        $this->hit = boolval($hit);
40
        $this->value = $this->hit ? $value : null;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getKey()
47
    {
48
        return $this->key;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function get()
55
    {
56
        return $this->value;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function isHit()
63
    {
64
        return $this->hit;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function set($value)
71
    {
72
        $this->value = $value;
73
74
        return $this;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function expiresAt($expires)
81
    {
82
        if ($expires instanceof DateTimeInterface && ! $expires instanceof DateTimeImmutable) {
83
            $timezone = $expires->getTimezone();
84
            $expires = DateTimeImmutable::createFromFormat('U', (string) $expires->getTimestamp(), $timezone);
85
            $expires->setTimezone($timezone);
86
        }
87
88
        $this->expires = $expires;
89
90
        return $this;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function expiresAfter($time)
97
    {
98
        if ($time === null) {
99
            $this->expires = null;
100
101
            return;
102
        }
103
104
        $this->expires = new DateTimeImmutable();
105
106
        if (! $time instanceof DateInterval) {
107
            $time = new DateInterval(sprintf('PT%sS', $time));
108
        }
109
110
        $this->expires = $this->expires->add($time);
111
112
        return $this;
113
    }
114
115
    /**
116
     * @return \DateTimeInterface
117
     */
118
    public function getExpiresAt()
119
    {
120
        return $this->expires;
121
    }
122
}
123