Targets   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 130
ccs 36
cts 36
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fromASN1() 0 8 1
A all() 0 4 1
A _allOfType() 0 8 1
A nameTargets() 0 4 1
A groupTargets() 0 4 1
A hasTarget() 0 9 3
A toASN1() 0 8 1
A count() 0 4 1
A getIterator() 0 4 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace X509\Certificate\Extension\Target;
6
7
use ASN1\Type\UnspecifiedType;
8
use ASN1\Type\Constructed\Sequence;
9
10
/**
11
 * Implements <i>Targets</i> ASN.1 type as a <i>SEQUENCE OF Target</i>.
12
 *
13
 * @link https://tools.ietf.org/html/rfc5755#section-4.3.2
14
 */
15
class Targets implements \Countable, \IteratorAggregate
16
{
17
    /**
18
     * Target elements.
19
     *
20
     * @var Target[] $_targets
21
     */
22
    protected $_targets;
23
    
24
    /**
25
     * Constructor.
26
     *
27
     * @param Target ...$targets
28
     */
29 11
    public function __construct(Target ...$targets)
30
    {
31 11
        $this->_targets = $targets;
32 11
    }
33
    
34
    /**
35
     * Initialize from ASN.1.
36
     *
37
     * @param Sequence $seq
38
     * @return self
39
     */
40 4
    public static function fromASN1(Sequence $seq): self
41
    {
42 4
        $targets = array_map(
43 4
            function (UnspecifiedType $el) {
44 4
                return Target::fromASN1($el->asTagged());
45 4
            }, $seq->elements());
46 4
        return new self(...$targets);
47
    }
48
    
49
    /**
50
     * Get all targets.
51
     *
52
     * @return Target[]
53
     */
54 7
    public function all(): array
55
    {
56 7
        return $this->_targets;
57
    }
58
    
59
    /**
60
     * Get all targets of given type.
61
     *
62
     * @param int $type
63
     * @return Target[]
64
     */
65 6
    protected function _allOfType(int $type): array
66
    {
67 6
        return array_values(
68 6
            array_filter($this->_targets,
69 6
                function (Target $target) use ($type) {
70 6
                    return $target->type() == $type;
71 6
                }));
72
    }
73
    
74
    /**
75
     * Get all name targets.
76
     *
77
     * @return Target[]
78
     */
79 1
    public function nameTargets(): array
80
    {
81 1
        return $this->_allOfType(Target::TYPE_NAME);
82
    }
83
    
84
    /**
85
     * Get all group targets.
86
     *
87
     * @return Target[]
88
     */
89 1
    public function groupTargets(): array
90
    {
91 1
        return $this->_allOfType(Target::TYPE_GROUP);
92
    }
93
    
94
    /**
95
     * Check whether given target is present.
96
     *
97
     * @param Target $target
98
     * @return boolean
99
     */
100 4
    public function hasTarget(Target $target): bool
101
    {
102 4
        foreach ($this->_allOfType($target->type()) as $t) {
103 4
            if ($target->equals($t)) {
104 4
                return true;
105
            }
106
        }
107 2
        return false;
108
    }
109
    
110
    /**
111
     * Generate ASN.1 structure.
112
     *
113
     * @return Sequence
114
     */
115 7
    public function toASN1(): Sequence
116
    {
117 7
        $elements = array_map(
118 7
            function (Target $target) {
119 7
                return $target->toASN1();
120 7
            }, $this->_targets);
121 7
        return new Sequence(...$elements);
122
    }
123
    
124
    /**
125
     *
126
     * @see \Countable::count()
127
     * @return int
128
     */
129 3
    public function count(): int
130
    {
131 3
        return count($this->_targets);
132
    }
133
    
134
    /**
135
     * Get iterator for targets.
136
     *
137
     * @see \IteratorAggregate::getIterator()
138
     * @return \ArrayIterator
139
     */
140 1
    public function getIterator(): \ArrayIterator
141
    {
142 1
        return new \ArrayIterator($this->_targets);
143
    }
144
}
145