Completed
Push — master ( e03480...83fdca )
by Jonathan
04:06
created

ItemDecorator::expiresAt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 2
cts 2
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
namespace Jsq\CacheEncryption;
3
4
use Psr\Cache\CacheItemInterface;
5
6
abstract class ItemDecorator implements CacheItemInterface
7
{
8
    /** @var string */
9
    private $cipher;
10
    /** @var mixed */
11
    private $decrypted;
12
    /** @var CacheItemInterface */
13
    private $decorated;
14
15 333
    public function __construct($cipher, CacheItemInterface $decorated)
16
    {
17 333
        $this->cipher = $cipher;
18 333
        $this->decorated = $decorated;
19 333
    }
20
21 228
    public function getKey()
22
    {
23 228
        return $this->decorated->getKey();
24
    }
25
26 174
    /**
27
     * @return CacheItemInterface
28 174
     */
29 132
    public function getDecorated()
30 132
    {
31
        return $this->decorated;
32 174
    }
33
34
    public function set($value)
35 294
    {
36
        $this->decorated->set($this->encrypt($value));
37 294
        $this->decrypted = $value;
38
39
        return $this;
40 222
    }
41
42 222
    public function get()
43 222
    {
44
        if (empty($this->decrypted) && $this->isHit()) {
45 222
            $this->decrypted = $this->decrypt($this->getDecorated()->get());
46
        }
47
48 144
        return $this->decrypted;
49
    }
50 144
51 144
    public function isHit()
52
    {
53
        return $this->decorated->isHit()
54 6
            && $this->isDecryptable();
55
    }
56 6
57
    public function expiresAt($expiresAt)
58 6
    {
59
        $this->decorated->expiresAt($expiresAt);
60
61 12
        return $this;
62
    }
63 12
64
    public function expiresAfter($expiresAfter)
65 12
    {
66
        $this->decorated->expiresAfter($expiresAfter);
67
68
        return $this;
69
    }
70
71
    protected function generateIv()
72
    {
73
        return random_bytes(openssl_cipher_iv_length($this->cipher));
74 222
    }
75
76 222
    protected function encryptString($string, $key, $iv)
77
    {
78
        return openssl_encrypt($string, $this->cipher, $key, 0, $iv);
79 222
    }
80
81 222
    protected function decryptString($string, $method, $key, $iv)
82 222
    {
83 222
        return openssl_decrypt($string, $method, $key, 0, $iv);
84
    }
85
86 222
    protected function getCipherMethod()
87
    {
88 222
        return $this->cipher;
89
    }
90
91 132
    /**
92
     * @param $data
93 132
     * 
94
     * @return EncryptedValue
95
     */
96
    abstract protected function encrypt($data);
97
98
    /**
99
     * @param EncryptedValue $data
100
     * 
101
     * @return mixed
102
     */
103
    abstract protected function decrypt($data);
104
105
    /**
106
     * @return bool
107
     */
108
    abstract protected function isDecryptable();
109
}
110