AuthorityRole::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of 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,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupMiddleware\ApiBundle\Identity\Value;
20
21
use Stringable;
22
use Surfnet\Stepup\Identity\Value\RegistrationAuthorityRole;
0 ignored issues
show
Bug introduced by
The type Surfnet\Stepup\Identity\...gistrationAuthorityRole was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Surfnet\StepupMiddleware\ApiBundle\Exception\InvalidArgumentException;
24
use Surfnet\StepupMiddleware\ApiBundle\Exception\RuntimeException;
25
26
class AuthorityRole implements Stringable
27
{
28
    public const ROLE_RA = 'ra';
29
    public const ROLE_RAA = 'raa';
30
    public const ROLE_SRAA = 'sraa';
31
32
    /**
33
     * @var string
34
     */
35
    private readonly string $role;
36
37
    public function __construct(string $role)
38
    {
39
        if (!in_array($role, [self::ROLE_RA, self::ROLE_RAA, self::ROLE_SRAA])) {
40
            throw InvalidArgumentException::invalidType(
41
                'One of AuthorityRole::ROLE_RA, AuthorityRole::ROLE_RAA or AuthorityRole::ROLE_SRAA',
42
                'role',
43
                $role,
44
            );
45
        }
46
47
        $this->role = $role;
0 ignored issues
show
Bug introduced by
The property role is declared read-only in Surfnet\StepupMiddleware...ity\Value\AuthorityRole.
Loading history...
48
    }
49
50
    /**
51
     * @SuppressWarnings("PHPMD.CamelCaseMethodName") this is the actual used notation
52
     * @SuppressWarnings("PHPMD.ShortMethodName")     this is the actual term
53
     *
54
     * @return AuthorityRole
55
     */
56
    public static function ra(): self
57
    {
58
        return new self(self::ROLE_RA);
59
    }
60
61
    /**
62
     * @SuppressWarnings("PHPMD.CamelCaseMethodName") this is the actual used notation
63
     *
64
     * @return AuthorityRole
65
     */
66
    public static function raa(): self
67
    {
68
        return new self(self::ROLE_RAA);
69
    }
70
71
    /**
72
     * @return AuthorityRole
73
     */
74
    public static function fromRegistrationAuthorityRole(RegistrationAuthorityRole $registrationAuthorityRole): AuthorityRole
75
    {
76
        if ($registrationAuthorityRole->isRa()) {
77
            return static::ra();
78
        } elseif ($registrationAuthorityRole->isRaa()) {
79
            return static::raa();
80
        }
81
82
        throw new RuntimeException(
83
            sprintf(
84
                'AuthorityRole cannot be created from RegistrationAuthorityRole of value "%s"',
85
                $registrationAuthorityRole,
86
            ),
87
        );
88
    }
89
90
    /**
91
     * @return bool
92
     */
93
    public function equals(AuthorityRole $other): bool
94
    {
95
        return $this->role === $other->role;
96
    }
97
98
    /**
99
     * @return string
100
     */
101
    public function getRole(): string
102
    {
103
        return $this->role;
104
    }
105
106
    public function __toString(): string
107
    {
108
        return $this->role;
109
    }
110
}
111