CasUserProvider::loadUserByUsername()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace drupol\CasBundle\Security\Core\User;
6
7
use drupol\psrcas\Introspection\Contract\ServiceValidate;
8
use drupol\psrcas\Introspection\Introspector;
9
use InvalidArgumentException;
10
use Psr\Http\Message\ResponseInterface;
11
use Symfony\Component\Security\Core\Exception\AuthenticationException;
12
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
13
use Symfony\Component\Security\Core\User\UserInterface;
14
15
use function get_class;
16
17
class CasUserProvider implements CasUserProviderInterface
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22 2
    public function loadUserByResponse(ResponseInterface $response): CasUserInterface
23
    {
24
        try {
25 2
            $introspect = Introspector::detect($response);
26
        } catch (InvalidArgumentException $exception) {
27
            throw new AuthenticationException($exception->getMessage());
28
        }
29
30 2
        if ($introspect instanceof ServiceValidate) {
31 2
            return new CasUser($introspect->getCredentials());
32
        }
33
34 2
        throw new AuthenticationException('Unable to load user from response.');
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 1
    public function loadUserByUsername(string $username)
41
    {
42 1
        throw new UnsupportedUserException(sprintf('Username "%s" does not exist.', $username));
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 1
    public function refreshUser(UserInterface $user)
49
    {
50 1
        if (!$user instanceof CasUserInterface) {
51 1
            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
52
        }
53
54 1
        return $user;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     *
60
     * @return bool
61
     */
62 1
    public function supportsClass(string $class)
63
    {
64 1
        return CasUser::class === $class;
65
    }
66
}
67