|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BaseValueObject\Scalar\Password; |
|
4
|
|
|
|
|
5
|
|
|
use BaseValueObject\Scalar\BaseScalar; |
|
6
|
|
|
use BaseValueObject\Scalar\ValueObject; |
|
7
|
|
|
use Webmozart\Assert\Assert; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class SimplePassword |
|
11
|
|
|
* @package BaseValueObject\Scalar\Password |
|
12
|
|
|
*/ |
|
13
|
|
|
class SimplePassword extends BaseScalar implements Password |
|
14
|
|
|
{ |
|
15
|
|
|
const MIN_LENGTH = 6; |
|
16
|
|
|
|
|
17
|
|
|
protected $value; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @inheritdoc |
|
21
|
|
|
*/ |
|
22
|
24 |
|
public static function byCleanPassword(string $cleanPassword): Password |
|
23
|
|
|
{ |
|
24
|
24 |
|
Assert::minLength($cleanPassword, static::MIN_LENGTH); |
|
25
|
21 |
|
return new static(password_hash($cleanPassword, PASSWORD_DEFAULT)); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* SimplePassword constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $hashedPassword |
|
32
|
|
|
*/ |
|
33
|
24 |
|
public function __construct(string $hashedPassword) |
|
34
|
|
|
{ |
|
35
|
24 |
|
$this->setByHashedPassword($hashedPassword); |
|
36
|
21 |
|
} |
|
37
|
|
|
|
|
38
|
24 |
|
private function setByHashedPassword(string $hashedPassword): void |
|
39
|
|
|
{ |
|
40
|
24 |
|
$this->validHashedPassword($hashedPassword); |
|
41
|
|
|
|
|
42
|
21 |
|
$this->value = $hashedPassword; |
|
43
|
21 |
|
} |
|
44
|
|
|
|
|
45
|
24 |
|
private function validHashedPassword($hashedPassword): void |
|
46
|
|
|
{ |
|
47
|
24 |
|
if (empty(password_get_info($hashedPassword)['algo'])) { |
|
48
|
3 |
|
throw new \InvalidArgumentException( |
|
49
|
3 |
|
'It is not a hashed password.' |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
21 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
* |
|
57
|
|
|
* @throws \InvalidArgumentException if it is the same password |
|
58
|
|
|
*/ |
|
59
|
6 |
|
public function changePassword(string $cleanPassword): Password |
|
60
|
|
|
{ |
|
61
|
6 |
|
if ($this->verify($cleanPassword)) { |
|
62
|
3 |
|
throw new \InvalidArgumentException( |
|
63
|
3 |
|
'You can not update with the same password.' |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
3 |
|
return static::byCleanPassword($cleanPassword); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @inheritdoc |
|
72
|
|
|
*/ |
|
73
|
12 |
|
public function verify(string $cleanPassword): bool |
|
74
|
|
|
{ |
|
75
|
12 |
|
return password_verify($cleanPassword, $this->value()); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* {@inheritdoc} |
|
80
|
|
|
* |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
15 |
|
public function value(): string |
|
84
|
|
|
{ |
|
85
|
15 |
|
return $this->value; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* This method is not supported |
|
90
|
|
|
* |
|
91
|
|
|
* @param ValueObject $valueObject |
|
92
|
|
|
* @return bool |
|
93
|
|
|
* @throws \BadMethodCallException |
|
94
|
|
|
*/ |
|
95
|
3 |
|
public function equals(ValueObject $valueObject): bool |
|
96
|
|
|
{ |
|
97
|
3 |
|
throw new \BadMethodCallException('Equals is not supported.'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
3 |
|
public function __toString(): string |
|
104
|
|
|
{ |
|
105
|
3 |
|
return $this->value(); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|