Completed
Push — master ( e0306e...074f1a )
by Jonathan
8s
created

Decorator::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 11
loc 11
ccs 8
cts 8
cp 1
rs 9.4285
cc 1
eloc 7
nc 1
nop 3
crap 1
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 129
    public function __construct(
23
        Cache $decorated,
24
        $password,
25
        $cipher = Iron\Iron::DEFAULT_ENCRYPTION_METHOD
26
    ) {
27 129
        $this->decorated = $decorated;
28 129
        $this->password = Iron\normalize_password($password);
29 129
        $this->iron = new Iron\Iron($cipher);
30 129
    }
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 33
    public function contains($id)
45
    {
46
        try {
47 33
            Token::fromSealed(
48 33
                $this->password,
49 33
                $this->decorated->fetch($id)
50 33
            );
51 15
            return true;
52 18
        } catch (Exception $e) {
53 18
            return false;
54
        }
55
    }
56
57 75 View Code Duplication
    public function save($id, $data, $ttl = 0)
58
    {
59 75
        return $this->decorated->save(
60 75
            $id,
61 75
            (string) $this->callAndTime(
62 75
                [$this->iron, 'encrypt'],
63 75
                [$this->password, $data, $ttl]
64 75
            ),
65
            $ttl
66 75
        );
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