Completed
Push — master ( 2f42b5...826187 )
by A.
03:41
created

RegistrationAuthorityRole::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 5
nc 2
nop 1
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\Stepup\Identity\Value;
20
21
use Broadway\Serializer\SerializableInterface;
22
use Surfnet\Stepup\Exception\InvalidArgumentException;
23
24
final class RegistrationAuthorityRole implements SerializableInterface
25
{
26
    const ROLE_RA   = 1;
27
    const ROLE_RAA  = 2;
28
    const ROLE_SRAA = 3;
29
30
    /**
31
     * @var int
32
     */
33
    private $role;
34
35
    /**
36
     * @param string $role may not be an empty string
37
     */
38
    public function __construct($role)
39
    {
40
        if (!is_int($role) || !in_array($role, [self::ROLE_RA, self::ROLE_RAA, self::ROLE_SRAA])) {
41
            throw new InvalidArgumentException(
42
                'Invalid role given, role must be one of RegistrationAuthorityRole::[ROLE_RA|ROLE_RAA|ROLE_SRAA]'
43
            );
44
        }
45
46
        $this->role = $role;
47
    }
48
49
    /**
50
     * @param RegistrationAuthorityRole $role
51
     * @return bool
52
     */
53
    public function equals(RegistrationAuthorityRole $role)
54
    {
55
        return $this->role === $role->role;
56
    }
57
58
    /**
59
     * @return bool
60
     */
61
    public function isRa()
62
    {
63
        return $this->role === self::ROLE_RA;
64
    }
65
66
    /**
67
     * @return bool
68
     */
69
    public function isRaa()
70
    {
71
        return $this->role === self::ROLE_RAA;
72
    }
73
74
    /**
75
     * return bool
76
     */
77
    public function isSraa()
78
    {
79
        return $this->role === self::ROLE_SRAA;
80
    }
81
82
    public function jsonSerialize()
83
    {
84
        return $this->role;
85
    }
86
87
    public function __toString()
88
    {
89
        return (string) $this->role;
90
    }
91
92
    public static function deserialize(array $data)
93
    {
94
        return new self($data['role']);
95
    }
96
97
    public function serialize()
98
    {
99
        return ['role' => $this->role];
100
    }
101
}
102