Completed
Pull Request — master (#12)
by
unknown
07:31 queued 04:16
created

PasswordResetToken::validateToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
c 4
b 1
f 1
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
namespace SumoCoders\FrameworkMultiUserBundle\Security;
4
5
use SumoCoders\FrameworkMultiUserBundle\Exception\InvalidPasswordResetTokenException;
6
use SumoCoders\FrameworkMultiUserBundle\User\UserInterface;
7
8
class PasswordResetToken
9
{
10
    /**
11
     * @var string
12
     */
13
    private $token;
14
15
    /**
16
     * PasswordResetToken constructor.
17
     *
18
     * @param string $token
19
     */
20
    public function __construct($token)
21
    {
22
        $this->token = $token;
23
    }
24
25
    /**
26
     * @return string
27
     */
28
    public function getToken()
29
    {
30
        return $this->token;
31
    }
32
33
    /**
34
     * @param UserInterface $user
35
     * @param $token
36
     *
37
     * @throws InvalidPasswordResetTokenException
38
     *
39
     * @return bool
40
     */
41
    public static function validateToken(UserInterface $user, PasswordResetToken $token)
42
    {
43
        if ($user->getPasswordResetToken()->equals($token)) {
0 ignored issues
show
Bug introduced by
The method getPasswordResetToken() does not exist on SumoCoders\FrameworkMult...ndle\User\UserInterface. Did you maybe mean getPassword()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
44
            return true;
45
        }
46
47
        throw new InvalidPasswordResetTokenException('The given token is not valid.');
48
    }
49
50
    /**
51
     * Generates a PasswordToken
52
     *
53
     * @return PasswordResetToken
54
     */
55
    public static function generate()
56
    {
57
        $token = time() . base64_encode(random_bytes(10));
58
59
        return new self($token);
60
    }
61
62
    /**
63
     * Check if a token is equal to a different token.
64
     *
65
     * @param PasswordResetToken $token
66
     *
67
     * @return bool
68
     */
69
    public function equals(PasswordResetToken $token)
70
    {
71
        return $token->token === $this->token;
72
    }
73
}
74