|
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\User\UserRepositoryInterface; |
|
9
|
|
|
use Sinergi\Users\Utils\Token; |
|
10
|
|
|
|
|
11
|
|
|
trait SessionEntityTrait |
|
12
|
|
|
{ |
|
13
|
|
|
protected $id; |
|
14
|
|
|
protected $userId; |
|
15
|
|
|
protected $user; |
|
16
|
|
|
/** @var UserRepositoryInterface */ |
|
17
|
|
|
protected $userRepository; |
|
18
|
|
|
protected $isLongSession = false; |
|
19
|
|
|
protected $expirationDatetime; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct() |
|
22
|
|
|
{ |
|
23
|
|
|
$this->createId(); |
|
24
|
|
|
$this->createExpirationDatetime(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function isValid(): bool |
|
28
|
|
|
{ |
|
29
|
|
|
return !$this->isExpired() && $this->getUser() instanceof UserEntityInterface; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function setExpirationDatetime(DateTime $expirationDatetime): SessionEntityInterface |
|
33
|
|
|
{ |
|
34
|
|
|
$this->expirationDatetime = $expirationDatetime; |
|
35
|
|
|
return $this; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function getExpirationDatetime(): DateTime |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->expirationDatetime; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function createExpirationDatetime() |
|
44
|
|
|
{ |
|
45
|
|
|
$expirationTime = $this->isLongSession() ? |
|
46
|
|
|
SessionEntityInterface::LONG_EXPIRATION_TIME : SessionEntityInterface::EXPIRATION_TIME; |
|
47
|
|
|
$expiration = (new DateTime)->add(new DateInterval($expirationTime)); |
|
48
|
|
|
return $this->setExpirationDatetime($expiration); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function isExpired() |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->getExpirationDatetime() < new DateTime; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function setId(string $id): SessionEntityInterface |
|
57
|
|
|
{ |
|
58
|
|
|
$this->id = $id; |
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getId(): string |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->id; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function createId(): SessionEntityInterface |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->setId($this->generateId()); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function generateId(): string |
|
73
|
|
|
{ |
|
74
|
|
|
return Token::generate(128); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function setIsLongSession(bool $isLongSession): SessionEntityInterface |
|
78
|
|
|
{ |
|
79
|
|
|
$this->isLongSession = $isLongSession; |
|
80
|
|
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function isLongSession(): bool |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->isLongSession; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function setUserId(int $userId): SessionEntityInterface |
|
89
|
|
|
{ |
|
90
|
|
|
$this->userId = $userId; |
|
91
|
|
|
return $this; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function getUserId(): int |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->userId; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function getUser(): UserEntityInterface |
|
100
|
|
|
{ |
|
101
|
|
|
if ($this->user) { |
|
102
|
|
|
return $this->user; |
|
103
|
|
|
} elseif (!$this->userRepository) { |
|
104
|
|
|
throw new \Exception('Cannot fetch user without user repository'); |
|
105
|
|
|
} |
|
106
|
|
|
return $this->user = $this->userRepository->findById($this->getUserId()); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function setUser(UserEntityInterface $user): SessionEntityInterface |
|
110
|
|
|
{ |
|
111
|
|
|
$this->setUserId($user->getId()); |
|
112
|
|
|
$this->user = $user; |
|
113
|
|
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function setUserRepository(UserRepositoryInterface $userRepository): SessionEntityInterface |
|
117
|
|
|
{ |
|
118
|
|
|
$this->userRepository = $userRepository; |
|
119
|
|
|
return $this; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function toArray(): array |
|
123
|
|
|
{ |
|
124
|
|
|
return [ |
|
125
|
|
|
'id' => $this->getId(), |
|
126
|
|
|
'userId' => $this->getUserId(), |
|
127
|
|
|
'isLongSession' => $this->isLongSession(), |
|
128
|
|
|
'expirationDatetime' => $this->getExpirationDatetime()->format('Y-m-d H:i:s'), |
|
129
|
|
|
]; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function jsonSerialize() |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->toArray(); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|