PrivateKey::__construct()   A
last analyzed

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 1
crap 1
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