SvceAuthInfo   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 7
dl 0
loc 155
ccs 38
cts 38
cp 1
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A fromASN1() 0 13 2
A service() 0 4 1
A ident() 0 4 1
A hasAuthInfo() 0 4 1
A authInfo() 0 7 2
A toASN1() 0 8 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\Primitive\OctetString;
11
use X501\ASN1\AttributeValue\AttributeValue;
12
use X501\MatchingRule\BinaryMatch;
13
use X509\GeneralName\GeneralName;
14
15
/**
16
 * Base class implementing <i>SvceAuthInfo</i> ASN.1 type used by
17
 * attribute certificate attribute values.
18
 *
19
 * @link https://tools.ietf.org/html/rfc5755#section-4.4.1
20
 */
21
abstract class SvceAuthInfo extends AttributeValue
22
{
23
    /**
24
     * Service.
25
     *
26
     * @var GeneralName $_service
27
     */
28
    protected $_service;
29
    
30
    /**
31
     * Ident.
32
     *
33
     * @var GeneralName $_ident
34
     */
35
    protected $_ident;
36
    
37
    /**
38
     * Auth info.
39
     *
40
     * @var string|null $_authInfo
41
     */
42
    protected $_authInfo;
43
    
44
    /**
45
     * Constructor.
46
     *
47
     * @param GeneralName $service
48
     * @param GeneralName $ident
49
     * @param string|null $auth_info
50
     */
51 9
    public function __construct(GeneralName $service, GeneralName $ident,
52
        $auth_info = null)
53
    {
54 9
        $this->_service = $service;
55 9
        $this->_ident = $ident;
56 9
        $this->_authInfo = $auth_info;
57 9
    }
58
    
59
    /**
60
     *
61
     * @param UnspecifiedType $el
62
     * @return self
63
     */
64 5
    public static function fromASN1(UnspecifiedType $el): self
65
    {
66 5
        $seq = $el->asSequence();
67 5
        $service = GeneralName::fromASN1($seq->at(0)->asTagged());
68 5
        $ident = GeneralName::fromASN1($seq->at(1)->asTagged());
69 5
        $auth_info = null;
70 5
        if ($seq->has(2, Element::TYPE_OCTET_STRING)) {
71 3
            $auth_info = $seq->at(2)
72 3
                ->asString()
73 3
                ->string();
74
        }
75 5
        return new static($service, $ident, $auth_info);
76
    }
77
    
78
    /**
79
     * Get service name.
80
     *
81
     * @return GeneralName
82
     */
83 2
    public function service(): GeneralName
84
    {
85 2
        return $this->_service;
86
    }
87
    
88
    /**
89
     * Get ident.
90
     *
91
     * @return GeneralName
92
     */
93 2
    public function ident(): GeneralName
94
    {
95 2
        return $this->_ident;
96
    }
97
    
98
    /**
99
     * Check whether authentication info is present.
100
     *
101
     * @return bool
102
     */
103 2
    public function hasAuthInfo(): bool
104
    {
105 2
        return isset($this->_authInfo);
106
    }
107
    
108
    /**
109
     * Get authentication info.
110
     *
111
     * @throws \LogicException
112
     * @return string
113
     */
114 2
    public function authInfo(): string
115
    {
116 2
        if (!$this->hasAuthInfo()) {
117 1
            throw new \LogicException("authInfo not set.");
118
        }
119 1
        return $this->_authInfo;
120
    }
121
    
122
    /**
123
     *
124
     * @see \X501\ASN1\AttributeValue\AttributeValue::toASN1()
125
     * @return Sequence
126
     */
127 9
    public function toASN1(): Sequence
128
    {
129 9
        $elements = array($this->_service->toASN1(), $this->_ident->toASN1());
130 9
        if (isset($this->_authInfo)) {
131 4
            $elements[] = new OctetString($this->_authInfo);
132
        }
133 9
        return new Sequence(...$elements);
134
    }
135
    
136
    /**
137
     *
138
     * @see \X501\ASN1\AttributeValue\AttributeValue::stringValue()
139
     * @return string
140
     */
141 3
    public function stringValue(): string
142
    {
143 3
        return "#" . bin2hex($this->toASN1()->toDER());
144
    }
145
    
146
    /**
147
     *
148
     * @see \X501\ASN1\AttributeValue\AttributeValue::equalityMatchingRule()
149
     * @return BinaryMatch
150
     */
151 1
    public function equalityMatchingRule(): BinaryMatch
152
    {
153 1
        return new BinaryMatch();
154
    }
155
    
156
    /**
157
     *
158
     * @see \X501\ASN1\AttributeValue\AttributeValue::rfc2253String()
159
     * @return string
160
     */
161 1
    public function rfc2253String(): string
162
    {
163 1
        return $this->stringValue();
164
    }
165
    
166
    /**
167
     *
168
     * @see \X501\ASN1\AttributeValue\AttributeValue::_transcodedString()
169
     * @return string
170
     */
171 1
    protected function _transcodedString(): string
172
    {
173 1
        return $this->stringValue();
174
    }
175
}
176