GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

IetfAttrSyntax::first()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\X509\AttributeCertificate\Attribute;
6
7
use Sop\ASN1\Element;
8
use Sop\ASN1\Type\Constructed\Sequence;
9
use Sop\ASN1\Type\Tagged\ImplicitlyTaggedType;
10
use Sop\ASN1\Type\UnspecifiedType;
11
use Sop\X501\ASN1\AttributeValue\AttributeValue;
12
use Sop\X501\MatchingRule\BinaryMatch;
13
use Sop\X501\MatchingRule\MatchingRule;
14
use Sop\X509\GeneralName\GeneralNames;
15
16
/**
17
 * Base class implementing *IetfAttrSyntax* ASN.1 type used by
18
 * attribute certificate attribute values.
19
 *
20
 * @see https://tools.ietf.org/html/rfc5755#section-4.4
21
 */
22
abstract class IetfAttrSyntax extends AttributeValue implements \Countable, \IteratorAggregate
23
{
24
    /**
25
     * Policy authority.
26
     *
27
     * @var null|GeneralNames
28
     */
29
    protected $_policyAuthority;
30
31
    /**
32
     * Values.
33
     *
34
     * @var IetfAttrValue[]
35
     */
36
    protected $_values;
37
38
    /**
39
     * Constructor.
40
     *
41
     * @param IetfAttrValue ...$values
42
     */
43 9
    public function __construct(IetfAttrValue ...$values)
44
    {
45 9
        $this->_policyAuthority = null;
46 9
        $this->_values = $values;
47 9
    }
48
49
    /**
50
     * {@inheritdoc}
51
     *
52
     * @return self
53
     */
54 4
    public static function fromASN1(UnspecifiedType $el): AttributeValue
55
    {
56 4
        $seq = $el->asSequence();
57 4
        $authority = null;
58 4
        $idx = 0;
59 4
        if ($seq->hasTagged(0)) {
60 4
            $authority = GeneralNames::fromASN1(
61 4
                $seq->getTagged(0)->asImplicit(Element::TYPE_SEQUENCE)
62 4
                    ->asSequence());
63 4
            ++$idx;
64
        }
65 4
        $values = array_map(
66
            function (UnspecifiedType $el) {
67 4
                return IetfAttrValue::fromASN1($el);
68 4
            }, $seq->at($idx)->asSequence()->elements());
69 4
        $obj = new static(...$values);
70 4
        $obj->_policyAuthority = $authority;
71 4
        return $obj;
72
    }
73
74
    /**
75
     * Get self with policy authority.
76
     *
77
     * @param GeneralNames $names
78
     *
79
     * @return self
80
     */
81 2
    public function withPolicyAuthority(GeneralNames $names): self
82
    {
83 2
        $obj = clone $this;
84 2
        $obj->_policyAuthority = $names;
85 2
        return $obj;
86
    }
87
88
    /**
89
     * Check whether policy authority is present.
90
     *
91
     * @return bool
92
     */
93 3
    public function hasPolicyAuthority(): bool
94
    {
95 3
        return isset($this->_policyAuthority);
96
    }
97
98
    /**
99
     * Get policy authority.
100
     *
101
     * @throws \LogicException If not set
102
     *
103
     * @return GeneralNames
104
     */
105 3
    public function policyAuthority(): GeneralNames
106
    {
107 3
        if (!$this->hasPolicyAuthority()) {
108 1
            throw new \LogicException('policyAuthority not set.');
109
        }
110 2
        return $this->_policyAuthority;
111
    }
112
113
    /**
114
     * Get values.
115
     *
116
     * @return IetfAttrValue[]
117
     */
118 3
    public function values(): array
119
    {
120 3
        return $this->_values;
121
    }
122
123
    /**
124
     * Get first value.
125
     *
126
     * @throws \LogicException If not set
127
     *
128
     * @return IetfAttrValue
129
     */
130 2
    public function first(): IetfAttrValue
131
    {
132 2
        if (!count($this->_values)) {
133 1
            throw new \LogicException('No values.');
134
        }
135 1
        return $this->_values[0];
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141 8
    public function toASN1(): Element
142
    {
143 8
        $elements = [];
144 8
        if (isset($this->_policyAuthority)) {
145 5
            $elements[] = new ImplicitlyTaggedType(
146 5
                0, $this->_policyAuthority->toASN1());
147
        }
148 8
        $values = array_map(
149
            function (IetfAttrValue $val) {
150 5
                return $val->toASN1();
151 8
            }, $this->_values);
152 8
        $elements[] = new Sequence(...$values);
153 8
        return new Sequence(...$elements);
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159 3
    public function stringValue(): string
160
    {
161 3
        return '#' . bin2hex($this->toASN1()->toDER());
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167 1
    public function equalityMatchingRule(): MatchingRule
168
    {
169 1
        return new BinaryMatch();
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175 1
    public function rfc2253String(): string
176
    {
177 1
        return $this->stringValue();
178
    }
179
180
    /**
181
     * Get number of values.
182
     *
183
     * @see \Countable::count()
184
     *
185
     * @return int
186
     */
187 2
    public function count(): int
188
    {
189 2
        return count($this->_values);
190
    }
191
192
    /**
193
     * Get iterator for values.
194
     *
195
     * @see \IteratorAggregate::getIterator()
196
     *
197
     * @return \ArrayIterator
198
     */
199 1
    public function getIterator(): \ArrayIterator
200
    {
201 1
        return new \ArrayIterator($this->_values);
202
    }
203
204
    /**
205
     * {@inheritdoc}
206
     */
207 1
    protected function _transcodedString(): string
208
    {
209 1
        return $this->stringValue();
210
    }
211
}
212