SessionController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 14.29 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 6
dl 8
loc 56
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 2
A getSession() 0 21 4
A createSession() 0 12 1
A deleteSession() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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