Passed
Push — master ( 463105...84cd59 )
by Roberto
04:47
created

CertificationChain   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 70.83%

Importance

Changes 0
Metric Value
wmc 19
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 140
ccs 34
cts 48
cp 0.7083
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A add() 0 15 2
A isBinary() 0 4 1
A removeExiredCertificates() 0 8 3
A listChain() 0 4 1
A __toString() 0 5 1
A getExtraCertsForPFX() 0 13 3
A loadListChain() 0 10 3
A loadList() 0 6 1
A rawString() 0 7 2
1
<?php
2
3
namespace NFePHP\Common\Certificate;
4
5
/**
6
 * Class for management and inclusion of certification chains to the public keys
7
 * of digital certificates model A1 (PKCS # 12)
8
 * @category   NFePHP
9
 * @package    NFePHP\Common\CertificationChain
10
 * @copyright  Copyright (c) 2008-2016
11
 * @license    http://www.gnu.org/licenses/lesser.html LGPL v3
12
 * @author     Roberto L. Machado <linux.rlm at gmail dot com>
13
 * @link       http://github.com/nfephp-org/sped-common for the canonical source repository
14
 */
15
16
use NFePHP\Common\Certificate\PublicKey;
17
18
class CertificationChain
19
{
20
    /**
21
     * @var string
22
     */
23
    private $rawKey = '';
24
    /**
25
     * @var array of PublicKeys::class
26
     */
27
    private $chainKeys = [];
28
    
29
    /**
30
     * Certification Chain Keys constructor
31
     * @param string $chainkeysstring
32
     */
33 6
    public function __construct($chainkeysstring = null)
34
    {
35 6
        if (!empty($chainkeysstring)) {
36 4
            $this->rawKey = $chainkeysstring;
37 4
            $this->loadListChain();
38
        }
39 6
    }
40
    
41
    /**
42
     * Add new certificate to certification chain
43
     * @param string $contente Certificate in DER, CER or PEM format
0 ignored issues
show
Documentation introduced by
There is no parameter named $contente. Did you maybe mean $content?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
44
     * @return array
45
     */
46 2
    public function add($content)
47
    {
48
        //verify format of certificate content if binary convert to PEM
49 2
        if ($this->isBinary($content)) {
50 1
            $content = base64_encode($content);
51 1
            $content = rtrim(chunk_split(preg_replace('/[\r\n]/', '', $content), 64, PHP_EOL));
52
            $content = <<<CONTENT
53
-----BEGIN CERTIFICATE-----
54 1
{$content}
55
-----END CERTIFICATE-----
56
57
CONTENT;
58
        }
59 2
        return $this->loadList($content);
60
    }
61
    
62
    /**
63
     * Detects if string contains binary characters
64
     * @param string $str
65
     * @return bool
66
     */
67 2
    private function isBinary($str)
68
    {
69 2
        return preg_match('~[^\x20-\x7E\t\r\n]~', $str) > 0;
70
    }
71
    
72
    /**
73
     * Remove certificate from certification chain by there common name
74
     */
75
    public function removeExiredCertificates()
76
    {
77
        foreach ($this->chainKeys as $key => $publickey) {
78
            if ($publickey->isExpired()) {
79
                unset($this->chainKeys[$key]);
80
            }
81
        }
82
    }
83
    
84
    /**
85
     * List certificates from actual certification chain
86
     * @return array
87
     */
88 3
    public function listChain()
89
    {
90 3
        return $this->chainKeys;
91
    }
92
    
93
    /**
94
     * Retuns all certificates in chain as string
95
     * @return string
96
     */
97 1
    public function __toString()
98
    {
99 1
        $this->rawString();
100 1
        return $this->rawKey;
101
    }
102
    
103
    /**
104
     * Returns a array for build extracerts in PFX files
105
     * @return array
106
     */
107
    public function getExtraCertsForPFX()
108
    {
109
        $ec = [];
110
        $args = [];
111
        $list = $this->chainKeys;
112
        foreach ($list as $cert) {
113
            $ec[] = "{$cert}";
114
        }
115
        if (empty($ec)) {
116
            $args = ['extracerts' => $ec];
117
        }
118
        return $args;
119
    }
120
    
121
    /**
122
     * Load chain certificates from string to array of PublicKey::class
123
     */
124 4
    private function loadListChain()
125
    {
126 4
        $arr = explode("-----END CERTIFICATE-----", $this->rawKey);
127 4
        foreach ($arr as $a) {
128 4
            if (strlen($a) > 20) {
129 4
                $cert = "$a-----END CERTIFICATE-----\n";
130 4
                $this->loadList($cert);
131
            }
132
        }
133 4
    }
134
    
135
    /**
136
     * Load PublicKey::class with certificates from chain
137
     * @param string $certificate
138
     * @return array
139
     */
140 6
    private function loadList($certificate)
141
    {
142 6
        $publickey = new PublicKey($certificate);
143 6
        $this->chainKeys[$publickey->commonName] = $publickey;
144 6
        return $this->chainKeys;
145
    }
146
    
147
    /**
148
     * Generate chain certificates as raw string
149
     */
150 1
    private function rawString()
151
    {
152 1
        $this->rawKey = '';
153 1
        foreach ($this->chainKeys as $publickey) {
154 1
            $this->rawKey .= "{$publickey}";
155
        }
156 1
    }
157
}
158