Failed Conditions
Pull Request — master (#319)
by Guilherme
08:18
created

PersonalData::getHash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\SupportBundle\Model;
12
13
class PersonalData
14
{
15
    const HASH_ALGO = 'sha512';
16
17
    /** @var string */
18
    private $name;
19
20
    /** @var string */
21
    private $hash;
22
23
    /** @var string */
24
    private $challenge;
25
26
    /** @var string */
27
    private $value;
28
29
    /** @var bool */
30
    private $isValueFilled;
31
32
    /**
33
     * PersonalData constructor.
34
     * @param string $name
35
     * @param string $hash
36
     * @param string $value
37
     * @param bool $isValueFilled
38
     * @param string $challenge
39
     */
40 8
    public function __construct(
41
        string $name,
42
        string $value = null,
43
        bool $isValueFilled = null,
44
        string $hash = null,
45
        string $challenge = null
46
    ) {
47 8
        $this->name = $name;
48 8
        $this->value = $value;
49 8
        $this->isValueFilled = $isValueFilled ?? (bool)$value;
50 8
        $this->challenge = self::enforceChallenge($challenge);
51 8
        $this->setHash($hash);
52 7
    }
53
54 3
    public static function createWithValue(string $name, ?string $value, string $challenge = null): PersonalData
55
    {
56 3
        if (null === $value) {
57 2
            $value = '';
58 2
            $filled = false;
59
        } else {
60 2
            $filled = (bool)$value;
61
        }
62
63 3
        return new self($name, $value, $filled, null, $challenge);
64
    }
65
66 4
    public static function createWithoutValue(string $name, ?string $value, string $challenge = null): PersonalData
67
    {
68 4
        $challenge = self::enforceChallenge($challenge);
69 4
        $hash = self::generateHash(self::enforceChallenge($challenge), $value);
70
71 4
        return new self($name, null, (bool)$value, $hash, $challenge);
72
    }
73
74 3
    public function checkValue(string $value): bool
75
    {
76 3
        $userHash = $this->generateHash($this->getChallenge(), $value);
77
78 3
        return hash_equals($this->getHash(), $userHash);
79
    }
80
81 8
    private function setHash(string $hash = null)
82
    {
83 8
        if ($hash === null) {
84 4
            if ($this->value !== null) {
85 3
                $hash = $this->generateHash($this->getChallenge(), $this->getValue());
86
            } else {
87 1
                throw new \InvalidArgumentException("Hash and Value can't both be null");
88
            }
89
        }
90
91 7
        $this->hash = $hash;
92 7
    }
93
94
    /**
95
     * @return string
96
     */
97 3
    public function getName(): string
98
    {
99 3
        return $this->name;
100
    }
101
102
    /**
103
     * @return string
104
     */
105 3
    public function getHash(): string
106
    {
107 3
        return $this->hash;
108
    }
109
110
    /**
111
     * @return string
112
     */
113 4
    public function getChallenge(): string
114
    {
115 4
        return $this->challenge;
116
    }
117
118
    /**
119
     * @return string
120
     */
121 5
    public function getValue(): ?string
122
    {
123 5
        return $this->value;
124
    }
125
126 3
    public function isValueFilled(): bool
127
    {
128 3
        return $this->isValueFilled;
129
    }
130
131 7
    private static function generateHash(string $challenge, ?string $value): string
132
    {
133 7
        return hash_hmac(self::HASH_ALGO, $challenge, $value);
134
    }
135
136 8
    private static function enforceChallenge(string $challenge = null): string
137
    {
138 8
        return $challenge ?? bin2hex(random_bytes(10));
139
    }
140
141 3
    public function __toString(): string
142
    {
143 3
        if ($this->getValue() !== null) {
0 ignored issues
show
introduced by
The condition $this->getValue() !== null is always true.
Loading history...
144 2
            return $this->getValue();
145
        } else {
146 1
            return $this->isValueFilled ? 'Yes' : 'No';
147
        }
148
    }
149
}
150