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

OpenSslEncryptionTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
c 1
b 0
f 1
lcom 2
cbo 1
dl 0
loc 52
ccs 15
cts 15
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 8 3
isHit() 0 1 ?
getDecorated() 0 1 ?
encrypt() 0 1 ?
decrypt() 0 1 ?
A generateIv() 0 6 1
A encryptString() 0 4 1
A decryptString() 0 4 1
A getCipherMethod() 0 4 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