Passed
Pull Request — master (#8)
by Tim
05:17 queued 03:13
created

Certificate   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 12
c 2
b 0
f 0
dl 0
loc 48
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A hasValidStructure() 0 3 1
A convertToCertificate() 0 5 1
A parseIssuer() 0 11 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Utils;
6
7
/**
8
 * Collection of Utility functions specifically for certificates
9
 */
10
class Certificate
11
{
12
    /**
13
     * The pattern that the contents of a certificate should adhere to
14
     */
15
    public const CERTIFICATE_PATTERN = '/^-----BEGIN CERTIFICATE-----([^-]*)^-----END CERTIFICATE-----/m';
16
17
18
    /**
19
     * @param string $certificate
20
     *
21
     * @return bool
22
     */
23
    public static function hasValidStructure(string $certificate): bool
24
    {
25
        return !!preg_match(self::CERTIFICATE_PATTERN, $certificate);
26
    }
27
28
29
    /**
30
     * @param string $X509CertificateContents
31
     *
32
     * @return string
33
     */
34
    public static function convertToCertificate(string $X509CertificateContents): string
35
    {
36
        return "-----BEGIN CERTIFICATE-----\n"
37
                . chunk_split($X509CertificateContents, 64, "\n")
38
                . "-----END CERTIFICATE-----";
39
    }
40
41
42
    /**
43
     * @param array|string $issuer
44
     *
45
     * @return string
46
     */
47
    public static function parseIssuer($issuer): string
48
    {
49
        if (is_array($issuer)) {
50
            $parts = [];
51
            foreach ($issuer as $key => $value) {
52
                array_unshift($parts, $key . '=' . $value);
53
            }
54
            return implode(',', $parts);
55
        }
56
57
        return $issuer;
58
    }
59
}
60