DistributionPoint   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 214
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 8
dl 0
loc 214
ccs 67
cts 67
cp 1
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A fromASN1() 0 26 4
A hasDistributionPointName() 0 4 1
A distributionPointName() 0 7 2
A hasFullName() 0 5 1
A fullName() 0 7 2
A hasRelativeName() 0 5 1
A relativeName() 0 7 2
A hasReasons() 0 4 1
A reasons() 0 7 2
A hasCRLIssuer() 0 4 1
A crlIssuer() 0 7 2
A toASN1() 0 15 4
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace X509\Certificate\Extension\DistributionPoint;
6
7
use ASN1\Element;
8
use ASN1\Type\Constructed\Sequence;
9
use ASN1\Type\Tagged\ExplicitlyTaggedType;
10
use ASN1\Type\Tagged\ImplicitlyTaggedType;
11
use X509\GeneralName\GeneralNames;
12
13
/**
14
 * Implements <i>DistributionPoint</i> ASN.1 type used by
15
 * 'CRL Distribution Points' certificate extension.
16
 *
17
 * @link https://tools.ietf.org/html/rfc5280#section-4.2.1.13
18
 */
19
class DistributionPoint
20
{
21
    /**
22
     * Distribution point name.
23
     *
24
     * @var DistributionPointName $_distributionPoint
25
     */
26
    protected $_distributionPoint;
27
    
28
    /**
29
     * Revocation reason.
30
     *
31
     * @var ReasonFlags $_reasons
32
     */
33
    protected $_reasons;
34
    
35
    /**
36
     * CRL issuer.
37
     *
38
     * @var GeneralNames $_issuer
39
     */
40
    protected $_issuer;
41
    
42
    /**
43
     * Constructor.
44
     *
45
     * @param DistributionPointName $name
46
     * @param ReasonFlags $reasons
47
     * @param GeneralNames $issuer
48
     */
49 17
    public function __construct(DistributionPointName $name = null,
50
        ReasonFlags $reasons = null, GeneralNames $issuer = null)
51
    {
52 17
        $this->_distributionPoint = $name;
53 17
        $this->_reasons = $reasons;
54 17
        $this->_issuer = $issuer;
55 17
    }
56
    
57
    /**
58
     * Initialize from ASN.1.
59
     *
60
     * @param Sequence $seq
61
     * @return self
62
     */
63 12
    public static function fromASN1(Sequence $seq): self
64
    {
65 12
        $name = null;
66 12
        $reasons = null;
67 12
        $issuer = null;
68 12
        if ($seq->hasTagged(0)) {
69
            // promoted to explicit tagging because underlying type is CHOICE
70 12
            $name = DistributionPointName::fromTaggedType(
71 12
                $seq->getTagged(0)
72 12
                    ->asExplicit()
73 12
                    ->asTagged());
74
        }
75 12
        if ($seq->hasTagged(1)) {
76 11
            $reasons = ReasonFlags::fromASN1(
77 11
                $seq->getTagged(1)
78 11
                    ->asImplicit(Element::TYPE_BIT_STRING)
79 11
                    ->asBitString());
80
        }
81 12
        if ($seq->hasTagged(2)) {
82 11
            $issuer = GeneralNames::fromASN1(
83 11
                $seq->getTagged(2)
84 11
                    ->asImplicit(Element::TYPE_SEQUENCE)
85 11
                    ->asSequence());
86
        }
87 12
        return new self($name, $reasons, $issuer);
88
    }
89
    
90
    /**
91
     * Check whether distribution point name is set.
92
     *
93
     * @return bool
94
     */
95 9
    public function hasDistributionPointName(): bool
96
    {
97 9
        return isset($this->_distributionPoint);
98
    }
99
    
100
    /**
101
     * Get distribution point name.
102
     *
103
     * @throws \LogicException
104
     * @return DistributionPointName
105
     */
106 9
    public function distributionPointName(): DistributionPointName
107
    {
108 9
        if (!$this->hasDistributionPointName()) {
109 1
            throw new \LogicException("distributionPoint not set.");
110
        }
111 8
        return $this->_distributionPoint;
112
    }
113
    
114
    /**
115
     * Check whether distribution point name is set and it's a full name.
116
     *
117
     * @return bool
118
     */
119 3
    public function hasFullName(): bool
120
    {
121 3
        return $this->distributionPointName()->tag() ==
122 3
             DistributionPointName::TAG_FULL_NAME;
123
    }
124
    
125
    /**
126
     * Get full distribution point name.
127
     *
128
     * @throws \LogicException
129
     * @return FullName
130
     */
131 3
    public function fullName(): FullName
132
    {
133 3
        if (!$this->hasFullName()) {
134 1
            throw new \LogicException("fullName not set.");
135
        }
136 2
        return $this->_distributionPoint;
137
    }
138
    
139
    /**
140
     * Check whether distribution point name is set and it's a relative name.
141
     *
142
     * @return bool
143
     */
144 2
    public function hasRelativeName(): bool
145
    {
146 2
        return $this->distributionPointName()->tag() ==
147 2
             DistributionPointName::TAG_RDN;
148
    }
149
    
150
    /**
151
     * Get relative distribution point name.
152
     *
153
     * @throws \LogicException
154
     * @return RelativeName
155
     */
156 2
    public function relativeName(): RelativeName
157
    {
158 2
        if (!$this->hasRelativeName()) {
159 1
            throw new \LogicException("nameRelativeToCRLIssuer not set.");
160
        }
161 1
        return $this->_distributionPoint;
162
    }
163
    
164
    /**
165
     * Check whether reasons flags is set.
166
     *
167
     * @return bool
168
     */
169 5
    public function hasReasons(): bool
170
    {
171 5
        return isset($this->_reasons);
172
    }
173
    
174
    /**
175
     * Get revocation reason flags.
176
     *
177
     * @throws \LogicException
178
     * @return ReasonFlags
179
     */
180 5
    public function reasons(): ReasonFlags
181
    {
182 5
        if (!$this->hasReasons()) {
183 1
            throw new \LogicException("reasons not set.");
184
        }
185 4
        return $this->_reasons;
186
    }
187
    
188
    /**
189
     * Check whether cRLIssuer is set.
190
     *
191
     * @return bool
192
     */
193 5
    public function hasCRLIssuer(): bool
194
    {
195 5
        return isset($this->_issuer);
196
    }
197
    
198
    /**
199
     * Get CRL issuer.
200
     *
201
     * @throws \LogicException
202
     * @return GeneralNames
203
     */
204 5
    public function crlIssuer(): GeneralNames
205
    {
206 5
        if (!$this->hasCRLIssuer()) {
207 1
            throw new \LogicException("crlIssuer not set.");
208
        }
209 4
        return $this->_issuer;
210
    }
211
    
212
    /**
213
     * Generate ASN.1 structure.
214
     *
215
     * @return Sequence
216
     */
217 18
    public function toASN1(): Sequence
218
    {
219 18
        $elements = array();
220 18
        if (isset($this->_distributionPoint)) {
221 18
            $elements[] = new ExplicitlyTaggedType(0,
222 18
                $this->_distributionPoint->toASN1());
223
        }
224 18
        if (isset($this->_reasons)) {
225 17
            $elements[] = new ImplicitlyTaggedType(1, $this->_reasons->toASN1());
226
        }
227 18
        if (isset($this->_issuer)) {
228 17
            $elements[] = new ImplicitlyTaggedType(2, $this->_issuer->toASN1());
229
        }
230 18
        return new Sequence(...$elements);
231
    }
232
}
233