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.
Test Failed
Push — master ( 405cf3...79c9ba )
by Joni
04:48
created

IetfAttrValue   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
eloc 31
dl 0
loc 163
ccs 41
cts 41
cp 1
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fromOctets() 0 3 1
A fromOID() 0 3 1
A fromASN1() 0 11 4
A isOctets() 0 3 1
A isOID() 0 3 1
A value() 0 3 1
A type() 0 3 1
A isString() 0 3 1
A toASN1() 0 12 4
A __toString() 0 3 1
A fromString() 0 3 1
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\Primitive\ObjectIdentifier;
9
use Sop\ASN1\Type\Primitive\OctetString;
10
use Sop\ASN1\Type\Primitive\UTF8String;
11
use Sop\ASN1\Type\UnspecifiedType;
12
13
/**
14
 * Implements *IetfAttrSyntax.values* ASN.1 CHOICE type.
15
 *
16
 * @see https://tools.ietf.org/html/rfc5755#section-4.4
17
 */
18
class IetfAttrValue
19
{
20
    /**
21
     * Element type tag.
22
     *
23
     * @var int
24
     */
25
    protected $_type;
26
27
    /**
28
     * Value.
29
     *
30
     * @var string
31
     */
32
    protected $_value;
33
34
    /**
35
     * Constructor.
36
     *
37
     * @param string $value
38
     * @param int    $type
39
     */
40 12
    public function __construct(string $value, int $type)
41
    {
42 12
        $this->_type = $type;
43 12
        $this->_value = $value;
44 12
    }
45
46
    /**
47
     * @return string
48
     */
49
    public function __toString(): string
50
    {
51
        return $this->_value;
52
    }
53 5
54
    /**
55 5
     * Initialize from ASN.1.
56 5
     *
57 5
     * @param UnspecifiedType $el
58 4
     *
59 2
     * @throws \UnexpectedValueException
60 1
     *
61
     * @return self
62 1
     */
63 1
    public static function fromASN1(UnspecifiedType $el): self
64
    {
65
        switch ($el->tag()) {
66
            case Element::TYPE_OCTET_STRING:
67
            case Element::TYPE_UTF8_STRING:
68
                return new self($el->asString()->string(), $el->tag());
69
            case Element::TYPE_OBJECT_IDENTIFIER:
70
                return new self($el->asObjectIdentifier()->oid(), $el->tag());
71
        }
72 2
        throw new \UnexpectedValueException(
73
            'Type ' . Element::tagToName($el->tag()) . ' not supported.');
74 2
    }
75
76
    /**
77
     * Initialize from octet string.
78
     *
79
     * @param string $octets
80
     *
81
     * @return self
82
     */
83 5
    public static function fromOctets(string $octets): self
84
    {
85 5
        return new self($octets, Element::TYPE_OCTET_STRING);
86
    }
87
88
    /**
89
     * Initialize from UTF-8 string.
90
     *
91
     * @param string $str
92
     *
93
     * @return self
94 2
     */
95
    public static function fromString(string $str): self
96 2
    {
97
        return new self($str, Element::TYPE_UTF8_STRING);
98
    }
99
100
    /**
101
     * Initialize from OID.
102
     *
103
     * @param string $oid
104 3
     *
105
     * @return self
106 3
     */
107
    public static function fromOID(string $oid): self
108
    {
109
        return new self($oid, Element::TYPE_OBJECT_IDENTIFIER);
110
    }
111
112
    /**
113
     * Get type tag.
114 1
     *
115
     * @return int
116 1
     */
117
    public function type(): int
118
    {
119
        return $this->_type;
120
    }
121
122
    /**
123
     * Whether value type is octets.
124 1
     *
125
     * @return bool
126 1
     */
127
    public function isOctets(): bool
128
    {
129
        return Element::TYPE_OCTET_STRING === $this->_type;
130
    }
131
132
    /**
133
     * Whether value type is OID.
134 1
     *
135
     * @return bool
136 1
     */
137
    public function isOID(): bool
138
    {
139
        return Element::TYPE_OBJECT_IDENTIFIER === $this->_type;
140
    }
141
142
    /**
143
     * Whether value type is string.
144 1
     *
145
     * @return bool
146 1
     */
147
    public function isString(): bool
148
    {
149
        return Element::TYPE_UTF8_STRING === $this->_type;
150
    }
151
152
    /**
153
     * Get value.
154
     *
155 6
     * @return string
156
     */
157 6
    public function value(): string
158 6
    {
159 1
        return $this->_value;
160 6
    }
161 5
162 2
    /**
163 1
     * Generate ASN.1 structure.
164
     *
165 1
     * @throws \LogicException
166 1
     *
167
     * @return Element
168
     */
169
    public function toASN1(): Element
170
    {
171
        switch ($this->_type) {
172
            case Element::TYPE_OCTET_STRING:
173 4
                return new OctetString($this->_value);
174
            case Element::TYPE_UTF8_STRING:
175 4
                return new UTF8String($this->_value);
176
            case Element::TYPE_OBJECT_IDENTIFIER:
177
                return new ObjectIdentifier($this->_value);
178
        }
179
        throw new \LogicException(
180
            'Type ' . Element::tagToName($this->_type) . ' not supported.');
181
    }
182
}
183