Completed
Push — master ( c9d345...34b48c )
by Valentyn
03:02 queued 01:25
created

ConfirmationToken::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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