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) |
|
|
|
|
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
|
|
|
|
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.