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

CasUserProvider::loadUserByResponse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 13
ccs 5
cts 7
cp 0.7143
crap 3.2098
rs 10
c 0
b 0
f 0
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