1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sinergi\Users\Session; |
4
|
|
|
|
5
|
|
|
use Interop\Container\ContainerInterface; |
6
|
|
|
use Sinergi\Users\Container; |
7
|
|
|
use Sinergi\Users\Session\Exception\InvalidSessionException; |
8
|
|
|
use Sinergi\Users\Session\Exception\SessionExpiredException; |
9
|
|
|
use Sinergi\Users\Session\Exception\SessionNotFoundException; |
10
|
|
|
use Sinergi\Users\User\UserEntityInterface; |
11
|
|
|
use Sinergi\Users\User\UserRepositoryInterface; |
12
|
|
|
|
13
|
|
|
class SessionController |
14
|
|
|
{ |
15
|
|
|
private $container; |
16
|
|
|
private $session; |
17
|
|
|
|
18
|
|
View Code Duplication |
public function __construct(ContainerInterface $container) |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
if ($container instanceof Container) { |
21
|
|
|
$this->container = $container; |
22
|
|
|
} else { |
23
|
|
|
$this->container = new Container($container); |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getSession(string $id): SessionEntityInterface |
28
|
|
|
{ |
29
|
|
|
/** @var SessionRepositoryInterface $sessionRepository */ |
30
|
|
|
$sessionRepository = $this->container->get(SessionRepositoryInterface::class); |
31
|
|
|
|
32
|
|
|
/** @var UserRepositoryInterface $sessionRepository */ |
33
|
|
|
$userRepository = $this->container->get(UserRepositoryInterface::class); |
34
|
|
|
|
35
|
|
|
$session = $sessionRepository->findById($id); |
36
|
|
|
if ($session instanceof SessionEntityInterface) { |
37
|
|
|
$session->setUserRepository($userRepository); |
38
|
|
|
if ($session->isExpired()) { |
39
|
|
|
throw new SessionExpiredException; |
40
|
|
|
} elseif (!$session->isValid()) { |
41
|
|
|
throw new InvalidSessionException; |
42
|
|
|
} |
43
|
|
|
return $this->session = $session; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
throw new SessionNotFoundException; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function createSession(UserEntityInterface $user, $isLongSession = false): SessionEntityInterface |
50
|
|
|
{ |
51
|
|
|
$session = $this->container->get(SessionEntityInterface::class); |
52
|
|
|
$session->setUser($user); |
53
|
|
|
$session->setIsLongSession($isLongSession); |
54
|
|
|
|
55
|
|
|
/** @var SessionRepositoryInterface $sessionRepository */ |
56
|
|
|
$sessionRepository = $this->container->get(SessionRepositoryInterface::class); |
57
|
|
|
$sessionRepository->save($session); |
58
|
|
|
|
59
|
|
|
return $this->session = $session; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function deleteSession(SessionEntityInterface $session) |
63
|
|
|
{ |
64
|
|
|
/** @var SessionRepositoryInterface $sessionRepository */ |
65
|
|
|
$sessionRepository = $this->container->get(SessionRepositoryInterface::class); |
66
|
|
|
$sessionRepository->delete($session); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.