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.

CertificateChain   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
eloc 25
dl 0
loc 133
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A fromPEMs() 0 7 1
A certificationPath() 0 3 1
A trustAnchorCertificate() 0 6 2
A count() 0 3 1
A certificates() 0 3 1
A getIterator() 0 3 1
A __construct() 0 3 1
A fromPEMString() 0 4 1
A toPEMString() 0 7 1
A endEntityCertificate() 0 6 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\X509\Certificate;
6
7
use Sop\CryptoEncoding\PEM;
8
use Sop\CryptoEncoding\PEMBundle;
9
use Sop\X509\CertificationPath\CertificationPath;
10
11
/**
12
 * Ordered list of certificates from the end-entity to the trust anchor.
13
 */
14
class CertificateChain implements \Countable, \IteratorAggregate
15
{
16
    /**
17
     * List of certificates in a chain.
18
     *
19
     * @var Certificate[]
20
     */
21
    protected $_certs;
22
23
    /**
24
     * Constructor.
25
     *
26
     * @param Certificate ...$certs List of certificates, end-entity first
27
     */
28 7
    public function __construct(Certificate ...$certs)
29
    {
30 7
        $this->_certs = $certs;
31 7
    }
32
33
    /**
34
     * Initialize from a list of PEMs.
35
     *
36
     * @param PEM ...$pems
37
     *
38
     * @return self
39
     */
40 2
    public static function fromPEMs(PEM ...$pems): self
41
    {
42 2
        $certs = array_map(
43
            function (PEM $pem) {
44 2
                return Certificate::fromPEM($pem);
45 2
            }, $pems);
46 2
        return new self(...$certs);
47
    }
48
49
    /**
50
     * Initialize from a string containing multiple PEM blocks.
51
     *
52
     * @param string $str
53
     *
54
     * @return self
55
     */
56 1
    public static function fromPEMString(string $str): self
57
    {
58 1
        $pems = PEMBundle::fromString($str)->all();
59 1
        return self::fromPEMs(...$pems);
60
    }
61
62
    /**
63
     * Get all certificates in a chain ordered from the end-entity certificate
64
     * to the trust anchor.
65
     *
66
     * @return Certificate[]
67
     */
68 3
    public function certificates(): array
69
    {
70 3
        return $this->_certs;
71
    }
72
73
    /**
74
     * Get the end-entity certificate.
75
     *
76
     * @throws \LogicException
77
     *
78
     * @return Certificate
79
     */
80 2
    public function endEntityCertificate(): Certificate
81
    {
82 2
        if (!count($this->_certs)) {
83 1
            throw new \LogicException('No certificates.');
84
        }
85 1
        return $this->_certs[0];
86
    }
87
88
    /**
89
     * Get the trust anchor certificate.
90
     *
91
     * @throws \LogicException
92
     *
93
     * @return Certificate
94
     */
95 2
    public function trustAnchorCertificate(): Certificate
96
    {
97 2
        if (!count($this->_certs)) {
98 1
            throw new \LogicException('No certificates.');
99
        }
100 1
        return $this->_certs[count($this->_certs) - 1];
101
    }
102
103
    /**
104
     * Convert certificate chain to certification path.
105
     *
106
     * @return CertificationPath
107
     */
108 1
    public function certificationPath(): CertificationPath
109
    {
110 1
        return CertificationPath::fromCertificateChain($this);
111
    }
112
113
    /**
114
     * Convert certificate chain to string of PEM blocks.
115
     *
116
     * @return string
117
     */
118 1
    public function toPEMString(): string
119
    {
120 1
        return implode("\n",
121 1
            array_map(
122
                function (Certificate $cert) {
123 1
                    return $cert->toPEM()->string();
124 1
                }, $this->_certs));
125
    }
126
127
    /**
128
     * @see \Countable::count()
129
     *
130
     * @return int
131
     */
132 1
    public function count(): int
133
    {
134 1
        return count($this->_certs);
135
    }
136
137
    /**
138
     * Get iterator for certificates.
139
     *
140
     * @see \IteratorAggregate::getIterator()
141
     *
142
     * @return \ArrayIterator
143
     */
144 2
    public function getIterator(): \ArrayIterator
145
    {
146 2
        return new \ArrayIterator($this->_certs);
147
    }
148
}
149