Passed
Push — master ( 6b549f...1ce121 )
by Pol
09:48 queued 07:32
created

CasUserProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A supportsClass() 0 3 1
A refreshUser() 0 7 2
A loadUserByUsername() 0 3 1
A loadUserByResponse() 0 13 3
1
<?php
2
3
/**
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 *
7
 * @see https://github.com/ecphp
8
 */
9
10
declare(strict_types=1);
11
12
namespace EcPhp\CasBundle\Security\Core\User;
13
14
use EcPhp\CasLib\Introspection\Contract\IntrospectorInterface;
15
use EcPhp\CasLib\Introspection\Contract\ServiceValidate;
16
use InvalidArgumentException;
17
use Psr\Http\Message\ResponseInterface;
18
use Symfony\Component\Security\Core\Exception\AuthenticationException;
19
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
20
use Symfony\Component\Security\Core\User\UserInterface;
21
22
use function get_class;
23
24
class CasUserProvider implements CasUserProviderInterface
25
{
26
    private IntrospectorInterface $introspector;
27
28 6
    public function __construct(IntrospectorInterface $introspector)
29
    {
30 6
        $this->introspector = $introspector;
31 6
    }
32
33 2
    public function loadUserByResponse(ResponseInterface $response): CasUserInterface
34
    {
35
        try {
36 2
            $introspect = $this->introspector->detect($response);
37
        } catch (InvalidArgumentException $exception) {
38
            throw new AuthenticationException($exception->getMessage());
39
        }
40
41 2
        if ($introspect instanceof ServiceValidate) {
42 2
            return new CasUser($introspect->getCredentials());
43
        }
44
45 2
        throw new AuthenticationException('Unable to load user from response.');
46
    }
47
48 1
    public function loadUserByUsername(string $username): UserInterface
49
    {
50 1
        throw new UnsupportedUserException(sprintf('Username "%s" does not exist.', $username));
51
    }
52
53 1
    public function refreshUser(UserInterface $user): UserInterface
54
    {
55 1
        if (!$user instanceof CasUserInterface) {
56 1
            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
57
        }
58
59 1
        return $user;
60
    }
61
62 1
    public function supportsClass(string $class): bool
63
    {
64 1
        return CasUser::class === $class;
65
    }
66
}
67