Passed
Push — master ( e717c7...e31763 )
by
unknown
08:28 queued 13s
created

ValidationToken::setResourceId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Entity;
8
9
use Chamilo\CoreBundle\Repository\ValidationTokenRepository;
10
use Doctrine\ORM\Mapping as ORM;
11
12
/**
13
 * ValidationToken entity.
14
 */
15
#[ORM\Table(name: 'validation_token')]
16
#[ORM\Index(columns: ['type', 'hash'], name: 'idx_type_hash')]
17
#[ORM\Entity(repositoryClass: ValidationTokenRepository::class)]
18
class ValidationToken
19
{
20
    #[ORM\Id]
21
    #[ORM\GeneratedValue(strategy: 'IDENTITY')]
22
    #[ORM\Column(type: 'integer')]
23
    protected ?int $id = null;
24
25
    #[ORM\Column(type: 'integer')]
26
    protected int $type;
27
28
    #[ORM\Column(type: 'bigint')]
29
    protected int $resourceId;
30
31
    #[ORM\Column(type: 'string', length: 64)]
32
    protected string $hash;
33
34
    #[ORM\Column(type: 'datetime')]
35
    protected \DateTime $createdAt;
36
37
    public function __construct(int $type, int $resourceId)
38
    {
39
        $this->type = $type;
40
        $this->resourceId = $resourceId;
41
        $this->hash = hash('sha256', uniqid((string) rand(), true));
42
        $this->createdAt = new \DateTime();
43
    }
44
45
    public function getId(): ?int
46
    {
47
        return $this->id;
48
    }
49
50
    public function getType(): int
51
    {
52
        return $this->type;
53
    }
54
55
    public function setType(int $type): self
56
    {
57
        $this->type = $type;
58
        return $this;
59
    }
60
61
    public function getResourceId(): int
62
    {
63
        return $this->resourceId;
64
    }
65
66
    public function setResourceId(int $resourceId): self
67
    {
68
        $this->resourceId = $resourceId;
69
        return $this;
70
    }
71
72
    public function getHash(): string
73
    {
74
        return $this->hash;
75
    }
76
77
    public function getCreatedAt(): \DateTime
78
    {
79
        return $this->createdAt;
80
    }
81
82
    public function setCreatedAt(\DateTime $createdAt): self
83
    {
84
        $this->createdAt = $createdAt;
85
        return $this;
86
    }
87
88
    /**
89
     * Generates a validation link.
90
     */
91
    public static function generateLink(int $type, int $resourceId): string
92
    {
93
        $token = new self($type, $resourceId);
94
        return '/validate/' . $type . '/' . $token->getHash();
95
    }
96
}
97