DistributionPointName::toASN1()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
nc 1
cc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace X509\Certificate\Extension\DistributionPoint;
6
7
use ASN1\Element;
8
use ASN1\Type\TaggedType;
9
use ASN1\Type\Tagged\ImplicitlyTaggedType;
10
use X501\ASN1\RDN;
11
use X509\GeneralName\GeneralNames;
12
13
/**
14
 * Base class for <i>DistributionPointName</i> ASN.1 CHOICE 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
abstract class DistributionPointName
20
{
21
    const TAG_FULL_NAME = 0;
22
    const TAG_RDN = 1;
23
    
24
    /**
25
     * Type.
26
     *
27
     * @var int $_tag
28
     */
29
    protected $_tag;
30
    
31
    /**
32
     * Generate ASN.1 element.
33
     *
34
     * @return Element
35
     */
36
    abstract protected function _valueASN1();
37
    
38
    /**
39
     * Initialize from TaggedType.
40
     *
41
     * @param TaggedType $el
42
     * @throws \UnexpectedValueException
43
     * @return self
44
     */
45 15
    public static function fromTaggedType(TaggedType $el): self
46
    {
47 15
        switch ($el->tag()) {
48 15
            case self::TAG_FULL_NAME:
49 12
                return new FullName(
50 12
                    GeneralNames::fromASN1(
51 12
                        $el->asImplicit(Element::TYPE_SEQUENCE)->asSequence()));
52 11
            case self::TAG_RDN:
53 10
                return new RelativeName(
54 10
                    RDN::fromASN1($el->asImplicit(Element::TYPE_SET)->asSet()));
55
            default:
56 1
                throw new \UnexpectedValueException(
57 1
                    "DistributionPointName tag " . $el->tag() . " not supported.");
58
        }
59
    }
60
    
61
    /**
62
     * Get type tag.
63
     *
64
     * @return int
65
     */
66 7
    public function tag(): int
67
    {
68 7
        return $this->_tag;
69
    }
70
    
71
    /**
72
     * Generate ASN.1 structure.
73
     *
74
     * @return ImplicitlyTaggedType
75
     */
76 20
    public function toASN1(): ImplicitlyTaggedType
77
    {
78 20
        return new ImplicitlyTaggedType($this->_tag, $this->_valueASN1());
79
    }
80
}
81