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

Decorator::getStats()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

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 2
eloc 2
nc 1
nop 0
crap 2
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