Completed
Push — master ( 9078e8...7440bd )
by Gabriel
02:37
created

SessionEntityTrait::setUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Sinergi\Users\Session;
4
5
use DateTime;
6
use DateInterval;
7
use Sinergi\Users\User\UserEntityInterface;
8
use Sinergi\Users\Utils\Token;
9
10
trait SessionEntityTrait
11
{
12
    protected $id;
13
    protected $user;
14
    protected $isLongSession = false;
15
    protected $expirationDatetime;
16
17
    public function __construct()
18
    {
19
        $this->createId();
20
        $this->createExpirationDatetime();
21
    }
22
23
    public function isValid(): bool
24
    {
25
        return !$this->isExpired() && $this->getUser() instanceof UserEntityInterface;
26
    }
27
28
    public function setExpirationDatetime(DateTime $expirationDatetime): SessionEntityInterface
29
    {
30
        $this->expirationDatetime = $expirationDatetime;
31
        return $this;
32
    }
33
34
    public function getExpirationDatetime(): DateTime
35
    {
36
        return $this->expirationDatetime;
37
    }
38
39
    public function createExpirationDatetime()
40
    {
41
        $expirationTime = $this->isLongSession() ?
42
            SessionEntityInterface::LONG_EXPIRATION_TIME : SessionEntityInterface::EXPIRATION_TIME;
43
        $expiration = (new DateTime)->add(new DateInterval($expirationTime));
44
        return $this->setExpirationDatetime($expiration);
45
    }
46
47
    public function isExpired()
48
    {
49
        return $this->getExpirationDatetime() < new DateTime;
50
    }
51
52
    public function setId(string $id): SessionEntityInterface
53
    {
54
        $this->id = $id;
55
        return $this;
56
    }
57
58
    public function getId(): string
59
    {
60
        return $this->id;
61
    }
62
63
    public function createId(): SessionEntityInterface
64
    {
65
        return $this->setId($this->generateId());
66
    }
67
68
    public function generateId(): string
69
    {
70
        return Token::generate(128);
71
    }
72
73
    public function setIsLongSession(bool $isLongSession): SessionEntityInterface
74
    {
75
        $this->isLongSession = $isLongSession;
76
        return $this;
77
    }
78
79
    public function isLongSession(): bool
80
    {
81
        return $this->isLongSession;
82
    }
83
84
    public function setUser(UserEntityInterface $user): SessionEntityInterface
85
    {
86
        $this->user = $user;
87
        return $this;
88
    }
89
90
    public function getUser(): UserEntityInterface
91
    {
92
        return $this->user;
93
    }
94
95
    public function toArray(): array
96
    {
97
        return [
98
            'id' => $this->getId(),
99
            'user' => $this->getUser()->toArray(),
100
            'isLongSession' => $this->isLongSession(),
101
            'expirationDatetime' => $this->getExpirationDatetime()->format('Y-m-d H:i:s'),
102
        ];
103
    }
104
105
    public function jsonSerialize(): array
106
    {
107
        return $this->toArray();
108
    }
109
}
110