Passed
Push — 1.0 ( 07d681...376964 )
by Pol
03:57
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 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 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
    /**
20
     * @var IntrospectorInterface
21
     */
22
    private $introspector;
23
24 6
    public function __construct(IntrospectorInterface $introspector)
25
    {
26 6
        $this->introspector = $introspector;
27 6
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 2
    public function loadUserByResponse(ResponseInterface $response): CasUserInterface
33
    {
34
        try {
35 2
            $introspect = $this->introspector->detect($response);
36
        } catch (InvalidArgumentException $exception) {
37
            throw new AuthenticationException($exception->getMessage());
38
        }
39
40 2
        if ($introspect instanceof ServiceValidate) {
41 2
            return new CasUser($introspect->getCredentials());
42
        }
43
44 2
        throw new AuthenticationException('Unable to load user from response.');
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 1
    public function loadUserByUsername($username)
51
    {
52 1
        throw new UnsupportedUserException(sprintf('Username "%s" does not exist.', $username));
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 1
    public function refreshUser(UserInterface $user)
59
    {
60 1
        if (!$user instanceof CasUserInterface) {
61 1
            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
62
        }
63
64 1
        return $user;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 1
    public function supportsClass($class)
71
    {
72 1
        return CasUser::class === $class;
73
    }
74
}
75