X509   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromCertificateData() 0 10 1
A offsetSet() 0 7 2
A getCertificate() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\Certificate;
6
7
use function chunk_split;
8
use function preg_replace;
9
10
/**
11
 * Specific Certificate Key.
12
 */
13
class X509 extends Key
14
{
15
    /**
16
     * @param string $certificateContents
17
     * @return \SimpleSAML\SAML2\Certificate\X509
18
     */
19
    public static function createFromCertificateData(string $certificateContents): X509
20
    {
21
        $data = [
22
            'encryption'      => true,
23
            'signing'         => true,
24
            'type'            => 'X509Certificate',
25
            'X509Certificate' => $certificateContents,
26
        ];
27
28
        return new self($data);
29
    }
30
31
32
    /**
33
     * {@inheritdoc} Best place to ensure the logic is encapsulated in a single place
34
     *
35
     * @param mixed $offset
36
     * @param mixed $value
37
     *
38
     * Type hint not possible due to upstream method signature
39
     */
40
    public function offsetSet($offset, $value): void
41
    {
42
        if ($offset === 'X509Certificate') {
43
            $value = preg_replace('~\s+~', '', $value);
44
        }
45
46
        parent::offsetSet($offset, $value);
47
    }
48
49
50
    /**
51
     * Get the certificate representation
52
     */
53
    public function getCertificate(): string
54
    {
55
        return "-----BEGIN CERTIFICATE-----\n"
56
                . chunk_split($this->keyData['X509Certificate'], 64)
57
                . "-----END CERTIFICATE-----\n";
58
    }
59
}
60