Completed
Push — master ( 7b7eb1...9078e8 )
by Gabriel
02:39
created

AuthenticationController::disconnectUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Sinergi\Users\Authentication;
4
5
use Exception;
6
use Interop\Container\ContainerInterface;
7
8
class AuthenticationController
9
{
10
    private $container;
11
12
    public function __construct(ContainerInterface $container)
13
    {
14
        $this->container = $container;
15
    }
16
17
    /**
18
     * @return bool
19
     */
20
    public function isAuthenticated()
21
    {
22
        try {
23
            $this->getAuthenticatedUser();
24
25
            return true;
26
        } catch (Exception $e) {
27
            return false;
28
        }
29
    }
30
31
    /**
32
     * @param array $parameters
33
     * @return UserEntity
34
     * @throws AuthenticationException
35
     */
36
    public function getUserByEmailAndPassword(array $parameters)
37
    {
38
        $user = $this->getUserRepository()
0 ignored issues
show
Bug introduced by
The method getUserRepository() does not exist on Sinergi\Users\Authentica...uthenticationController. Did you maybe mean getUser()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
39
            ->findOneByEmail($parameters['email']);
40
41
        if ($user instanceof UserEntity && $user->testPassword($parameters['password'])) {
0 ignored issues
show
Bug introduced by
The class Sinergi\Users\Authentication\UserEntity 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...
42
            return $user;
43
        }
44
45
        throw new AuthenticationException();
46
    }
47
48
    public function login(string $email, string $password, bool $isLongSession = false)
0 ignored issues
show
Unused Code introduced by
The parameter $email is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $password is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $isLongSession is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
    {
50
        
51
        if ($parameters instanceof UserEntity) {
0 ignored issues
show
Bug introduced by
The class Sinergi\Users\Authentication\UserEntity 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...
52
            $user = $parameters;
0 ignored issues
show
Bug introduced by
The variable $parameters does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
53
        } else {
54
            $user = $this->getUserRepository()
0 ignored issues
show
Bug introduced by
The method getUserRepository() does not exist on Sinergi\Users\Authentica...uthenticationController. Did you maybe mean getUser()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
55
                ->findOneByEmail($parameters['email']);
56
        }
57
58
        if (!($user instanceof UserEntity)
0 ignored issues
show
Bug introduced by
The class Sinergi\Users\Authentication\UserEntity 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...
59
            || (is_array($parameters)
60
                && !$user->testPassword($parameters['password']))
61
        ) {
62
            throw new AuthenticationException(
63
                $this->getDictionary()
0 ignored issues
show
Bug introduced by
The method getDictionary() does not seem to exist on object<Sinergi\Users\Aut...thenticationController>.

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...
64
                    ->get('user.authentication.error.invalid_credentials')
65
            );
66
        }
67
68
        if (!$user->isActive()) {
69
            $statusLabel = (new UserController($this->getContainer()))
0 ignored issues
show
Bug introduced by
The method getContainer() does not seem to exist on object<Sinergi\Users\Aut...thenticationController>.

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...
70
                ->getUserStatusLabel($user->getStatus());
71
72
            $accountDisabledError = $this->getDictionary()
0 ignored issues
show
Bug introduced by
The method getDictionary() does not seem to exist on object<Sinergi\Users\Aut...thenticationController>.

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...
73
                ->get('user.authentication.error.account_disabled');
74
75
            throw new AuthenticationException(
76
                sprintf($accountDisabledError, $statusLabel)
77
            );
78
        }
79
80
        try {
81
            $longSession = is_array($parameters) ?
82
                (bool)$parameters['is_long_session'] : false;
83
84
            $this->setSession(
0 ignored issues
show
Bug introduced by
The method setSession() does not seem to exist on object<Sinergi\Users\Aut...thenticationController>.

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...
85
86
                $this->getContainer()->getSessionController()->createSession(
0 ignored issues
show
Bug introduced by
The method getContainer() does not seem to exist on object<Sinergi\Users\Aut...thenticationController>.

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...
87
                    $user,
88
                    $longSession
89
                )
90
            );
91
92
            $this->triggerEvent('user.login');
93
94 View Code Duplication
            if (!$user->isEmailConfirmed()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
95
                throw new AuthenticationException(
96
                    $this->getDictionary()
0 ignored issues
show
Bug introduced by
The method getDictionary() does not seem to exist on object<Sinergi\Users\Aut...thenticationController>.

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...
97
                        ->get('user.authentication.error.email_not_confirmed')
98
                    . '<br><a href="#" data-action="resend-confirmation-email">' . $this->getDictionary()
0 ignored issues
show
Bug introduced by
The method getDictionary() does not seem to exist on object<Sinergi\Users\Aut...thenticationController>.

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...
99
                        ->get('user.authentication.error.resend_confirmation_email')
100
                    . '</a>'
101
                );
102
            }
103
            return $user;
104
        } catch (AuthenticationException $e) {
0 ignored issues
show
Bug introduced by
The class Sinergi\Users\Authentica...AuthenticationException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
105
            throw $e;
106
        } catch (SessionCreationException $e) {
0 ignored issues
show
Bug introduced by
The class Sinergi\Users\Authentica...essionCreationException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
107
            throw new AuthenticationException(
108
                $this->getDictionary()
0 ignored issues
show
Bug introduced by
The method getDictionary() does not seem to exist on object<Sinergi\Users\Aut...thenticationController>.

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...
109
                    ->get('user.authentication.error.internal_error')
110
            );
111
        }
112
    }
113
114
    public function disconnectUser()
115
    {
116
        $this->getContainer()->getSessionController()->deleteSession();
0 ignored issues
show
Bug introduced by
The method getContainer() does not seem to exist on object<Sinergi\Users\Aut...thenticationController>.

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...
117
        $this->triggerEvent('user.logout');
118
    }
119
120
    /**
121
     * @return UserEntity
122
     * @throws Exception
123
     */
124
    public function getAuthenticatedUser()
125
    {
126
        if (isset($this->user)) {
127
            return $this->user;
0 ignored issues
show
Bug introduced by
The property user does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
128
        }
129
130
        $session = $this->getSession();
0 ignored issues
show
Bug introduced by
The method getSession() does not seem to exist on object<Sinergi\Users\Aut...thenticationController>.

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...
131
        if ($session instanceof SessionEntity) {
0 ignored issues
show
Bug introduced by
The class Sinergi\Users\Authentication\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...
132
133 View Code Duplication
            if (!$session->getUser()->isEmailConfirmed()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
134
                throw new AuthenticationException(
135
                    $this->getDictionary()
0 ignored issues
show
Bug introduced by
The method getDictionary() does not seem to exist on object<Sinergi\Users\Aut...thenticationController>.

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...
136
                        ->get('user.authentication.error.email_not_confirmed')
137
                    . '<br><a href="#" data-action="resend-confirmation-email">' . $this->getDictionary()
0 ignored issues
show
Bug introduced by
The method getDictionary() does not seem to exist on object<Sinergi\Users\Aut...thenticationController>.

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
                        ->get('user.authentication.error.resend_confirmation_email')
139
                    . '</a>'
140
                );
141
            }
142
143
            return $this->user = $session->getUser();
144
        }
145
146
        throw new Exception(
147
            $this->getDictionary()
0 ignored issues
show
Bug introduced by
The method getDictionary() does not seem to exist on object<Sinergi\Users\Aut...thenticationController>.

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...
148
                ->get('user.authentication.error.not_authenticated')
149
        );
150
    }
151
152
    /**
153
     * @return bool|UserEntity
154
     * @deprecated
155
     */
156
    public function getPendingUser()
157
    {
158
159
        $session = $this->getSession();
0 ignored issues
show
Bug introduced by
The method getSession() does not seem to exist on object<Sinergi\Users\Aut...thenticationController>.

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...
160
        if ($session instanceof SessionEntity) {
0 ignored issues
show
Bug introduced by
The class Sinergi\Users\Authentication\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...
161
162
            if ($session->getUser()->isEmailConfirmed()) {
163
                return false;
164
            }
165
166
            return $session->getUser();
167
        }
168
169
        return false;
170
    }
171
172
    /**
173
     * @return UserEntity|null
174
     */
175
    public function getUser()
176
    {
177
        try {
178
            return $this->getAuthenticatedUser();
179
        } catch (Exception $e) {
180
            return null;
181
        }
182
    }
183
184
    /**
185
     * @param string $event
186
     *
187
     * @return $this
188
     */
189
    private function triggerEvent($event)
190
    {
191
        $this->getContainer()->getEvenementEmitter()->emit($event);
0 ignored issues
show
Bug introduced by
The method getContainer() does not seem to exist on object<Sinergi\Users\Aut...thenticationController>.

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...
192
193
        return $this;
194
    }
195
}
196