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

AuthenticationController::login()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 24
rs 8.5125
cc 6
eloc 15
nc 5
nop 3
1
<?php
2
3
namespace Sinergi\Users\Authentication;
4
5
use Exception;
6
use Interop\Container\ContainerInterface;
7
use Psr\Http\Message\ResponseInterface;
8
use Sinergi\Users\Container;
9
use Sinergi\Users\Session\SessionController;
10
use Sinergi\Users\User\UserEntityInterface;
11
use Sinergi\Users\User\UserRepositoryInterface;
12
13
class AuthenticationController
14
{
15
    private $container;
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 bool
28
     */
29
    public function isAuthenticated()
30
    {
31
        try {
32
            $this->getAuthenticatedUser();
33
34
            return true;
35
        } catch (Exception $e) {
36
            return false;
37
        }
38
    }
39
40
    /**
41
     * @param array $parameters
42
     * @return UserEntity
43
     * @throws AuthenticationException
44
     */
45
    public function getUserByEmailAndPassword(array $parameters)
46
    {
47
        $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...
48
            ->findOneByEmail($parameters['email']);
49
50
        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...
51
            return $user;
52
        }
53
54
        throw new AuthenticationException();
55
    }
56
57
    public function login(string $email, string $password, bool $isLongSession = false)
58
    {
59
        /** @var UserRepositoryInterface $userRepository */
60
        $userRepository = $this->container->get(UserRepositoryInterface::class);
61
        $user = $userRepository->findByEmail($email);
62
63
        if (!($user instanceof UserEntityInterface) || !$user->testPassword($password)) {
64
            throw new AuthenticationException('Invalid credentials', 1000);
65
        }
66
67
        if (!$user->isActive()) {
68
            switch ($user->getStatus()) {
69
                case UserEntityInterface::STATUS_BANNED:
70
                    throw new AuthenticationException('Account banned', 1001);
71
                case UserEntityInterface::STATUS_DELETED:
72
                    throw new AuthenticationException('Account deleted', 1002);
73
                default:
74
                    throw new AuthenticationException('Account invalid', 1003);
75
            }
76
        }
77
78
        $sessionController = new SessionController($this->container);
79
        return $sessionController->createSession($user, $isLongSession);
80
    }
81
82
    public function disconnectUser()
83
    {
84
        $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...
85
        $this->triggerEvent('user.logout');
86
    }
87
88
    /**
89
     * @return UserEntity
90
     * @throws Exception
91
     */
92
    public function getAuthenticatedUser()
93
    {
94
        if (isset($this->user)) {
95
            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...
96
        }
97
98
        $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...
99
        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...
100
101
            if (!$session->getUser()->isEmailConfirmed()) {
102
                throw new AuthenticationException(
103
                    $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...
104
                        ->get('user.authentication.error.email_not_confirmed')
105
                    . '<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...
106
                        ->get('user.authentication.error.resend_confirmation_email')
107
                    . '</a>'
108
                );
109
            }
110
111
            return $this->user = $session->getUser();
112
        }
113
114
        throw new Exception(
115
            $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...
116
                ->get('user.authentication.error.not_authenticated')
117
        );
118
    }
119
120
    /**
121
     * @return bool|UserEntity
122
     * @deprecated
123
     */
124
    public function getPendingUser()
125
    {
126
127
        $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...
128
        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...
129
130
            if ($session->getUser()->isEmailConfirmed()) {
131
                return false;
132
            }
133
134
            return $session->getUser();
135
        }
136
137
        return false;
138
    }
139
140
    /**
141
     * @return UserEntity|null
142
     */
143
    public function getUser()
144
    {
145
        try {
146
            return $this->getAuthenticatedUser();
147
        } catch (Exception $e) {
148
            return null;
149
        }
150
    }
151
152
    /**
153
     * @param string $event
154
     *
155
     * @return $this
156
     */
157
    private function triggerEvent($event)
158
    {
159
        $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...
160
161
        return $this;
162
    }
163
}
164