PasswordResetToken::generate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SumoCoders\FrameworkMultiUserBundle\Security;
4
5
use SumoCoders\FrameworkMultiUserBundle\Exception\InvalidPasswordResetTokenException;
6
use SumoCoders\FrameworkMultiUserBundle\User\Interfaces\User;
7
8
class PasswordResetToken
9
{
10
    /** @var string */
11
    private $token;
12
13
    public function __construct(string $token)
14
    {
15
        $this->token = $token;
16
    }
17
18
    public function getToken(): string
19
    {
20
        return $this->token;
21
    }
22
23
    /**
24
     * @param User $user
25
     * @param PasswordResetToken $token
26
     *
27
     * @throws InvalidPasswordResetTokenException
28
     *
29
     * @return bool
30
     */
31
    public static function validateToken(User $user, PasswordResetToken $token): bool
32
    {
33
        if ($user->getPasswordResetToken()->equals($token)) {
34
            return true;
35
        }
36
37
        throw new InvalidPasswordResetTokenException('The given token is not valid.');
38
    }
39
40
    public static function generate(): PasswordResetToken
41
    {
42
        return new self(uniqid());
43
    }
44
45
    public function equals(PasswordResetToken $token): bool
46
    {
47
        return $token->token === $this->token;
48
    }
49
50
    public function __toString(): string
51
    {
52
        return $this->getToken();
53
    }
54
}
55