Passed
Push — master ( 86c3d2...1ebd3c )
by Gabor
07:26
created

Auth   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 34
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B authenticate() 0 25 5
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
declare(strict_types = 1);
13
14
namespace WebHemi\Auth;
15
16
use WebHemi\Adapter\Auth\AbstractAuthAdapter;
17
use WebHemi\Adapter\Auth\AuthCredentialInterface;
18
use WebHemi\Adapter\Auth\AuthResultInterface;
19
use WebHemi\Data\Entity\User\UserEntity;
20
use WebHemi\Data\Storage\User\UserStorage;
21
22
/**
23
 * Class Auth
24
 */
25
final class Auth extends AbstractAuthAdapter
26
{
27
    /**
28
     * Authenticates the user.
29
     *
30
     * @param AuthCredentialInterface $credential
31
     * @return AuthResultInterface
32
     */
33 1
    public function authenticate(AuthCredentialInterface $credential) : AuthResultInterface
34
    {
35
        /** @var AuthResultInterface $result */
36 1
        $result = $this->getNewAuthResultInstance();
37 1
        $credentials = $credential->getCredentials();
38
39
        /** @var UserStorage $dataStorage */
40 1
        $dataStorage = $this->getDataStorage();
41 1
        $user = $dataStorage->getUserByUserName($credentials['username']);
42
43 1
        if (!$user instanceof UserEntity) {
44 1
            $result->setCode(AuthResultInterface::FAILURE_IDENTITY_NOT_FOUND);
45 1
        } elseif (!$user->getEnabled()) {
46 1
            $result->setCode(AuthResultInterface::FAILURE_IDENTITY_DISABLED);
47 1
        } elseif (!$user->getActive()) {
48 1
            $result->setCode(AuthResultInterface::FAILURE_IDENTITY_INACTIVE);
49 1
        } elseif (!password_verify($credentials['password'], $user->getPassword())) {
50 1
            $result->setCode(AuthResultInterface::FAILURE_CREDENTIAL_INVALID);
51
        } else {
52 1
            $this->setIdentity($user);
53 1
            $result->setCode(AuthResultInterface::SUCCESS);
54
        }
55
56 1
        return $result;
57
    }
58
}
59