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.
Completed
Push — master ( 4f5f8d...895536 )
by Joni
06:58
created

PEM::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Sop\CryptoEncoding;
4
5
/**
6
 * Implements PEM file encoding and decoding.
7
 *
8
 * @link https://tools.ietf.org/html/rfc7468
9
 */
10
class PEM
11
{
12
    // well-known PEM types
13
    const TYPE_CERTIFICATE = "CERTIFICATE";
14
    const TYPE_CRL = "X509 CRL";
15
    const TYPE_CERTIFICATE_REQUEST = "CERTIFICATE REQUEST";
16
    const TYPE_ATTRIBUTE_CERTIFICATE = "ATTRIBUTE CERTIFICATE";
17
    const TYPE_PRIVATE_KEY = "PRIVATE KEY";
18
    const TYPE_PUBLIC_KEY = "PUBLIC KEY";
19
    const TYPE_ENCRYPTED_PRIVATE_KEY = "ENCRYPTED PRIVATE KEY";
20
    const TYPE_RSA_PRIVATE_KEY = "RSA PRIVATE KEY";
21
    const TYPE_RSA_PUBLIC_KEY = "RSA PUBLIC KEY";
22
    const TYPE_EC_PRIVATE_KEY = "EC PRIVATE KEY";
23
    const TYPE_PKCS7 = "PKCS7";
24
    const TYPE_CMS = "CMS";
25
    
26
    /**
27
     * Regular expression to match PEM block.
28
     *
29
     * @var string
30
     */
31
    const PEM_REGEX = /* @formatter:off */ '/' .
32
        /* line start */ '(?:^|[\r\n])' .
33
        /* header */     '-----BEGIN (.+?)-----[\r\n]+' .
34
        /* payload */    '(.+?)' .
35
        /* trailer */    '[\r\n]+-----END \\1-----' .
36
    '/ms'; /* @formatter:on */
37
    
38
    /**
39
     * Content type.
40
     *
41
     * @var string $_type
42
     */
43
    protected $_type;
44
    
45
    /**
46
     * Payload.
47
     *
48
     * @var string $_data
49
     */
50
    protected $_data;
51
    
52
    /**
53
     * Constructor.
54
     *
55
     * @param string $type Content type
56
     * @param string $data Payload
57
     */
58 5
    public function __construct($type, $data)
59
    {
60 5
        $this->_type = $type;
61 5
        $this->_data = $data;
62 5
    }
63
    
64
    /**
65
     * Initialize from a PEM-formatted string.
66
     *
67
     * @param string $str
68
     * @throws \UnexpectedValueException If string is not valid PEM
69
     * @return self
70
     */
71 5
    public static function fromString($str)
72
    {
73 5
        if (!preg_match(self::PEM_REGEX, $str, $match)) {
74 1
            throw new \UnexpectedValueException("Not a PEM formatted string.");
75
        }
76 4
        $payload = preg_replace('/\s+/', "", $match[2]);
77 4
        $data = base64_decode($payload, true);
78 4
        if (false === $data) {
79 1
            throw new \UnexpectedValueException("Failed to decode PEM data.");
80
        }
81 3
        return new self($match[1], $data);
82
    }
83
    
84
    /**
85
     * Initialize from a file.
86
     *
87
     * @param string $filename Path to file
88
     * @throws \RuntimeException If file reading fails
89
     * @return self
90
     */
91 2 View Code Duplication
    public static function fromFile($filename)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
    {
93 2
        if (!is_readable($filename) ||
94 2
             false === ($str = file_get_contents($filename))) {
95 1
            throw new \RuntimeException("Failed to read $filename.");
96
        }
97 1
        return self::fromString($str);
98
    }
99
    
100
    /**
101
     * Get content type.
102
     *
103
     * @return string
104
     */
105 1
    public function type()
106
    {
107 1
        return $this->_type;
108
    }
109
    
110
    /**
111
     * Get payload.
112
     *
113
     * @return string
114
     */
115 1
    public function data()
116
    {
117 1
        return $this->_data;
118
    }
119
    
120
    /**
121
     * Encode to PEM string.
122
     *
123
     * @return string
124
     */
125 4
    public function string()
126
    {
127 4
        return "-----BEGIN {$this->_type}-----\n" .
128 4
             trim(chunk_split(base64_encode($this->_data), 64, "\n")) . "\n" .
129 4
             "-----END {$this->_type}-----";
130
    }
131
    
132
    /**
133
     *
134
     * @return string
135
     */
136 1
    public function __toString()
137
    {
138 1
        return $this->string();
139
    }
140
}
141