GuestSession::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Guests\Entity;
6
7
use Doctrine\ORM\Mapping as ORM;
8
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
9
10
/**
11
 * @ORM\Entity(repositoryClass="App\Guests\Repository\GuestRepository")
12
 * @ORM\Table(name="guest_sessions")
13
 * @UniqueEntity(fields="token", message="This token already taken")
14
 */
15
class GuestSession
16
{
17
    /**
18
     * @ORM\Id()
19
     * @ORM\GeneratedValue()
20
     * @ORM\Column(type="integer")
21
     */
22
    private $id;
23
24
    /**
25
     * @ORM\Column(type="string", length=256, unique=true)
26
     */
27
    private $token;
28
29
    /**
30
     * @var \DateTimeInterface
31
     * @ORM\Column(type="date")
32
     */
33
    private $expiresAt;
34
35
    /**
36
     * GuestSession constructor.
37
     *
38
     * @throws \Exception
39
     */
40 1
    public function __construct()
41
    {
42 1
        $this->token = bin2hex(openssl_random_pseudo_bytes(32));
43 1
        $this->expiresAt = new \DateTimeImmutable('+1 week');
44 1
    }
45
46 1
    public function getId()
47
    {
48 1
        return $this->id;
49
    }
50
51 2
    public function getToken(): string
52
    {
53 2
        return $this->token;
54
    }
55
}
56