PrivateKey   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 31
ccs 13
cts 14
cp 0.9286
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A read() 0 7 2
A sign() 0 8 2
1
<?php
2
3
namespace XmlSigner;
4
5
use XmlSigner\Exception\CertificateException;
6
7
class PrivateKey implements SignerInterface
8
{
9
    private $privateKey;
10
    private $resource;
11
12 6
    public function __construct(string $privateKey)
13
    {
14 6
        $this->privateKey = $privateKey;
15 6
        $this->read();
16 5
    }
17
18 6
    private function read()
19
    {
20 6
        if (!$resource = openssl_pkey_get_private($this->privateKey)) {
21 1
            throw CertificateException::privateKey();
22
        }
23 5
        $this->resource = $resource;
24 5
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 3
    public function sign($content, $algorithm = OPENSSL_ALGO_SHA1)
30
    {
31 3
        $encryptedData = '';
32 3
        if (!openssl_sign($content, $encryptedData, $this->resource, $algorithm)) {
33
            throw CertificateException::signContent();
34
        }
35 3
        return $encryptedData;
36
    }
37
}
38