Completed
Pull Request — master (#12)
by
unknown
03:16
created

PasswordResetToken::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
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|null $token
19
     */
20
    public function __construct($token = null)
21
    {
22
        $this->token = $this->generateToken();
23
24
        if ($token) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $token of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
25
            $this->token = $token;
26
        }
27
    }
28
29
    /**
30
     * @return string
31
     */
32
    public function getToken()
33
    {
34
        return $this->token;
35
    }
36
37
    /**
38
     * Generates a token string.
39
     *
40
     * @return string
41
     */
42
    private function generateToken()
43
    {
44
        return time() . base64_encode(random_bytes(10));
45
    }
46
47
    /**
48
     * @param UserInterface $user
49
     * @param $token
50
     *
51
     * @throws InvalidPasswordResetTokenException
52
     *
53
     * @return bool
54
     */
55
    public static function validateToken(UserInterface $user, PasswordResetToken $token)
56
    {
57
        if ($user->getPasswordResetToken() === $token->getToken()) {
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...
58
            return true;
59
        }
60
61
        throw new InvalidPasswordResetTokenException('The given token is not valid.');
62
    }
63
64
    /**
65
     * Generates a PasswordToken
66
     *
67
     * @return PasswordResetToken
68
     */
69
    public static function generate()
70
    {
71
        return new self();
72
    }
73
}
74