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

AbstractToken   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 23
c 0
b 0
f 0
dl 0
loc 82
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __unserialize() 0 5 1
A __serialize() 0 6 1
A __toString() 0 9 1
A userIdentifier() 0 3 2
A roleNames() 0 3 1
A user() 0 3 1
A __construct() 0 4 2
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\Authentication\Token;
13
14
use Slick\WebStack\Domain\Security\Authentication\TokenInterface;
15
use Slick\WebStack\Domain\Security\Common\AttributesBagInterface;
16
use Slick\WebStack\Domain\Security\Common\AttributesBagMethods;
17
use Slick\WebStack\Domain\Security\UserInterface;
18
19
/**
20
 * AbstractToken
21
 *
22
 * @package Slick\WebStack\Domain\Security\Authentication\Token
23
 * @template-covariant TUser of UserInterface
24
 * @implements TokenInterface<TUser>
25
 */
26
abstract class AbstractToken implements TokenInterface
27
{
28
    use AttributesBagMethods;
29
30
    /** @var array<string> */
31
    protected array $roleNames = [];
32
33
    /** @phpstan-var TUser|null  */
34
    protected ?UserInterface $user = null;
35
36
    /**
37
     * Creates an AbstractToken
38
     *
39
     * @param array<string|\Stringable> $roles
40
     */
41
    public function __construct(array $roles = [])
42
    {
43
        foreach ($roles as $role) {
44
            $this->roleNames[] = (string) $role;
45
        }
46
    }
47
48
    /**
49
     * @inheritDoc
50
     */
51
    public function __toString(): string
52
    {
53
        $class = static::class;
54
        $class = substr($class, strrpos($class, '\\') + 1);
55
        return sprintf(
56
            '%s(user="%s", roles="%s")',
57
            $class,
58
            $this->userIdentifier(),
59
            implode(', ', $this->roleNames)
60
        );
61
    }
62
63
    /**
64
     * @inheritDoc
65
     */
66
    public function userIdentifier(): string
67
    {
68
        return $this->user ? $this->user->userIdentifier() : '';
69
    }
70
71
    /**
72
     * @inheritDoc
73
     */
74
    public function roleNames(): array
75
    {
76
        return $this->roleNames;
77
    }
78
79
    /**
80
     * @inheritDoc
81
     */
82
    public function user(): ?UserInterface
83
    {
84
        return $this->user;
85
    }
86
87
    /**
88
     * @inheritDoc
89
     */
90
    public function __serialize(): array
91
    {
92
        return [
93
            "user" => $this->user,
94
            "roleNames" => $this->roleNames,
95
            "attributes" => $this->attributes
96
        ];
97
    }
98
99
    /**
100
     * @inheritDoc
101
     * @param array<string, mixed> $data
102
     */
103
    public function __unserialize(array $data): void
104
    {
105
        $this->user = $data["user"];
106
        $this->roleNames = $data["roleNames"];
107
        $this->attributes = $data["attributes"];
108
    }
109
}
110