Failed Conditions
Push — issue#699 ( 7155bc )
by Guilherme
07:26
created

PersonalData::enforceChallenge()   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 1
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 3
    public function __construct(
41
        string $name,
42
        string $value = null,
43
        bool $isValueFilled = null,
44
        string $hash = null,
45
        string $challenge = null
46
    ) {
47 3
        $this->name = $name;
48 3
        $this->value = $value;
49 3
        $this->isValueFilled = $isValueFilled ?? (bool)$value;
50 3
        $this->challenge = self::enforceChallenge($challenge);
51 3
        $this->setHash($hash);
52 2
    }
53
54 1
    public static function createWithValue(string $name, ?string $value, string $challenge = null): PersonalData
55
    {
56 1
        if (null === $value) {
57
            $value = '';
58
        }
59 1
        return new self($name, $value, (bool)$value, null, $challenge);
60
    }
61
62 1
    public static function createWithoutValue(string $name, ?string $value, string $challenge = null): PersonalData
63
    {
64 1
        $challenge = self::enforceChallenge($challenge);
65 1
        $hash = self::generateHash(self::enforceChallenge($challenge), $value);
66
67 1
        return new self($name, null, (bool)$value, $hash, $challenge);
68
    }
69
70 2
    public function checkValue(string $value): bool
71
    {
72 2
        $userHash = $this->generateHash($this->getChallenge(), $value);
73
74 2
        return hash_equals($this->getHash(), $userHash);
75
    }
76
77 3
    private function setHash(string $hash = null)
78
    {
79 3
        if ($hash === null) {
80 2
            if ($this->value !== null) {
81 1
                $hash = $this->generateHash($this->getChallenge(), $this->getValue());
82
            } else {
83 1
                throw new \InvalidArgumentException("Hash and Value can't both be null");
84
            }
85
        }
86
87 2
        $this->hash = $hash;
88 2
    }
89
90
    /**
91
     * @return string
92
     */
93 2
    public function getName(): string
94
    {
95 2
        return $this->name;
96
    }
97
98
    /**
99
     * @return string
100
     */
101 2
    public function getHash(): string
102
    {
103 2
        return $this->hash;
104
    }
105
106
    /**
107
     * @return string
108
     */
109 2
    public function getChallenge(): string
110
    {
111 2
        return $this->challenge;
112
    }
113
114
    /**
115
     * @return string
116
     */
117 2
    public function getValue(): ?string
118
    {
119 2
        return $this->value;
120
    }
121
122
    public function isValueFilled(): bool
123
    {
124
        return $this->isValueFilled;
125
    }
126
127 2
    private static function generateHash(string $challenge, ?string $value): string
128
    {
129 2
        return hash_hmac(self::HASH_ALGO, $challenge, $value);
130
    }
131
132 3
    private static function enforceChallenge(string $challenge = null): string
133
    {
134 3
        return $challenge ?? bin2hex(random_bytes(10));
135
    }
136
137
    public function __toString(): string
138
    {
139
        if ($this->getValue() !== null) {
0 ignored issues
show
introduced by
The condition $this->getValue() !== null is always true.
Loading history...
140
            return $this->getValue();
141
        } else {
142
            return $this->isValueFilled ? 'Yes' : 'No';
143
        }
144
    }
145
}
146