Passed
Push — master ( a3b0d7...035240 )
by Gabor
03:51
created

Auth::getIdentity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
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
 * @codeCoverageIgnore - unfinished code
26
 */
27
final class Auth extends AbstractAuthAdapter
28
{
29
    /**
30
     * Authenticates the user.
31
     *
32
     * @param AuthCredentialInterface $credential
33
     * @return AuthResultInterface
34
     */
35
    public function authenticate(AuthCredentialInterface $credential) : AuthResultInterface
36
    {
37
        /** @var AuthResultInterface $result */
38
        $result = $this->getNewAuthResultInstance();
39
        $credentials = $credential->getCredentials();
40
41
        /** @var UserStorage $dataStorage */
42
        $dataStorage = $this->getDataStorage();
43
        $user = $dataStorage->getUserByUserName($credentials['username']);
44
45
        if (!$user instanceof UserEntity) {
46
            $result->setCode(AuthResultInterface::FAILURE_IDENTITY_NOT_FOUND);
47
        } elseif (!password_verify($credentials['password'], $user->getPassword())) {
48
            $result->setCode(AuthResultInterface::FAILURE_CREDENTIAL_INVALID);
49
        } else {
50
            $this->setIdentity($user);
51
            $result->setCode(AuthResultInterface::SUCCESS);
52
        }
53
54
        return $result;
55
    }
56
}
57