SessionEntityTrait   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 25
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 129
rs 10

19 Methods

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