Completed
Push — master ( f20e30...a2c18f )
by Jonathan
04:47
created

EncryptingItemDecorator::isDecryptable()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 1
ccs 0
cts 0
cp 0
nc 1
1
<?php
2
namespace Jeskew\Cache;
3
4
use Psr\Cache\CacheItemInterface;
5
6
abstract class EncryptingItemDecorator implements CacheItemInterface
7
{
8
    /** @var CacheItemInterface */
9
    private $decorated;
10
    /** @var mixed */
11
    private $decrypted;
12
13 333
    public function __construct(CacheItemInterface $decorated)
14
    {
15 333
        $this->decorated = $decorated;
16 333
    }
17
18 228
    public function getKey()
19
    {
20 228
        return $this->decorated->getKey();
21
    }
22
23 174
    public function get()
24
    {
25 174
        if (empty($this->decrypted) && $this->isDecryptable()) {
26 112
            $this->decrypted = $this->decrypt($this->decorated->get());
27 112
        }
28
29 174
        return $this->decrypted;
30
    }
31
32 294
    public function getDecorated()
33
    {
34 294
        return $this->decorated;
35
    }
36
37 222
    public function set($value)
38
    {
39 222
        $this->decorated->set($this->encrypt($value));
40 222
        $this->decrypted = $value;
41
42 222
        return $this;
43
    }
44
45 140
    public function isHit()
46
    {
47 140
        return $this->decorated->isHit()
48 140
            && $this->isDecryptable();
49
    }
50
51 6
    public function expiresAt($expiresAt)
52
    {
53 6
        $this->decorated->expiresAt($expiresAt);
54
55 6
        return $this;
56
    }
57
58 12
    public function expiresAfter($expiresAfter)
59
    {
60 12
        $this->decorated->expiresAfter($expiresAfter);
61
62 12
        return $this;
63
    }
64
65
    abstract protected function encrypt($data);
66
67
    abstract protected function decrypt(EncryptedValue $data);
68
69
    abstract protected function isDecryptable();
70
}
71