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 ( dede62...bc6e1a )
by Joni
01:24
created

PEMBundle::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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