Psr6Cache::has()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PublishingKit\Cache\Services\Cache;
6
7
use PublishingKit\Cache\Contracts\Services\CacheContract;
8
use Psr\Cache\CacheItemPoolInterface;
9
use TypeError;
10
use DateTime;
11
12
final class Psr6Cache implements CacheContract
13
{
14
    /**
15
     * @var CacheItemPoolInterface
16
     */
17
    private $cache;
18
19 72
    public function __construct(CacheItemPoolInterface $cache)
20
    {
21 72
        $this->cache = $cache;
22 72
    }
23
24
    /**
25
     * {@inheritDoc}
26
     */
27 9
    public function get(string $key, $default = null)
28
    {
29 9
        $item = $this->cache->getItem($key);
30 9
        if (!$item->isHit()) {
31 6
            return $default;
32
        }
33 3
        return $item->get();
34
    }
35
36
    /**
37
     * {@inheritDoc}
38
     */
39 12
    public function put(string $key, $value, $expiry = null): void
40
    {
41 12
        if (isset($expiry) && !is_int($expiry) && !is_a($expiry, 'DateTime')) {
42 3
            throw new TypeError('Expiry can only be null, an instance of DateTime, or an integer');
43
        }
44 9
        $item = $this->cache->getItem($key);
45 9
        $item->set($value);
46 9
        if (is_int($expiry)) {
47 3
            $item->expiresAfter($expiry);
48
        }
49 9
        if ($expiry instanceof DateTime) {
50 3
            $item->expiresAt($expiry);
51
        }
52 9
        $this->cache->save($item);
53 9
    }
54
55
    /**
56
     * {@inheritDoc}
57
     */
58 3
    public function forever(string $key, $value): void
59
    {
60 3
        $item = $this->cache->getItem($key);
61 3
        $item->set($value);
62 3
        $this->cache->save($item);
63 3
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68 3
    public function forget(string $key): void
69
    {
70 3
        $this->cache->deleteItem($key);
71 3
    }
72
73
    /**
74
     * {@inheritDoc}
75
     */
76 6
    public function has(string $key): bool
77
    {
78 6
        $item = $this->cache->getItem($key);
79 6
        return $item->isHit();
80
    }
81
82
    /**
83
     * {@inheritDoc}
84
     */
85 21
    public function remember(string $key, $expiry, callable $callback)
86
    {
87 21
        if (!is_int($expiry) && !is_a($expiry, 'DateTime')) {
88 3
            throw new TypeError('Expiry can only be an instance of DateTime, or an integer');
89
        }
90 18
        $item = $this->cache->getItem($key);
91 18
        if (!$item->isHit()) {
92 12
            $value = $callback();
93 12
            $item->set($value);
94 12
            if (is_int($expiry)) {
95 6
                $item->expiresAfter($expiry);
96
            }
97 12
            if ($expiry instanceof DateTime) {
98 6
                $item->expiresAt($expiry);
99
            }
100 12
            $this->cache->save($item);
101 12
            return $value;
102
        }
103 6
        return $item->get();
104
    }
105
106
    /**
107
     * {@inheritDoc}
108
     */
109 12
    public function rememberForever(string $key, callable $callback)
110
    {
111 12
        $item = $this->cache->getItem($key);
112 12
        if (!$item->isHit()) {
113 6
            $value = $callback();
114 6
            $item->set($value);
115 6
            $this->cache->save($item);
116 6
            return $value;
117
        }
118 6
        return $item->get();
119
    }
120
121
    /**
122
     * {@inheritDoc}
123
     */
124 3
    public function flush(): void
125
    {
126 3
        $this->cache->clear();
127 3
    }
128
129 3
    public function getCache(): CacheItemPoolInterface
130
    {
131 3
        return $this->cache;
132
    }
133
}
134