Completed
Push — master ( f446cd...59ed7d )
by Valentyn
13:46 queued 11:33
created

ConfirmationToken::isValid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 4
cts 5
cp 0.8
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.032
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Users\Entity;
6
7
use Doctrine\ORM\Mapping as ORM;
8
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
9
10
/**
11
 * @ORM\Entity(repositoryClass="App\Users\Repository\ConfirmationTokenRepository")
12
 * @ORM\Table(name="users_confirmation_tokens")
13
 * @UniqueEntity(fields="token", message="This token already taken")
14
 */
15
class ConfirmationToken
16
{
17
    const TYPE_CONFIRM_EMAIL = 'confirm_email';
18
    const TYPE_PASSWORD_RECOVERY = 'password_recovery';
19
20
    /**
21
     * @ORM\Id()
22
     * @ORM\GeneratedValue()
23
     * @ORM\Column(type="integer")
24
     */
25
    private $id;
26
27
    /**
28
     * @var User
29
     * @ORM\ManyToOne(targetEntity="App\Users\Entity\User")
30
     */
31
    private $user;
32
33
    /**
34
     * @ORM\Column(type="string", length=32, unique=true)
35
     */
36
    private $token;
37
38
    /**
39
     * @ORM\Column(type="string", length=256)
40
     */
41
    private $type;
42
43
    /**
44
     * @ORM\Column(type="datetime", nullable=true)
45
     *
46
     * @var \DateTimeImmutable|\DateTimeInterface
47
     */
48
    private $expires_at;
49
50
    /**
51
     * ConfirmationToken constructor.
52
     *
53
     * @param \App\Users\Entity\User $user
54
     * @param $type
55
     * @param \DateTimeInterface|null $expires_at
56
     *
57
     * @throws \Exception
58
     */
59 9
    public function __construct(User $user, $type, \DateTimeInterface $expires_at = null)
60
    {
61 9
        if (in_array($type, $this->getValidTypes(), true) === false) {
62 1
            throw new \InvalidArgumentException(sprintf('$type should be valid type! Instead %s given', $type));
63
        }
64
65 8
        if ($expires_at !== null) {
66 6
            $now = new \DateTimeImmutable();
67 6
            if ($expires_at <= $now) {
68 1
                throw new \InvalidArgumentException(sprintf('You can not create already expired token'));
69
            }
70
        }
71
72 7
        $this->type = $type;
73 7
        $this->token = bin2hex(openssl_random_pseudo_bytes(16));
74 7
        $this->user = $user;
75 7
        $this->expires_at = $expires_at;
76 7
    }
77
78 9
    public function getValidTypes(): array
79
    {
80 9
        return [self::TYPE_CONFIRM_EMAIL, self::TYPE_PASSWORD_RECOVERY];
81
    }
82
83
    /**
84
     * @return mixed
85
     */
86
    public function getId()
87
    {
88
        return $this->id;
89
    }
90
91
    /**
92
     * @return User
93
     */
94 2
    public function getUser(): User
95
    {
96 2
        return $this->user;
97
    }
98
99
    /**
100
     * @return mixed
101
     */
102 4
    public function getToken()
103
    {
104 4
        return $this->token;
105
    }
106
107
    /**
108
     * @return mixed
109
     */
110
    public function getType()
111
    {
112
        return $this->type;
113
    }
114
115
    /**
116
     * @throws \Exception
117
     *
118
     * @return bool
119
     */
120 2
    public function isValid(): bool
121
    {
122 2
        if (!$this->expires_at) {
123
            return true;
124
        }
125
126 2
        $now = new \DateTimeImmutable();
127
128 2
        return $this->expires_at > $now;
129
    }
130
}
131