Completed
Push — master ( 074f1a...50e4ad )
by Jonathan
14:34
created

Decorator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.6666
cc 1
eloc 7
nc 1
nop 3
crap 1
1
<?php
2
namespace Jsq\Cache\PasswordEncryption;
3
4
use Doctrine\Common\Cache\Cache;
5
use Jsq\Cache\EncryptedValue;
6
use Jsq\Cache\EncryptingDecorator;
7
8
class Decorator extends EncryptingDecorator
9
{
10
    /** @var string */
11
    private $cipher;
12
    /** @var string */
13
    private $passphrase;
14 84
15
    public function __construct(
16
        Cache $decorated,
17
        $passphrase,
18
        $cipher = 'aes-256-cbc'
19 84
    ) {
20 84
        parent::__construct($decorated);
21 84
        $this->passphrase = $passphrase;
22 84
        $this->cipher = $cipher;
23
    }
24 57
25
    protected function isDataDecryptable($data, string $id): bool
26
    {
27 57
        return $data instanceof Value
28
            && $data->getMac() === $this->authenticate($id, $data->getCipherText());
29
    }
30 36
31
    protected function encrypt($data, string $id): EncryptedValue
32 36
    {
33 36
        $iv = $this->generateIv($this->cipher);
34 36
        $cipherText = $this->encipher(
35 36
            serialize($data),
36 36
            $this->cipher,
37
            $this->passphrase,
38 36
            $iv
39
        );
40 36
41 36
        return new Value(
42 36
            $cipherText,
43 36
            $this->cipher,
44 36
            $iv,
45 36
            $this->authenticate($id, $cipherText)
46
        );
47
    }
48 18
49 View Code Duplication
    protected function decrypt($data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50 18
    {
51
        if (!$data instanceof Value) return false;
52 18
53 18
        return unserialize($this->decipher(
54 18
            $data->getCipherText(),
55 18
            $data->getMethod(),
56 18
            $this->passphrase,
57 18
            $data->getInitializationVector()
58
        ));
59
    }
60 36
61
    private function authenticate($key, $cipherText)
62 36
    {
63
        return $this->hmac($cipherText, $this->hmac($key, $this->passphrase));
64
    }
65
}
66