RoleAttributeValue   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 10
dl 0
loc 154
ccs 43
cts 43
cp 1
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A fromString() 0 5 1
A fromASN1() 0 16 2
A hasRoleAuthority() 0 4 1
A roleAuthority() 0 7 2
A roleName() 0 4 1
A toASN1() 0 10 2
A stringValue() 0 4 1
A equalityMatchingRule() 0 4 1
A rfc2253String() 0 4 1
A _transcodedString() 0 4 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace X509\AttributeCertificate\Attribute;
6
7
use ASN1\Element;
8
use ASN1\Type\UnspecifiedType;
9
use ASN1\Type\Constructed\Sequence;
10
use ASN1\Type\Tagged\ExplicitlyTaggedType;
11
use ASN1\Type\Tagged\ImplicitlyTaggedType;
12
use X501\ASN1\AttributeType;
13
use X501\ASN1\AttributeValue\AttributeValue;
14
use X501\MatchingRule\BinaryMatch;
15
use X509\GeneralName\GeneralName;
16
use X509\GeneralName\GeneralNames;
17
use X509\GeneralName\UniformResourceIdentifier;
18
19
/**
20
 * Implements value for 'Role' attribute.
21
 *
22
 * @link https://tools.ietf.org/html/rfc5755#section-4.4.5
23
 */
24
class RoleAttributeValue extends AttributeValue
25
{
26
    /**
27
     * Issuing authority.
28
     *
29
     * @var GeneralNames $_roleAuthority
30
     */
31
    protected $_roleAuthority;
32
    
33
    /**
34
     * Role name.
35
     *
36
     * @var GeneralName $_roleName
37
     */
38
    protected $_roleName;
39
    
40
    /**
41
     * Constructor.
42
     *
43
     * @param GeneralName $name Role name
44
     * @param GeneralNames|null $authority Issuing authority
45
     */
46 15
    public function __construct(GeneralName $name, GeneralNames $authority = null)
47
    {
48 15
        $this->_roleAuthority = $authority;
49 15
        $this->_roleName = $name;
50 15
        $this->_oid = AttributeType::OID_ROLE;
51 15
    }
52
    
53
    /**
54
     * Initialize from a role string.
55
     *
56
     * @param string $role_name Role name in URI format
57
     * @param GeneralNames|null $authority Issuing authority
58
     * @return self
59
     */
60 2
    public static function fromString(string $role_name,
61
        GeneralNames $authority = null): self
62
    {
63 2
        return new self(new UniformResourceIdentifier($role_name), $authority);
64
    }
65
    
66
    /**
67
     *
68
     * @param UnspecifiedType $el
69
     * @return self
70
     */
71 8
    public static function fromASN1(UnspecifiedType $el): self
72
    {
73 8
        $seq = $el->asSequence();
74 8
        $authority = null;
75 8
        if ($seq->hasTagged(0)) {
76 1
            $authority = GeneralNames::fromASN1(
77 1
                $seq->getTagged(0)
78 1
                    ->asImplicit(Element::TYPE_SEQUENCE)
79 1
                    ->asSequence());
80
        }
81 8
        $name = GeneralName::fromASN1(
82 8
            $seq->getTagged(1)
83 8
                ->asExplicit()
84 8
                ->asTagged());
85 8
        return new self($name, $authority);
86
    }
87
    
88
    /**
89
     * Check whether issuing authority is present.
90
     *
91
     * @return bool
92
     */
93 2
    public function hasRoleAuthority(): bool
94
    {
95 2
        return isset($this->_roleAuthority);
96
    }
97
    
98
    /**
99
     * Get issuing authority.
100
     *
101
     * @throws \LogicException
102
     * @return GeneralNames
103
     */
104 2
    public function roleAuthority(): GeneralNames
105
    {
106 2
        if (!$this->hasRoleAuthority()) {
107 1
            throw new \LogicException("roleAuthority not set.");
108
        }
109 1
        return $this->_roleAuthority;
110
    }
111
    
112
    /**
113
     * Get role name.
114
     *
115
     * @return GeneralName
116
     */
117 2
    public function roleName(): GeneralName
118
    {
119 2
        return $this->_roleName;
120
    }
121
    
122
    /**
123
     *
124
     * @see \X501\ASN1\AttributeValue\AttributeValue::toASN1()
125
     * @return Sequence
126
     */
127 17
    public function toASN1(): Sequence
128
    {
129 17
        $elements = array();
130 17
        if (isset($this->_roleAuthority)) {
131 4
            $elements[] = new ImplicitlyTaggedType(0,
132 4
                $this->_roleAuthority->toASN1());
133
        }
134 17
        $elements[] = new ExplicitlyTaggedType(1, $this->_roleName->toASN1());
135 17
        return new Sequence(...$elements);
136
    }
137
    
138
    /**
139
     *
140
     * @see \X501\ASN1\AttributeValue\AttributeValue::stringValue()
141
     * @return string
142
     */
143 3
    public function stringValue(): string
144
    {
145 3
        return "#" . bin2hex($this->toASN1()->toDER());
146
    }
147
    
148
    /**
149
     *
150
     * @see \X501\ASN1\AttributeValue\AttributeValue::equalityMatchingRule()
151
     * @return BinaryMatch
152
     */
153 1
    public function equalityMatchingRule(): BinaryMatch
154
    {
155 1
        return new BinaryMatch();
156
    }
157
    
158
    /**
159
     *
160
     * @see \X501\ASN1\AttributeValue\AttributeValue::rfc2253String()
161
     * @return string
162
     */
163 1
    public function rfc2253String(): string
164
    {
165 1
        return $this->stringValue();
166
    }
167
    
168
    /**
169
     *
170
     * @see \X501\ASN1\AttributeValue\AttributeValue::_transcodedString()
171
     * @return string
172
     */
173 1
    protected function _transcodedString(): string
174
    {
175 1
        return $this->stringValue();
176
    }
177
}
178