UserPassword::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the BenGorUser package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace BenGorUser\User\Domain\Model;
14
15
/**
16
 * User password domain class.
17
 *
18
 * @author Beñat Espiña <[email protected]>
19
 * @author Gorka Laucirica <[email protected]>
20
 */
21
final class UserPassword
22
{
23
    /**
24
     * The encoded password.
25
     *
26
     * @var string
27
     */
28
    private $encodedPassword;
29
30
    /**
31
     * The salt.
32
     *
33
     * @var string
34
     */
35
    private $salt;
36
37
    /**
38
     * Constructor.
39
     *
40
     * @param string      $anEncodedPassword The encoded password
41
     * @param string|null $salt              The salt
42
     */
43
    private function __construct($anEncodedPassword, $salt)
44
    {
45
        $this->salt = $salt;
46
        $this->encodedPassword = $anEncodedPassword;
47
    }
48
49
    /**
50
     * Named static constructor with the given encoded password.
51
     *
52
     * @param string $anEncodedPassword The encoded password
53
     * @param string $salt              The salt
54
     *
55
     * @return self
56
     */
57
    public static function fromEncoded($anEncodedPassword, $salt)
58
    {
59
        return new self($anEncodedPassword, $salt);
60
    }
61
62
    /**
63
     * Named static constructor with the given plain password and encoder.
64
     *
65
     * @param string              $aPlainPassword The plain password
66
     * @param UserPasswordEncoder $anEncoder      The encoder
67
     * @param string|null         $salt           The salt
68
     *
69
     * @return self
70
     */
71
    public static function fromPlain($aPlainPassword, UserPasswordEncoder $anEncoder, $salt = null)
72
    {
73
        if (null === $salt) {
74
            $salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
75
        }
76
        $encodedPassword = $anEncoder->encode($aPlainPassword, $salt);
77
78
        return new self($encodedPassword, $salt);
79
    }
80
81
    /**
82
     * Method that checks if the password given is equal to the current.
83
     *
84
     * @param string              $aPlainPassword The plain password
85
     * @param UserPasswordEncoder $anEncoder      The encoder
86
     *
87
     * @return bool
88
     */
89
    public function equals($aPlainPassword, UserPasswordEncoder $anEncoder)
90
    {
91
        return $anEncoder->isPasswordValid($this, $aPlainPassword);
92
    }
93
94
    /**
95
     * Gets the encoded password.
96
     *
97
     * @return string
98
     */
99
    public function encodedPassword()
100
    {
101
        return $this->encodedPassword;
102
    }
103
104
    /**
105
     * Gets the salt.
106
     *
107
     * @return string
108
     */
109
    public function salt()
110
    {
111
        return $this->salt;
112
    }
113
114
    /**
115
     * Magic method that represents the password in string format.
116
     *
117
     * @return string
118
     */
119
    public function __toString()
120
    {
121
        return $this->encodedPassword;
122
    }
123
}
124