Completed
Pull Request — master (#12)
by
unknown
05:50 queued 03:06
created

PasswordResetToken::generate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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