Passed
Push — dependabot/npm_and_yarn/nanoid... ( aaf2c9...c4aa90 )
by
unknown
14:37 queued 06:22
created

ValidationToken   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 84
rs 10
wmc 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getType() 0 3 1
A getHash() 0 3 1
A generateLink() 0 5 1
A setResourceId() 0 5 1
A getId() 0 3 1
A getCreatedAt() 0 3 1
A setCreatedAt() 0 5 1
A getResourceId() 0 3 1
A setType() 0 5 1
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 DateTime;
11
use Doctrine\ORM\Mapping as ORM;
12
13
/**
14
 * ValidationToken entity.
15
 */
16
#[ORM\Table(name: 'validation_token')]
17
#[ORM\Index(columns: ['type', 'hash'], name: 'idx_type_hash')]
18
#[ORM\Entity(repositoryClass: ValidationTokenRepository::class)]
19
class ValidationToken
20
{
21
    #[ORM\Id]
22
    #[ORM\GeneratedValue(strategy: 'IDENTITY')]
23
    #[ORM\Column(type: 'integer')]
24
    protected ?int $id = null;
25
26
    #[ORM\Column(type: 'integer')]
27
    protected int $type;
28
29
    #[ORM\Column(type: 'bigint')]
30
    protected int $resourceId;
31
32
    #[ORM\Column(type: 'string', length: 64)]
33
    protected string $hash;
34
35
    #[ORM\Column(type: 'datetime')]
36
    protected DateTime $createdAt;
37
38
    public function __construct(int $type, int $resourceId)
39
    {
40
        $this->type = $type;
41
        $this->resourceId = $resourceId;
42
        $this->hash = hash('sha256', uniqid((string) rand(), true));
43
        $this->createdAt = new DateTime();
44
    }
45
46
    public function getId(): ?int
47
    {
48
        return $this->id;
49
    }
50
51
    public function getType(): int
52
    {
53
        return $this->type;
54
    }
55
56
    public function setType(int $type): self
57
    {
58
        $this->type = $type;
59
60
        return $this;
61
    }
62
63
    public function getResourceId(): int
64
    {
65
        return $this->resourceId;
66
    }
67
68
    public function setResourceId(int $resourceId): self
69
    {
70
        $this->resourceId = $resourceId;
71
72
        return $this;
73
    }
74
75
    public function getHash(): string
76
    {
77
        return $this->hash;
78
    }
79
80
    public function getCreatedAt(): DateTime
81
    {
82
        return $this->createdAt;
83
    }
84
85
    public function setCreatedAt(DateTime $createdAt): self
86
    {
87
        $this->createdAt = $createdAt;
88
89
        return $this;
90
    }
91
92
    /**
93
     * Generates a validation link.
94
     */
95
    public static function generateLink(int $type, int $resourceId): string
96
    {
97
        $token = new self($type, $resourceId);
98
99
        return '/validate/'.$type.'/'.$token->getHash();
100
    }
101
}
102