Completed
Push — master ( 7aadd0...e03480 )
by Jonathan
10s
created

OpenSslEncryptionTrait::getCipherMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace Jsq\CacheEncryption;
3
4
use Psr\Cache\CacheItemInterface;
5
6
trait OpenSslEncryptionTrait
7
{
8
    /** @var string */
9
    private $cipher;
10
    /** @var mixed */
11
    private $decrypted;
12
13 174
    public function get()
14
    {
15 174
        if (empty($this->decrypted) && $this->isHit()) {
16 132
            $this->decrypted = $this->decrypt($this->getDecorated()->get());
17 132
        }
18
19 174
        return $this->decrypted;
20
    }
21
22
    /**
23
     * @return bool
24
     */
25
    abstract public function isHit();
26
27
    /**
28
     * @return CacheItemInterface
29
     */
30
    abstract public function getDecorated();
31
32
    abstract protected function encrypt($data);
33
34
    abstract protected function decrypt($data);
35
36 222
    private function generateIv()
37
    {
38 222
        return openssl_random_pseudo_bytes(
39 222
            openssl_cipher_iv_length($this->cipher)
40 222
        );
41
    }
42
43 222
    private function encryptString($string, $key, $iv)
44
    {
45 222
        return openssl_encrypt($string, $this->cipher, $key, 0, $iv);
46
    }
47
48 132
    private function decryptString($string, $method, $key, $iv)
49
    {
50 132
        return openssl_decrypt($string, $method, $key, 0, $iv);
51
    }
52
53 222
    private function getCipherMethod()
54
    {
55 222
        return $this->cipher;
56
    }
57
}
58