Completed
Pull Request — master (#3)
by Jonathan
01:58
created

Decorator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 68
Duplicated Lines 16.18 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 96.88%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 9
c 1
b 0
f 1
lcom 1
cbo 4
dl 11
loc 68
ccs 31
cts 32
cp 0.9688
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A fetch() 0 11 2
A contains() 0 12 2
A save() 11 11 1
A delete() 0 4 1
A getStats() 0 4 2

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\IronEncryption;
3
4
use Doctrine\Common\Cache\Cache;
5
use Exception;
6
use Jsq\Cache\EncryptionStatsTrait;
7
use Jsq\Iron;
8
use Jsq\Iron\PasswordInterface;
9
use Jsq\Iron\Token;
10
11
class Decorator implements Cache
12
{
13
    use EncryptionStatsTrait;
14
15
    /** @var Cache */
16
    private $decorated;
17
    /** @var Iron\Iron */
18
    private $iron;
19
    /** @var PasswordInterface */
20
    private $password;
21
22 114
    public function __construct(
23
        Cache $decorated,
24
        $password,
25
        $cipher = Iron\Iron::DEFAULT_ENCRYPTION_METHOD
26
    ) {
27 114
        $this->decorated = $decorated;
28 114
        $this->password = Iron\normalize_password($password);
29 114
        $this->iron = new Iron\Iron($cipher);
30 114
    }
31
32 78
    public function fetch($id)
33
    {
34
        try {
35 78
            return $this->returnHit($this->callAndTime(
36 78
                [$this->iron, 'decrypt'],
37 78
                [$this->password, $this->decorated->fetch($id)]
38 78
            ));
39 33
        } catch (Exception $e) {
40 33
            return $this->returnMiss(false);
41
        }
42
    }
43
44 18
    public function contains($id)
45
    {
46
        try {
47 18
            Token::fromSealed(
48 18
                $this->password,
49 18
                $this->decorated->fetch($id)
50 18
            );
51
            return true;
52 18
        } catch (Exception $e) {
53 18
            return false;
54
        }
55
    }
56
57 60 View Code Duplication
    public function save($id, $data, $ttl = 0)
1 ignored issue
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...
58
    {
59 60
        return $this->decorated->save(
60 60
            $id,
61 60
            (string) $this->callAndTime(
62 60
                [$this->iron, 'encrypt'],
63 60
                [$this->password, $data, $ttl]
64 60
            ),
65
            $ttl
66 60
        );
67
    }
68
69 3
    public function delete($id)
70
    {
71 3
        return $this->decorated->delete($id);
72
    }
73
74 45
    public function getStats()
75
    {
76 45
        return $this->getEncryptionStats($this->decorated->getStats() ?: []);
77
    }
78
}
79