Passed
Push — master ( 2c4349...5c635f )
by Pol
21:59 queued 19:27
created

CasUserProvider::__construct()   A

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