Passed
Push — 4.4 ( 40bda2...4fe51e )
by Pol
02:52
created

CasUserProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 86.67%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
eloc 12
c 2
b 0
f 0
dl 0
loc 50
ccs 13
cts 15
cp 0.8667
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A loadUserByResponse() 0 13 3
A refreshUser() 0 7 2
A loadUserByUsername() 0 3 1
A supportsClass() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace drupol\CasBundle\Security\Core\User;
6
7
use drupol\psrcas\Introspection\Contract\ServiceValidate;
8
use drupol\psrcas\Introspection\Introspector;
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
     * {@inheritdoc}
21
     */
22 2
    public function loadUserByResponse(ResponseInterface $response): CasUserInterface
23
    {
24
        try {
25 2
            $introspect = Introspector::detect($response);
26
        } catch (InvalidArgumentException $exception) {
27
            throw new AuthenticationException($exception->getMessage());
28
        }
29
30 2
        if ($introspect instanceof ServiceValidate) {
31 2
            return new CasUser($introspect->getCredentials());
32
        }
33
34 2
        throw new AuthenticationException('Unable to load user from response.');
35
    }
36
37
    /**
38
     * @param string $username
39
     *
40
     * @return UserInterface
41
     */
42 1
    public function loadUserByUsername($username)
43
    {
44 1
        throw new UnsupportedUserException(sprintf('Username "%s" does not exist.', $username));
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 1
    public function refreshUser(UserInterface $user)
51
    {
52 1
        if (!$user instanceof CasUserInterface) {
53 1
            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
54
        }
55
56 1
        return $user;
57
    }
58
59
    /**
60
     * @param string $class
61
     *
62
     * @return bool
63
     */
64 1
    public function supportsClass($class)
65
    {
66 1
        return CasUser::class === $class;
67
    }
68
}
69