Principal::isAnonymous()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Caridea
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
 * use this file except in compliance with the License. You may obtain a copy of
8
 * the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 * License for the specific language governing permissions and limitations under
16
 * the License.
17
 *
18
 * @copyright 2015-2018 LibreWorks contributors
19
 * @license   Apache-2.0
20
 */
21
namespace Caridea\Auth;
22
23
/**
24
 * Security principal; an authenticated or anonymous user.
25
 *
26
 * @copyright 2015-2018 LibreWorks contributors
27
 * @license   Apache-2.0
28
 */
29
class Principal
30
{
31
    /**
32
     * @var string The principal username
33
     */
34
    protected $username;
35
    /**
36
     * @var bool Whether the principal is anonymous
37
     */
38
    protected $anonymous;
39
    /**
40
     * @var array Associative array of extra details
41
     */
42
    protected $details;
43
44
    /**
45
     * @var Principal singleton instance of the anonymous Principal
46
     */
47
    private static $anon;
48
49
    /**
50
     * Creates a new security principal.
51
     *
52
     * @param string|null $username
53
     * @param array $details
54
     * @param bool $anonymous
55
     */
56 2
    protected function __construct(?string $username = null, array $details = [], bool $anonymous = false)
57
    {
58 2
        $this->username = $username;
59 2
        $this->details = $details;
60 2
        $this->anonymous = $anonymous;
61 2
    }
62
63
    /**
64
     * Gets a key-value array containing any authentication details.
65
     *
66
     * @return array The auth details
67
     */
68 2
    public function getDetails(): array
69
    {
70 2
        return $this->details;
71
    }
72
73
    /**
74
     * Gets the authenticated principal username.
75
     *
76
     * An anonymous user has a `null` username.
77
     *
78
     * @return string|null The username, or `null`
79
     */
80 2
    public function getUsername(): ?string
81
    {
82 2
        return $this->username;
83
    }
84
85
    /**
86
     * Gets whether this authentication is anonymous.
87
     *
88
     * @return bool Whether this principal is anonymous
89
     */
90 2
    public function isAnonymous(): bool
91
    {
92 2
        return $this->anonymous;
93
    }
94
95
    /**
96
     * Gets a string representation.
97
     *
98
     * @return string The string representation
99
     */
100 2
    public function __toString(): string
101
    {
102 2
        return $this->anonymous ? "[anonymous]" : $this->username;
103
    }
104
105
    /**
106
     * Gets a non-anonymous Principal.
107
     *
108
     * @param string $username The principal username
109
     * @param array $details Any authentication details
110
     * @return Principal The principal
111
     */
112 1
    public static function get(string $username, array $details): Principal
113
    {
114 1
        return new self($username, $details);
115
    }
116
117
    /**
118
     * Gets a token representing an anonymous authentication.
119
     *
120
     * @return Principal The anonymous principal
121
     */
122 1
    public static function getAnonymous(): Principal
123
    {
124 1
        if (self::$anon === null) {
125 1
            self::$anon = new self(null, [], true);
126
        }
127 1
        return self::$anon;
128
    }
129
}
130