Passed
Pull Request — master (#15)
by Pol
10:38
created

CasUserProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.125

Importance

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