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

AuthManager   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 38
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getAuthenticatedUser() 0 23 6
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