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.
Completed
Branch php72 (a7f01e)
by Joni
04:53
created

TargetInformationExtension   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
eloc 27
dl 0
loc 131
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A _fromDER() 0 7 1
A _valueASN1() 0 7 1
A groups() 0 3 1
A names() 0 3 1
A count() 0 3 1
A fromTargets() 0 3 1
A targets() 0 10 3
A getIterator() 0 3 1
A __clone() 0 3 1
A __construct() 0 4 1
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\Target\Target;
11
use Sop\X509\Certificate\Extension\Target\Targets;
12
13
/**
14
 * Implements 'AC Targeting' certificate extension.
15
 *
16
 * <b>NOTE</b>: Syntax is <i>SEQUENCE OF Targets</i>, but only one
17
 * <i>Targets</i> element must be used.
18
 * Multiple <i>Targets</i> elements shall be merged into single <i>Targets</i>.
19
 *
20
 * @see https://tools.ietf.org/html/rfc5755#section-4.3.2
21
 */
22
class TargetInformationExtension extends Extension implements \Countable, \IteratorAggregate
23
{
24
    /**
25
     * Targets elements.
26
     *
27
     * @var Targets[]
28
     */
29
    protected $_targets;
30
31
    /**
32
     * Targets[] merged to single Targets.
33
     *
34
     * @var null|Targets
35
     */
36
    private $_merged;
37
38
    /**
39
     * Constructor.
40
     *
41
     * @param bool    $critical
42
     * @param Targets ...$targets
43
     */
44 5
    public function __construct(bool $critical, Targets ...$targets)
45
    {
46 5
        parent::__construct(self::OID_TARGET_INFORMATION, $critical);
47 5
        $this->_targets = $targets;
48 5
    }
49
50
    /**
51
     * Reset internal state on clone.
52
     */
53 1
    public function __clone()
54
    {
55 1
        $this->_merged = null;
56 1
    }
57
58
    /**
59
     * Initialize from one or more Target objects.
60
     *
61
     * Extension criticality shall be set to true as specified by RFC 5755.
62
     *
63
     * @param Target ...$target
64
     *
65
     * @return TargetInformationExtension
66
     */
67 1
    public static function fromTargets(Target ...$target): self
68
    {
69 1
        return new self(true, new Targets(...$target));
70
    }
71
72
    /**
73
     * Get all targets.
74
     *
75
     * @return Targets
76
     */
77 8
    public function targets(): Targets
78
    {
79 8
        if (!isset($this->_merged)) {
80 4
            $a = [];
81 4
            foreach ($this->_targets as $targets) {
82 4
                $a = array_merge($a, $targets->all());
83
            }
84 4
            $this->_merged = new Targets(...$a);
85
        }
86 8
        return $this->_merged;
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->_merged could return the type null which is incompatible with the type-hinted return Sop\X509\Certificate\Extension\Target\Targets. Consider adding an additional type-check to rule them out.
Loading history...
87
    }
88
89
    /**
90
     * Get all name targets.
91
     *
92
     * @return Target[]
93
     */
94 1
    public function names(): array
95
    {
96 1
        return $this->targets()->nameTargets();
97
    }
98
99
    /**
100
     * Get all group targets.
101
     *
102
     * @return Target[]
103
     */
104 1
    public function groups(): array
105
    {
106 1
        return $this->targets()->groupTargets();
107
    }
108
109
    /**
110
     * @see \Countable::count()
111
     *
112
     * @return int
113
     */
114 1
    public function count(): int
115
    {
116 1
        return count($this->targets());
117
    }
118
119
    /**
120
     * Get iterator for targets.
121
     *
122
     * @see \IteratorAggregate::getIterator()
123
     *
124
     * @return \ArrayIterator
125
     */
126 1
    public function getIterator(): \ArrayIterator
127
    {
128 1
        return new \ArrayIterator($this->targets()->all());
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134 3
    protected static function _fromDER(string $data, bool $critical): Extension
135
    {
136 3
        $targets = array_map(
137
            function (UnspecifiedType $el) {
138 3
                return Targets::fromASN1($el->asSequence());
139 3
            }, UnspecifiedType::fromDER($data)->asSequence()->elements());
140 3
        return new self($critical, ...$targets);
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146 6
    protected function _valueASN1(): Element
147
    {
148 6
        $elements = array_map(
149
            function (Targets $targets) {
150 6
                return $targets->toASN1();
151 6
            }, $this->_targets);
152 6
        return new Sequence(...$elements);
153
    }
154
}
155