PasswordResetToken   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 47
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getToken() 0 4 1
A validateToken() 0 8 2
A generate() 0 4 1
A equals() 0 4 1
A __toString() 0 4 1
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