Completed
Push — master ( 7440bd...cf598b )
by Gabriel
03:17
created

SessionController::extendSession()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 2
eloc 7
nc 2
nop 0
1
<?php
2
3
namespace Sinergi\Users\Session;
4
5
use Interop\Container\ContainerInterface;
6
use Psr\Http\Message\RequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Sinergi\Users\Container;
10
use Sinergi\Users\User\UserEntityInterface;
11
12
class SessionController
13
{
14
    private $container;
15
    private $session;
16
17 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...
18
    {
19
        if ($container instanceof Container) {
20
            $this->container = $container;
21
        } else {
22
            $this->container = new Container($container);
23
        }
24
    }
25
26
    /**
27
     * @return SessionEntity|null
28
     */
29
    public function getSession()
30
    {
31
        if (null !== $this->session) {
32
            return $this->session;
33
        }
34
35
        $headers = $this->getRequest()->headers();
0 ignored issues
show
Bug introduced by
The method getRequest() does not seem to exist on object<Sinergi\Users\Session\SessionController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
36
37
        if (isset($headers['x-session']) && $sessionId = $headers['x-session']) {
38
            $session = $this->getSessionRepository()->find($sessionId);
39
            if ($session instanceof SessionEntity && $session->isValid()) {
0 ignored issues
show
Bug introduced by
The class Sinergi\Users\Session\SessionEntity does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
40
                return $this->session = $session;
41
            }
42
        } else if ($sessionId = $this->getRequest()->cookies()
0 ignored issues
show
Bug introduced by
The method getRequest() does not seem to exist on object<Sinergi\Users\Session\SessionController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
            ->get(SessionEntity::COOKIE_NAME)
44
        ) {
45
            $session = $this->getSessionRepository()->find($sessionId);
46
            if ($session instanceof SessionEntity && $session->isValid()) {
0 ignored issues
show
Bug introduced by
The class Sinergi\Users\Session\SessionEntity does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
47
                return $this->session = $session;
48
            }
49
        }
50
51
        return null;
52
    }
53
54
    /**
55
     * @param SessionEntity|null $session
56
     */
57
    public function setSession(SessionEntity $session = null)
58
    {
59
        $this->session = $session;
60
    }
61
    
62
    public function createSession(UserEntityInterface $user, $isLongSession = false): SessionEntityInterface
63
    {
64
        $session = $this->container->get(SessionEntityInterface::class);
65
        $session->setUser($user);
66
        $session->setIsLongSession($isLongSession);
67
68
        /** @var SessionRepositoryInterface $sessionRepository */
69
        $sessionRepository = $this->container->get(SessionRepositoryInterface::class);
70
        $sessionRepository->save($session);
71
72
        return $this->session = $session;
73
    }
74
75
    /**
76
     * @return $this
77
     */
78
    public function deleteSession()
79
    {
80
        $session = $this->getSession();
81
        if ($session instanceof SessionEntity) {
0 ignored issues
show
Bug introduced by
The class Sinergi\Users\Session\SessionEntity does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
82
            $this->getDoctrine()->getEntityManager()->remove($session);
0 ignored issues
show
Bug introduced by
The method getDoctrine() does not seem to exist on object<Sinergi\Users\Session\SessionController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
83
            $this->getDoctrine()->getEntityManager()->flush($session);
0 ignored issues
show
Bug introduced by
The method getDoctrine() does not seem to exist on object<Sinergi\Users\Session\SessionController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
84
        }
85
        $this->deleteSessionCookie();
86
87
        return $this;
88
    }
89
90
    /**
91
     * @param SessionEntity $session
92
     */
93
    public function createSessionCookie(SessionEntity $session = null)
94
    {
95
        if ($session === null) {
96
            $session = $this->session;
97
        }
98
99
        if ($session !== null) {
100
            $config = $this->getConfig();
0 ignored issues
show
Bug introduced by
The method getConfig() does not seem to exist on object<Sinergi\Users\Session\SessionController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
101
            $cookie = new ResponseCookie(
102
                SessionEntity::COOKIE_NAME,
103
                $session->getId(),
104
                $session->getExpirationDatetime(),
105
                $config->get('cookie.path'),
106
                $config->get('cookie.domain'),
107
                $config->get('cookie.secure'),
108
                $config->get('cookie.httponly')
109
            );
110
111
            $this->getResponse()->cookies()->set(
0 ignored issues
show
Bug introduced by
The method getResponse() does not seem to exist on object<Sinergi\Users\Session\SessionController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
112
                $cookie->getName(),
113
                $cookie
114
            );
115
        }
116
    }
117
118
    /**
119
     * @return $this
120
     */
121
    public function deleteSessionCookie()
122
    {
123
        $this->getResponse()->cookies()->set(SessionEntity::COOKIE_NAME, null);
0 ignored issues
show
Bug introduced by
The method getResponse() does not seem to exist on object<Sinergi\Users\Session\SessionController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
124
125
        return $this;
126
    }
127
128
    /**
129
     * @return $this
130
     */
131
    public function extendSession()
132
    {
133
        $session = $this->getSession();
134
        if ($session !== null) {
135
136
            $session->createExpirationDatetime();
137
            $this->getEntityManager()->flush($this->session);
0 ignored issues
show
Bug introduced by
The method getEntityManager() does not seem to exist on object<Sinergi\Users\Session\SessionController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
138
139
            $this->createSessionCookie($this->session);
140
        }
141
142
        return $this;
143
    }
144
145
    /**
146
     * @return SessionRepository
147
     */
148
    private function getSessionRepository()
149
    {
150
        return $this->getEntityManager()
0 ignored issues
show
Bug introduced by
The method getEntityManager() does not seem to exist on object<Sinergi\Users\Session\SessionController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
151
            ->getRepository(SessionEntity::class);
152
    }
153
}
154