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.

PolicyMappingsExtension   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 19
eloc 45
dl 0
loc 161
ccs 51
cts 51
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A issuerMappings() 0 9 3
A hasAnyPolicyMapping() 0 11 4
A count() 0 3 1
A mappings() 0 3 1
A __construct() 0 4 1
A getIterator() 0 3 1
A _fromDER() 0 11 2
A _valueASN1() 0 10 2
A issuerDomainPolicies() 0 7 1
A flattenedMappings() 0 11 3
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\X509\Certificate\Extension;
6
7
use Sop\ASN1\Element;
8
use Sop\ASN1\Type\Constructed\Sequence;
9
use Sop\ASN1\Type\UnspecifiedType;
10
use Sop\X509\Certificate\Extension\CertificatePolicy\PolicyInformation;
11
use Sop\X509\Certificate\Extension\PolicyMappings\PolicyMapping;
12
13
/**
14
 * Implements 'Policy Mappings' certificate extension.
15
 *
16
 * @see https://tools.ietf.org/html/rfc5280#section-4.2.1.5
17
 */
18
class PolicyMappingsExtension extends Extension implements \Countable, \IteratorAggregate
19
{
20
    /**
21
     * Policy mappings.
22
     *
23
     * @var PolicyMapping[]
24
     */
25
    protected $_mappings;
26
27
    /**
28
     * Constructor.
29
     *
30
     * @param bool          $critical
31
     * @param PolicyMapping ...$mappings One or more PolicyMapping objects
32
     */
33 5
    public function __construct(bool $critical, PolicyMapping ...$mappings)
34
    {
35 5
        parent::__construct(self::OID_POLICY_MAPPINGS, $critical);
36 5
        $this->_mappings = $mappings;
37 5
    }
38
39
    /**
40
     * Get all mappings.
41
     *
42
     * @return PolicyMapping[]
43
     */
44 2
    public function mappings(): array
45
    {
46 2
        return $this->_mappings;
47
    }
48
49
    /**
50
     * Get mappings flattened into a single array of arrays of subject domains
51
     * keyed by issuer domain.
52
     *
53
     * Eg. if policy mappings contains multiple mappings with the same issuer
54
     * domain policy, their corresponding subject domain policies are placed
55
     * under the same key.
56
     *
57
     * @return (string[])[]
58
     */
59 2
    public function flattenedMappings(): array
60
    {
61 2
        $mappings = [];
62 2
        foreach ($this->_mappings as $mapping) {
63 2
            $idp = $mapping->issuerDomainPolicy();
64 2
            if (!isset($mappings[$idp])) {
65 2
                $mappings[$idp] = [];
66
            }
67 2
            array_push($mappings[$idp], $mapping->subjectDomainPolicy());
68
        }
69 2
        return $mappings;
70
    }
71
72
    /**
73
     * Get all subject domain policy OIDs that are mapped to given issuer
74
     * domain policy OID.
75
     *
76
     * @param string $oid Issuer domain policy
77
     *
78
     * @return string[] List of OIDs in dotted format
79
     */
80 1
    public function issuerMappings(string $oid): array
81
    {
82 1
        $oids = [];
83 1
        foreach ($this->_mappings as $mapping) {
84 1
            if ($mapping->issuerDomainPolicy() === $oid) {
85 1
                $oids[] = $mapping->subjectDomainPolicy();
86
            }
87
        }
88 1
        return $oids;
89
    }
90
91
    /**
92
     * Get all mapped issuer domain policy OIDs.
93
     *
94
     * @return string[]
95
     */
96 1
    public function issuerDomainPolicies(): array
97
    {
98 1
        $idps = array_map(
99
            function (PolicyMapping $mapping) {
100 1
                return $mapping->issuerDomainPolicy();
101 1
            }, $this->_mappings);
102 1
        return array_values(array_unique($idps));
103
    }
104
105
    /**
106
     * Check whether policy mappings have anyPolicy mapped.
107
     *
108
     * RFC 5280 section 4.2.1.5 states that "Policies MUST NOT be mapped either
109
     * to or from the special value anyPolicy".
110
     *
111
     * @return bool
112
     */
113 7
    public function hasAnyPolicyMapping(): bool
114
    {
115 7
        foreach ($this->_mappings as $mapping) {
116 7
            if (PolicyInformation::OID_ANY_POLICY === $mapping->issuerDomainPolicy()) {
117 1
                return true;
118
            }
119 6
            if (PolicyInformation::OID_ANY_POLICY === $mapping->subjectDomainPolicy()) {
120 6
                return true;
121
            }
122
        }
123 4
        return false;
124
    }
125
126
    /**
127
     * Get the number of mappings.
128
     *
129
     * @see \Countable::count()
130
     *
131
     * @return int
132
     */
133 1
    public function count(): int
134
    {
135 1
        return count($this->_mappings);
136
    }
137
138
    /**
139
     * Get iterator for policy mappings.
140
     *
141
     * @see \IteratorAggregate::getIterator()
142
     *
143
     * @return \ArrayIterator
144
     */
145 1
    public function getIterator(): \ArrayIterator
146
    {
147 1
        return new \ArrayIterator($this->_mappings);
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153 2
    protected static function _fromDER(string $data, bool $critical): Extension
154
    {
155 2
        $mappings = array_map(
156
            function (UnspecifiedType $el) {
157 1
                return PolicyMapping::fromASN1($el->asSequence());
158 2
            }, UnspecifiedType::fromDER($data)->asSequence()->elements());
159 2
        if (!count($mappings)) {
160 1
            throw new \UnexpectedValueException(
161 1
                'PolicyMappings must have at least one mapping.');
162
        }
163 1
        return new self($critical, ...$mappings);
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169 6
    protected function _valueASN1(): Element
170
    {
171 6
        if (!count($this->_mappings)) {
172 1
            throw new \LogicException('No mappings.');
173
        }
174 5
        $elements = array_map(
175
            function (PolicyMapping $mapping) {
176 5
                return $mapping->toASN1();
177 5
            }, $this->_mappings);
178 5
        return new Sequence(...$elements);
179
    }
180
}
181