Passed
Push — 1.0 ( 07d681...376964 )
by Pol
03:57
created

CasUserProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 56
ccs 16
cts 18
cp 0.8889
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsClass() 0 3 1
A refreshUser() 0 7 2
A loadUserByResponse() 0 13 3
A __construct() 0 3 1
A loadUserByUsername() 0 3 1
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