Failed Conditions
Push — master ( 5e6761...398dce )
by Adrien
04:14 queued 01:57
created

HasPassword   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Test Coverage

Coverage 95.83%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 95
rs 10
ccs 23
cts 24
cp 0.9583
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createToken() 0 6 1
A isTokenValid() 0 9 2
A revokeToken() 0 4 1
A setPassword() 0 16 3
A getPassword() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecodev\Felix\Model\Traits;
6
7
use Cake\Chronos\Chronos;
8
use Doctrine\ORM\Mapping as ORM;
9
10
/**
11
 * Trait for a user with a password and password reset capabilities
12
 */
13
trait HasPassword
14
{
15
    /**
16
     * @var string
17
     *
18
     * @API\Exclude
19
     *
20
     * @ORM\Column(type="string", length=255)
21
     */
22
    private $password = '';
23
24
    /**
25
     * @var null|string
26
     * @ORM\Column(type="string", length=32, nullable=true, unique=true)
27
     */
28
    private $token;
29
30
    /**
31
     * @var null|Chronos
32
     *
33
     * @ORM\Column(type="datetime", nullable=true)
34
     */
35
    private $tokenCreationDate;
36
37
    /**
38
     * Hash and change the user password
39
     *
40
     * @param string $password
41
     */
42 2
    public function setPassword(string $password): void
43
    {
44
        // Ignore empty password that could be sent "by mistake" by the client
45
        // when agreeing to terms
46 2
        if ($password === '') {
47 1
            return;
48
        }
49
50 2
        $this->revokeToken();
51
52 2
        $password = password_hash($password, PASSWORD_DEFAULT);
53 2
        if (!is_string($password)) {
0 ignored issues
show
introduced by
The condition is_string($password) is always true.
Loading history...
54
            throw new \Exception('Could not hash password');
55
        }
56
57 2
        $this->password = $password;
58 2
    }
59
60
    /**
61
     * Returns the hashed password
62
     *
63
     * @API\Exclude
64
     *
65
     * @return string
66
     */
67 1
    public function getPassword(): string
68
    {
69 1
        return $this->password;
70
    }
71
72
    /**
73
     * Generate a new random token to reset password
74
     */
75 1
    public function createToken(): string
76
    {
77 1
        $this->token = bin2hex(random_bytes(16));
78 1
        $this->tokenCreationDate = new Chronos();
79
80 1
        return $this->token;
81
    }
82
83
    /**
84
     * Destroy existing token
85
     */
86 2
    public function revokeToken(): void
87
    {
88 2
        $this->token = null;
89 2
        $this->tokenCreationDate = null;
90 2
    }
91
92
    /**
93
     * Check if token is valid.
94
     *
95
     * @API\Exclude
96
     *
97
     * @return bool
98
     */
99 1
    public function isTokenValid(): bool
100
    {
101 1
        if (!$this->tokenCreationDate) {
102 1
            return false;
103
        }
104
105 1
        $timeLimit = $this->tokenCreationDate->addMinutes(30);
106
107 1
        return $timeLimit->isFuture();
108
    }
109
}
110