CookieIdentity   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
eloc 22
dl 0
loc 104
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getHash() 0 3 1
A getKey() 0 3 1
A matchKey() 0 3 1
A makeHash() 0 3 1
A setHash() 0 3 1
A setKey() 0 5 1
A generateNewKey() 0 5 1
A setSeries() 0 3 1
A getSeries() 0 3 1
A getFingerprint() 0 3 1
A generateNewSeries() 0 3 1
1
<?php
2
3
namespace Palladium\Entity;
4
5
class CookieIdentity extends Identity
6
{
7
8
    const SERIES_SIZE = 16;
9
    const KEY_SIZE = 32;
10
11
    private $series;
12
    private $key;
13
    private $hash;
14
15
    protected $type = Identity::TYPE_COOKIE;
16
17
18 11
    public function setSeries(string $series = null)
19
    {
20 11
        $this->series = $series;
21 11
    }
22
23
24
    /**
25
     * @codeCoverageIgnore
26
     */
27
    public function getSeries()
28
    {
29
        return $this->series;
30
    }
31
32
33
    /**
34
     * Produces a hash from series to obscure it for storage.
35
     */
36 12
    public function getFingerprint(): string
37
    {
38 12
        return hash('sha384', $this->series);
39
    }
40
41
42 10
    public function generateNewSeries()
43
    {
44 10
        $this->series = bin2hex(random_bytes(CookieIdentity::SERIES_SIZE));
45 10
    }
46
47
48
    /**
49
     * Assigns a new identification key and resets a the hash.
50
     *
51
     * @param string $key
52
     */
53 5
    public function setKey(string $key = null)
54
    {
55 5
        $this->hash = null;
56 5
        $this->key = $key;
57 5
        $this->hash = $this->makeHash($key);
58 5
    }
59
60
61
    /**
62
     * @codeCoverageIgnore
63
     * @return string|null
64
     */
65
    public function getKey()
66
    {
67
        return $this->key;
68
    }
69
70
71
    /**
72
     * Sets a new key and resets the hash.
73
     */
74 11
    public function generateNewKey()
75
    {
76 11
        $key = bin2hex(random_bytes(CookieIdentity::KEY_SIZE));
77 11
        $this->key = $key;
78 11
        $this->hash = $this->makeHash($key);
79 11
    }
80
81
82
    /**
83
     * @codeCoverageIgnore
84
     */
85
    public function getHash()
86
    {
87
        return $this->hash;
88
    }
89
90
91 16
    private function makeHash($key): string
92
    {
93 16
        return hash('sha384', $key);
94
    }
95
96
97
    /**
98
     * @param string $hash
99
     */
100 11
    public function setHash(string $hash = null)
101
    {
102 11
        $this->hash = $hash;
103 11
    }
104
105
106 6
    public function matchKey($key): bool
107
    {
108 6
        return  $this->makeHash($key) === $this->hash;
109
    }
110
}
111