Passed
Branch master (372b2a)
by Filipe
01:32
created

PassportMethodsTrait::user()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 0
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;
13
14
use Slick\WebStack\Domain\Security\Http\Authenticator\Passport\Badge\UserBadge;
15
use Slick\WebStack\Domain\Security\Http\Authenticator\Passport\BadgeInterface;
16
use Slick\WebStack\Domain\Security\SecurityException;
17
use Slick\WebStack\Domain\Security\UserInterface;
18
19
/**
20
 * PassportMethodsTrait
21
 *
22
 * @package Slick\WebStack\Domain\Security\Http\Authenticator
23
 */
24
trait PassportMethodsTrait
25
{
26
27
    protected ?UserInterface $user = null;
28
29
    /** @var array<BadgeInterface>  */
30
    protected array $badges = [];
31
32
    /**
33
     * The user that will be or was subject of authentication
34
     *
35
     * @return UserInterface
36
     * @throws SecurityException
37
     */
38
    public function user(): UserInterface
39
    {
40
        if (null === $this->user) {
41
            /** @phpstan-var UserBadge<UserInterface> $badge */
42
            $badge = $this->badge(UserBadge::class);
43
            $this->user = $badge->user();
0 ignored issues
show
Bug introduced by
The method user() does not exist on Slick\WebStack\Domain\Se...Passport\BadgeInterface. It seems like you code against a sub-type of Slick\WebStack\Domain\Se...Passport\BadgeInterface such as Slick\WebStack\Domain\Se...assport\Badge\UserBadge. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
            /** @scrutinizer ignore-call */ 
44
            $this->user = $badge->user();
Loading history...
44
        }
45
46
        return $this->user;
47
    }
48
49
    /**
50
     * Adds a new security badge.
51
     *
52
     *  A passport can hold only one instance of the same security badge.
53
     *  This method replaces the current badge if it is already set on this
54
     *  passport.
55
     *
56
     * @param BadgeInterface $badge
57
     * @param string|null $badgeName A FQCN to which the badge should be mapped to.
58
     *                                This allows replacing a built-in badge by a custom one using
59
     *                                e.g. addBadge(new MyCustomUserBadge(), UserBadge::class)
60
     *
61
     * @return static
62
     */
63
    public function addBadge(BadgeInterface $badge, ?string $badgeName = null): static
64
    {
65
        $badgeName ??= $badge::class;
66
        $this->badges[$badgeName] = $badge;
67
        return $this;
68
    }
69
70
    /**
71
     * Check if a badge exists.
72
     *
73
     * @param string $badgeName The name of the badge to check.
74
     *
75
     * @return bool Returns true if the badge exists, false otherwise.
76
     */
77
    public function hasBadge(string $badgeName): bool
78
    {
79
        return array_key_exists($badgeName, $this->badges);
80
    }
81
82
    /**
83
     * Returns the badge with the given name.
84
     *
85
     * @param string $badgeName The name of the badge.
86
     * @return BadgeInterface|null The BadgeInterface instance if found; otherwise null.
87
     */
88
    public function badge(string $badgeName): ?BadgeInterface
89
    {
90
        return $this->badges[$badgeName] ?? null;
91
    }
92
93
    /**
94
     * @return array<class-string<BadgeInterface>, BadgeInterface>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string<Badge...rface>, BadgeInterface> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string<BadgeInterface>, BadgeInterface>.
Loading history...
95
     */
96
    public function badges(): array
97
    {
98
        return $this->badges;
99
    }
100
}
101