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.
Passed
Push — master ( 96c540...075373 )
by Joni
01:37
created

PEMBundle::last()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\CryptoEncoding;
6
7
/**
8
 * Container for multiple PEM objects.
9
 *
10
 * The order of PEMs shall be retained, eg. when read from a file.
11
 */
12
class PEMBundle implements \Countable, \IteratorAggregate
13
{
14
    /**
15
     * Array of PEM objects.
16
     *
17
     * @var PEM[]
18
     */
19
    protected $_pems;
20
21
    /**
22
     * Constructor.
23
     *
24
     * @param PEM ...$pems
25
     */
26 3
    public function __construct(PEM ...$pems)
27
    {
28 3
        $this->_pems = $pems;
29 3
    }
30
31
    /**
32
     * @return string
33
     */
34 1
    public function __toString(): string
35
    {
36 1
        return $this->string();
37
    }
38
39
    /**
40
     * Initialize from a string.
41
     *
42
     * @param string $str
43
     *
44
     * @throws \UnexpectedValueException
45
     *
46
     * @return self
47
     */
48 3
    public static function fromString(string $str): self
49
    {
50 3
        if (!preg_match_all(PEM::PEM_REGEX, $str, $matches, PREG_SET_ORDER)) {
51 1
            throw new \UnexpectedValueException('No PEM blocks.');
52
        }
53 2
        $pems = array_map(
54
            function ($match) {
55 2
                $payload = preg_replace('/\s+/', '', $match[2]);
56 2
                $data = base64_decode($payload, true);
57 2
                if (false === $data) {
58 1
                    throw new \UnexpectedValueException(
59 1
                        'Failed to decode PEM data.');
60
                }
61 1
                return new PEM($match[1], $data);
62 2
            }, $matches);
63 1
        return new self(...$pems);
64
    }
65
66
    /**
67
     * Initialize from a file.
68
     *
69
     * @param string $filename
70
     *
71
     * @throws \RuntimeException If file reading fails
72
     *
73
     * @return self
74
     */
75 2
    public static function fromFile(string $filename): self
76
    {
77 2
        if (!is_readable($filename) ||
78 2
            false === ($str = file_get_contents($filename))) {
79 1
            throw new \RuntimeException("Failed to read {$filename}.");
80
        }
81 1
        return self::fromString($str);
82
    }
83
84
    /**
85
     * Get self with PEM objects appended.
86
     *
87
     * @param PEM ...$pems
88
     *
89
     * @return self
90
     */
91 1
    public function withPEMs(PEM ...$pems): self
92
    {
93 1
        $obj = clone $this;
94 1
        $obj->_pems = array_merge($obj->_pems, $pems);
95 1
        return $obj;
96
    }
97
98
    /**
99
     * Get all PEMs in a bundle.
100
     *
101
     * @return PEM[]
102
     */
103 3
    public function all(): array
104
    {
105 3
        return $this->_pems;
106
    }
107
108
    /**
109
     * Get the first PEM in a bundle.
110
     *
111
     * @throws \LogicException If bundle contains no PEM objects
112
     *
113
     * @return PEM
114
     */
115 2
    public function first(): PEM
116
    {
117 2
        if (!count($this->_pems)) {
118 1
            throw new \LogicException('No PEMs.');
119
        }
120 1
        return $this->_pems[0];
121
    }
122
123
    /**
124
     * Get the last PEM in a bundle.
125
     *
126
     * @throws \LogicException If bundle contains no PEM objects
127
     *
128
     * @return PEM
129
     */
130 2
    public function last(): PEM
131
    {
132 2
        if (!count($this->_pems)) {
133 1
            throw new \LogicException('No PEMs.');
134
        }
135 1
        return $this->_pems[count($this->_pems) - 1];
136
    }
137
138
    /**
139
     * @see \Countable::count()
140
     *
141
     * @return int
142
     */
143 2
    public function count(): int
144
    {
145 2
        return count($this->_pems);
146
    }
147
148
    /**
149
     * Get iterator for PEMs.
150
     *
151
     * @see \IteratorAggregate::getIterator()
152
     *
153
     * @return \ArrayIterator
154
     */
155 1
    public function getIterator(): \ArrayIterator
156
    {
157 1
        return new \ArrayIterator($this->_pems);
158
    }
159
160
    /**
161
     * Encode bundle to a string of contiguous PEM blocks.
162
     *
163
     * @return string
164
     */
165 2
    public function string(): string
166
    {
167 2
        return implode("\n",
168 2
            array_map(
169
                function (PEM $pem) {
170 2
                    return $pem->string();
171 2
                }, $this->_pems));
172
    }
173
}
174