Passed
Push — master ( d3cc39...f2a84f )
by Melech
03:59
created

AuthenticatedUsers   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
eloc 31
dl 0
loc 146
rs 10
c 1
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setImpersonated() 0 7 1
A isUserAuthenticated() 0 15 4
A remove() 0 17 4
A getCurrent() 0 3 1
A hasCurrent() 0 3 1
A getImpersonated() 0 3 1
A isImpersonating() 0 3 1
A setCurrent() 0 7 1
A __construct() 0 7 2
A all() 0 3 1
A add() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Auth\Data;
15
16
use Valkyrja\Auth\Data\Contract\AuthenticatedUsers as Contract;
17
18
use function in_array;
19
20
/**
21
 * Class AuthenticatedUsers.
22
 *
23
 * @author Melech Mizrachi
24
 */
25
class AuthenticatedUsers implements Contract
26
{
27
    /**
28
     * The users.
29
     *
30
     * @var array<int|non-empty-string, non-empty-string|int>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int|non-empty-string, non-empty-string|int> at position 4 could not be parsed: Unknown type name 'non-empty-string' at position 4 in array<int|non-empty-string, non-empty-string|int>.
Loading history...
31
     */
32
    protected array $users = [];
33
34
    /**
35
     * @param non-empty-string|int|null $currentId      The current user's id
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string|int|null at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string|int|null.
Loading history...
36
     * @param non-empty-string|int|null $impersonatedId The impersonated user's id
37
     * @param non-empty-string|int      ...$users       The users
38
     */
39
    public function __construct(
40
        protected string|int|null $currentId = null,
41
        protected string|int|null $impersonatedId = null,
42
        string|int ...$users
43
    ) {
44
        foreach ($users as $user) {
45
            $this->add($user);
46
        }
47
    }
48
49
    /**
50
     * @inheritDoc
51
     */
52
    public function hasCurrent(): bool
53
    {
54
        return $this->currentId !== null;
55
    }
56
57
    /**
58
     * @inheritDoc
59
     */
60
    public function getCurrent(): string|int|null
61
    {
62
        return $this->currentId ?? null;
63
    }
64
65
    /**
66
     * @inheritDoc
67
     *
68
     * @param non-empty-string|int $id The user
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string|int at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string|int.
Loading history...
69
     */
70
    public function setCurrent(string|int $id): static
71
    {
72
        $this->currentId = $id;
73
74
        $this->add($id);
75
76
        return $this;
77
    }
78
79
    /**
80
     * @inheritDoc
81
     */
82
    public function isImpersonating(): bool
83
    {
84
        return $this->impersonatedId !== null;
85
    }
86
87
    /**
88
     * @inheritDoc
89
     */
90
    public function getImpersonated(): string|int|null
91
    {
92
        return $this->impersonatedId ?? null;
93
    }
94
95
    /**
96
     * @inheritDoc
97
     *
98
     * @param non-empty-string|int $id The user
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string|int at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string|int.
Loading history...
99
     */
100
    public function setImpersonated(string|int $id): static
101
    {
102
        $this->impersonatedId = $id;
103
104
        $this->add($id);
105
106
        return $this;
107
    }
108
109
    /**
110
     * @inheritDoc
111
     */
112
    public function isUserAuthenticated(string|int $id): bool
113
    {
114
        if ($this->currentId === $id) {
115
            return true;
116
        }
117
118
        if ($this->impersonatedId === $id) {
119
            return true;
120
        }
121
122
        if (in_array($id, $this->users, true)) {
123
            return true;
124
        }
125
126
        return false;
127
    }
128
129
    /**
130
     * @inheritDoc
131
     *
132
     * @param non-empty-string|int $id The user
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string|int at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string|int.
Loading history...
133
     */
134
    public function add(string|int $id): static
135
    {
136
        $this->users[$id] = $id;
137
138
        return $this;
139
    }
140
141
    /**
142
     * @inheritDoc
143
     *
144
     * @param non-empty-string|int $id The id of the user to remove
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string|int at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string|int.
Loading history...
145
     */
146
    public function remove(string|int $id): static
147
    {
148
        unset($this->users[$id]);
149
150
        if ($this->currentId === $id) {
151
            $this->currentId = null;
152
153
            if (! empty($this->users)) {
154
                $this->setCurrent($this->users[array_key_first($this->users)]);
155
            }
156
        }
157
158
        if ($this->impersonatedId === $id) {
159
            $this->impersonatedId = null;
160
        }
161
162
        return $this;
163
    }
164
165
    /**
166
     * @inheritDoc
167
     */
168
    public function all(): array
169
    {
170
        return $this->users;
171
    }
172
}
173