Completed
Pull Request — master (#29)
by Valentyn
07:01
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
c 0
b 0
f 0
dl 0
loc 4
rs 10
ccs 0
cts 0
cp 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace App\Users\Entity;
5
6
use App\Users\Entity\User;
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
19
    /**
20
     * @ORM\Id()
21
     * @ORM\GeneratedValue()
22
     * @ORM\Column(type="integer")
23
     */
24
    private $id;
25
26
    /**
27
     * @var $user User
28
     * @ORM\ManyToOne(targetEntity="App\Users\Entity\User")
29
     */
30
    private $user;
31
32
    /**
33
     * @ORM\Column(type="string", length=32, unique=true)
34
     */
35
    private $token;
36
37
    /**
38
     * @ORM\Column(type="string", length=256)
39
     */
40
    private $type;
41
42
    /**
43
     * @ORM\Column(type="datetime", nullable=true)
44
     */
45
    private $expires_at;
46
47
    public function __construct(User $user, $type)
48
    {
49 5
        if (in_array($type, $this->getValidTypes()) === false) {
50
            throw new \InvalidArgumentException(sprintf('$type should be valid type! Instead %s given', $type));
51 5
        }
52
53
        $this->type = $type;
54
        $this->token = bin2hex(openssl_random_pseudo_bytes(16));
55 5
        $this->user = $user;
56 5
    }
57 5
58 5
    public function getValidTypes(): array
59
    {
60
        return [self::TYPE_CONFIRM_EMAIl];
61
    }
62
63
    /**
64 5
     * @param \DateTimeInterface $expires_at
65
     * @return ConfirmationToken
66 5
     */
67 5
    public function setExpiresAt(\DateTimeInterface $expires_at)
68
    {
69
        $this->expires_at = $expires_at;
70
        return $this;
71
    }
72
73
    /**
74
     * @return mixed
75
     */
76
    public function getId()
77
    {
78
        return $this->id;
79
    }
80
81 1
    /**
82
     * @return User
83 1
     */
84
    public function getUser(): User
85
    {
86
        return $this->user;
87
    }
88
89 5
    /**
90
     * @return mixed
91 5
     */
92
    public function getToken()
93
    {
94
        return $this->token;
95
    }
96
97
    /**
98
     * @return mixed
99
     */
100
    public function getType()
101
    {
102
        return $this->type;
103
    }
104
105
    /**
106
     * @return mixed
107
     */
108
    public function getExpiresAt()
109
    {
110
        return $this->expires_at;
111
    }
112
}