Failed Conditions
Push — master ( 47a38a...a87e0d )
by Sébastien
02:33
created

AuthManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Service\Auth;
6
7
use App\Security\UserProviderInterface;
8
use App\Service\Auth\Exception\AuthException;
9
use App\Service\Auth\Exception\BadCredentialException;
10
use App\Service\Auth\Exception\MissingCredentialException;
11
use Zend\Expressive\Authentication\UserInterface;
12
13
class AuthManager
14
{
15
    /**
16
     * @var UserProviderInterface
17
     */
18
    private $userProvider;
19
20
    public function __construct(UserProviderInterface $userProvider)
21
    {
22
        $this->userProvider = $userProvider;
23
    }
24
25
    /**
26
     * @throws Exception\AuthExceptionInterface
27
     */
28
    public function getAuthenticatedUser(string $email, string $password): UserInterface
29
    {
30
        $email    = trim($email);
31
        $password = trim($password);
32
33
        if ($email === '' || $password === '') {
34
            throw new MissingCredentialException('Missing or empty credentials');
35
        }
36
37
        try {
38
            $user = $this->userProvider->getUserByEmail($email);
39
            if ($user !== null) {
40
                $dbPassword = $user->getDetail('password');
41
                if ($dbPassword === $password) {
42
                    return $user;
43
                }
44
                throw new BadCredentialException('Login / password does not match');
45
            }
46
        } catch (\Throwable $e) {
47
            throw new AuthException($e->getMessage());
48
        }
49
50
        throw new AuthException('Unknown authentication exception');
51
    }
52
}
53