Target   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 0
loc 88
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
toASN1() 0 1 ?
string() 0 1 ?
A fromChosenASN1() 0 5 1
A fromASN1() 0 14 4
A type() 0 4 1
A equals() 0 10 3
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace X509\Certificate\Extension\Target;
6
7
use ASN1\Type\TaggedType;
8
9
/**
10
 * Base class for <i>Target</i> ASN.1 CHOICE type.
11
 *
12
 * @link https://tools.ietf.org/html/rfc5755#section-4.3.2
13
 */
14
abstract class Target
15
{
16
    const TYPE_NAME = 0;
17
    const TYPE_GROUP = 1;
18
    const TYPE_CERT = 2;
19
    
20
    /**
21
     * Type tag.
22
     *
23
     * @var int $_type
24
     */
25
    protected $_type;
26
    
27
    /**
28
     * Generate ASN.1 element.
29
     *
30
     * @return \ASN1\Element
31
     */
32
    abstract public function toASN1();
33
    
34
    /**
35
     * Get string value of the target.
36
     *
37
     * @return string
38
     */
39
    abstract public function string(): string;
40
    
41
    /**
42
     * Initialize concrete object from the chosen ASN.1 element.
43
     *
44
     * @param TaggedType $el
45
     * @return self
46
     */
47 1
    public static function fromChosenASN1(TaggedType $el)
48
    {
49 1
        throw new \BadMethodCallException(
50 1
            __FUNCTION__ . " must be implemented in the derived class.");
51
    }
52
    
53
    /**
54
     * Parse from ASN.1.
55
     *
56
     * @param TaggedType $el
57
     * @throws \UnexpectedValueException
58
     * @return self
59
     */
60 8
    public static function fromASN1(TaggedType $el): self
61
    {
62 8
        switch ($el->tag()) {
63 8
            case self::TYPE_NAME:
64 5
                return TargetName::fromChosenASN1($el->asExplicit()->asTagged());
65 5
            case self::TYPE_GROUP:
66 3
                return TargetGroup::fromChosenASN1(
67 3
                    $el->asExplicit()->asTagged());
68 2
            case self::TYPE_CERT:
69 1
                throw new \RuntimeException("targetCert not supported.");
70
        }
71 1
        throw new \UnexpectedValueException(
72 1
            "Target type " . $el->tag() . " not supported.");
73
    }
74
    
75
    /**
76
     * Get type tag.
77
     *
78
     * @return int
79
     */
80 8
    public function type(): int
81
    {
82 8
        return $this->_type;
83
    }
84
    
85
    /**
86
     * Check whether target is equal to another.
87
     *
88
     * @param Target $other
89
     * @return bool
90
     */
91 8
    public function equals(Target $other): bool
92
    {
93 8
        if ($this->_type != $other->_type) {
94 1
            return false;
95
        }
96 7
        if ($this->toASN1()->toDER() != $other->toASN1()->toDER()) {
97 4
            return false;
98
        }
99 3
        return true;
100
    }
101
}
102