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

Decorator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 18.97 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A isDataDecryptable() 0 5 2
A encrypt() 0 17 1
A decrypt() 11 11 2
A authenticate() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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