Completed
Push — master ( 789220...9af5c5 )
by Jonathan
04:43
created

EncryptingItemDecorator::get()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4286
cc 3
eloc 4
nc 2
nop 0
crap 3
1
<?php
2
namespace Jsq\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 132
            $this->decrypted = $this->decrypt($this->decorated->get());
27 132
        }
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 144
    public function isHit()
46
    {
47 144
        return $this->decorated->isHit()
48 144
            && $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