Completed
Push — master ( cf598b...fa35f0 )
by Gabriel
02:37
created

AuthenticationController::getUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace Sinergi\Users\Authentication;
4
5
use Interop\Container\ContainerInterface;
6
use Sinergi\Users\Authentication\Exception\AccountBannedException;
7
use Sinergi\Users\Authentication\Exception\AccountDeletedException;
8
use Sinergi\Users\Authentication\Exception\AccountInvalidException;
9
use Sinergi\Users\Authentication\Exception\InvalidCredentialsException;
10
use Sinergi\Users\Container;
11
use Sinergi\Users\Session\SessionController;
12
use Sinergi\Users\User\UserEntityInterface;
13
use Sinergi\Users\User\UserRepositoryInterface;
14
15
class AuthenticationController
16
{
17
    private $container;
18
19 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...
20
    {
21
        if ($container instanceof Container) {
22
            $this->container = $container;
23
        } else {
24
            $this->container = new Container($container);
25
        }
26
    }
27
28
    public function login(string $email, string $password, bool $isLongSession = false)
29
    {
30
        /** @var UserRepositoryInterface $userRepository */
31
        $userRepository = $this->container->get(UserRepositoryInterface::class);
32
        $user = $userRepository->findByEmail($email);
33
34
        if (!($user instanceof UserEntityInterface) || !$user->testPassword($password)) {
35
            throw new InvalidCredentialsException;
36
        }
37
38
        if (!$user->isActive()) {
39
            switch ($user->getStatus()) {
40
                case UserEntityInterface::STATUS_BANNED:
41
                    throw new AccountBannedException;
42
                case UserEntityInterface::STATUS_DELETED:
43
                    throw new AccountDeletedException;
44
                default:
45
                    throw new AccountInvalidException;
46
            }
47
        }
48
49
        $sessionController = new SessionController($this->container);
50
        return $sessionController->createSession($user, $isLongSession);
51
    }
52
}
53