PolicyConstraintsExtension   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 5
dl 0
loc 124
ccs 44
cts 44
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A _fromDER() 0 20 3
A hasRequireExplicitPolicy() 0 4 1
A requireExplicitPolicy() 0 7 2
A hasInhibitPolicyMapping() 0 4 1
A inhibitPolicyMapping() 0 7 2
A _valueASN1() 0 13 3
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace X509\Certificate\Extension;
6
7
use ASN1\Element;
8
use ASN1\Type\UnspecifiedType;
9
use ASN1\Type\Constructed\Sequence;
10
use ASN1\Type\Primitive\Integer;
11
use ASN1\Type\Tagged\ImplicitlyTaggedType;
12
13
/**
14
 * Implements 'Policy Constraints' certificate extensions.
15
 *
16
 * @link https://tools.ietf.org/html/rfc5280#section-4.2.1.11
17
 */
18
class PolicyConstraintsExtension extends Extension
19
{
20
    /**
21
     *
22
     * @var int $_requireExplicitPolicy
23
     */
24
    protected $_requireExplicitPolicy;
25
    
26
    /**
27
     *
28
     * @var int $_inhibitPolicyMapping
29
     */
30
    protected $_inhibitPolicyMapping;
31
    
32
    /**
33
     * Constructor.
34
     *
35
     * @param bool $critical
36
     * @param int|null $require_explicit_policy
37
     * @param int|null $inhibit_policy_mapping
38
     */
39 12
    public function __construct(bool $critical, $require_explicit_policy = null,
40
        $inhibit_policy_mapping = null)
41
    {
42 12
        parent::__construct(self::OID_POLICY_CONSTRAINTS, $critical);
43 12
        $this->_requireExplicitPolicy = isset($require_explicit_policy) ? intval(
44 12
            $require_explicit_policy) : null;
45 12
        $this->_inhibitPolicyMapping = isset($inhibit_policy_mapping) ? intval(
46 12
            $inhibit_policy_mapping) : null;
47 12
    }
48
    
49
    /**
50
     *
51
     * {@inheritdoc}
52
     * @return self
53
     */
54 10
    protected static function _fromDER(string $data, bool $critical): self
55
    {
56 10
        $seq = UnspecifiedType::fromDER($data)->asSequence();
57 10
        $require_explicit_policy = null;
58 10
        $inhibit_policy_mapping = null;
59 10
        if ($seq->hasTagged(0)) {
60 9
            $require_explicit_policy = $seq->getTagged(0)
61 9
                ->asImplicit(Element::TYPE_INTEGER)
62 9
                ->asInteger()
63 9
                ->intNumber();
64
        }
65 10
        if ($seq->hasTagged(1)) {
66 9
            $inhibit_policy_mapping = $seq->getTagged(1)
67 9
                ->asImplicit(Element::TYPE_INTEGER)
68 9
                ->asInteger()
69 9
                ->intNumber();
70
        }
71 10
        return new self($critical, $require_explicit_policy,
72 10
            $inhibit_policy_mapping);
73
    }
74
    
75
    /**
76
     * Whether requireExplicitPolicy is present.
77
     *
78
     * @return bool
79
     */
80 18
    public function hasRequireExplicitPolicy(): bool
81
    {
82 18
        return isset($this->_requireExplicitPolicy);
83
    }
84
    
85
    /**
86
     * Get requireExplicitPolicy.
87
     *
88
     * @throws \LogicException
89
     * @return int
90
     */
91 18
    public function requireExplicitPolicy(): int
92
    {
93 18
        if (!$this->hasRequireExplicitPolicy()) {
94 1
            throw new \LogicException("requireExplicitPolicy not set.");
95
        }
96 17
        return $this->_requireExplicitPolicy;
97
    }
98
    
99
    /**
100
     * Whether inhibitPolicyMapping is present.
101
     *
102
     * @return bool
103
     */
104 5
    public function hasInhibitPolicyMapping(): bool
105
    {
106 5
        return isset($this->_inhibitPolicyMapping);
107
    }
108
    
109
    /**
110
     * Get inhibitPolicyMapping.
111
     *
112
     * @throws \LogicException
113
     * @return int
114
     */
115 4
    public function inhibitPolicyMapping(): int
116
    {
117 4
        if (!$this->hasInhibitPolicyMapping()) {
118 1
            throw new \LogicException("inhibitPolicyMapping not set.");
119
        }
120 3
        return $this->_inhibitPolicyMapping;
121
    }
122
    
123
    /**
124
     *
125
     * {@inheritdoc}
126
     * @return Sequence
127
     */
128 19
    protected function _valueASN1(): Sequence
129
    {
130 19
        $elements = array();
131 19
        if (isset($this->_requireExplicitPolicy)) {
132 18
            $elements[] = new ImplicitlyTaggedType(0,
133 18
                new Integer($this->_requireExplicitPolicy));
134
        }
135 19
        if (isset($this->_inhibitPolicyMapping)) {
136 16
            $elements[] = new ImplicitlyTaggedType(1,
137 16
                new Integer($this->_inhibitPolicyMapping));
138
        }
139 19
        return new Sequence(...$elements);
140
    }
141
}
142