CRLDistributionPointsExtension   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 93
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A _fromDER() 0 14 2
A _valueASN1() 0 11 2
A distributionPoints() 0 4 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;
6
7
use ASN1\Type\UnspecifiedType;
8
use ASN1\Type\Constructed\Sequence;
9
use X509\Certificate\Extension\DistributionPoint\DistributionPoint;
10
11
/**
12
 * Implements 'CRL Distribution Points' certificate extension.
13
 *
14
 * @link https://tools.ietf.org/html/rfc5280#section-4.2.1.13
15
 */
16
class CRLDistributionPointsExtension extends Extension implements 
17
    \Countable,
18
    \IteratorAggregate
19
{
20
    /**
21
     * Distribution points.
22
     *
23
     * @var DistributionPoint[] $_distributionPoints
24
     */
25
    protected $_distributionPoints;
26
    
27
    /**
28
     * Constructor.
29
     *
30
     * @param bool $critical
31
     * @param DistributionPoint ...$distribution_points
32
     */
33 11
    public function __construct(bool $critical,
34
        DistributionPoint ...$distribution_points)
35
    {
36 11
        parent::__construct(self::OID_CRL_DISTRIBUTION_POINTS, $critical);
37 11
        $this->_distributionPoints = $distribution_points;
38 11
    }
39
    
40
    /**
41
     *
42
     * {@inheritdoc}
43
     * @return self
44
     */
45 11
    protected static function _fromDER(string $data, bool $critical): self
46
    {
47 11
        $dps = array_map(
48 11
            function (UnspecifiedType $el) {
49 10
                return DistributionPoint::fromASN1($el->asSequence());
50 11
            }, UnspecifiedType::fromDER($data)->asSequence()->elements());
51 11
        if (!count($dps)) {
52 1
            throw new \UnexpectedValueException(
53
                "CRLDistributionPoints must have" .
54 1
                     " at least one DistributionPoint.");
55
        }
56
        // late static bound, extended by Freshest CRL extension
57 10
        return new static($critical, ...$dps);
58
    }
59
    
60
    /**
61
     *
62
     * {@inheritdoc}
63
     * @return Sequence
64
     */
65 17
    protected function _valueASN1(): Sequence
66
    {
67 17
        if (!count($this->_distributionPoints)) {
68 1
            throw new \LogicException("No distribution points.");
69
        }
70 16
        $elements = array_map(
71 16
            function (DistributionPoint $dp) {
72 16
                return $dp->toASN1();
73 16
            }, $this->_distributionPoints);
74 16
        return new Sequence(...$elements);
75
    }
76
    
77
    /**
78
     * Get distribution points.
79
     *
80
     * @return DistributionPoint[]
81
     */
82 1
    public function distributionPoints(): array
83
    {
84 1
        return $this->_distributionPoints;
85
    }
86
    
87
    /**
88
     * Get the number of distribution points.
89
     *
90
     * @see \Countable::count()
91
     * @return int
92
     */
93 1
    public function count(): int
94
    {
95 1
        return count($this->_distributionPoints);
96
    }
97
    
98
    /**
99
     * Get iterator for distribution points.
100
     *
101
     * @see \IteratorAggregate::getIterator()
102
     * @return \ArrayIterator
103
     */
104 3
    public function getIterator(): \ArrayIterator
105
    {
106 3
        return new \ArrayIterator($this->_distributionPoints);
107
    }
108
}
109