UserBadge   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 22
c 0
b 0
f 0
dl 0
loc 91
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A provider() 0 3 1
A user() 0 6 2
A __construct() 0 8 2
A retrieveUser() 0 19 4
A isResolved() 0 3 1
A userIdentifier() 0 3 1
1
<?php
2
3
/**
4
 * This file is part of web-stack
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Slick\WebStack\Domain\Security\Http\Authenticator\Passport\Badge;
13
14
use Slick\WebStack\Domain\Security\Exception\AuthenticationServiceException;
15
use Slick\WebStack\Domain\Security\Exception\BadCredentialsException;
16
use Slick\WebStack\Domain\Security\Exception\UserNotFoundException;
17
use Slick\WebStack\Domain\Security\Http\Authenticator\Passport\BadgeInterface;
18
use Slick\WebStack\Domain\Security\SecurityException;
19
use Slick\WebStack\Domain\Security\User\UserProviderInterface;
20
use Slick\WebStack\Domain\Security\UserInterface;
21
22
/**
23
 * UserBadge
24
 *
25
 * @package Slick\WebStack\Domain\Security\Http\Authenticator\Passport\Badge
26
 * @template-covariant TUser of UserInterface
27
 */
28
class UserBadge implements BadgeInterface
29
{
30
    public const MAX_USERNAME_LENGTH = 4096;
31
32
    private ?UserInterface $user = null;
33
34
    /** @var callable|UserProviderInterface<TUser> */
35
    private readonly mixed $provider;
36
37
    /**
38
     * Creates a UserBadge
39
     *
40
     * @param string $userIdentifier
41
     * @param UserProviderInterface<TUser>|callable $provider
42
     * @throws BadCredentialsException
43
     */
44
    public function __construct(
45
        private readonly string $userIdentifier,
46
        UserProviderInterface|callable $provider
47
    ) {
48
        if (strlen($this->userIdentifier) > self::MAX_USERNAME_LENGTH) {
49
            throw new BadCredentialsException('Username too long.');
50
        }
51
        $this->provider = $provider;
0 ignored issues
show
Bug introduced by
The property provider is declared read-only in Slick\WebStack\Domain\Se...assport\Badge\UserBadge.
Loading history...
52
    }
53
54
    /**
55
     * UserBadge user
56
     *
57
     * @return UserInterface
58
     * @throws SecurityException
59
     */
60
    public function user(): UserInterface
61
    {
62
        if (null === $this->user) {
63
            $this->user = $this->retrieveUser();
64
        }
65
        return $this->user;
66
    }
67
68
    /**
69
     * @inheritDoc
70
     */
71
    public function isResolved(): bool
72
    {
73
        return true;
74
    }
75
76
    /**
77
     * UserBadge userIdentifier
78
     *
79
     * @return string
80
     */
81
    public function userIdentifier(): string
82
    {
83
        return $this->userIdentifier;
84
    }
85
86
    /**
87
     * UserBadge provider
88
     *
89
     * @return callable|UserProviderInterface<TUser>
90
     */
91
    public function provider(): callable|UserProviderInterface
92
    {
93
        return $this->provider;
94
    }
95
96
    /**
97
     * @throws SecurityException|UserNotFoundException
98
     * @throws SecurityException|AuthenticationServiceException
99
     */
100
    private function retrieveUser(): UserInterface
101
    {
102
        if ($this->provider instanceof UserProviderInterface) {
103
            return $this->provider->loadUserByIdentifier($this->userIdentifier);
104
        }
105
106
        /** @phpstan-var TUser $user */
107
        $user = ($this->provider)($this->userIdentifier);
108
        if (null === $user) {
109
            throw new UserNotFoundException(sprintf('User with identifier "%s" not found.', $this->userIdentifier));
110
        }
111
112
        if (!$user instanceof UserInterface) {
113
            throw new AuthenticationServiceException(
114
                sprintf('The user provider must return a UserInterface object, "%s" given.', get_debug_type($user))
115
            );
116
        }
117
118
        return $user;
119
    }
120
}
121