PrivateKey::sign()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 5
cp 0.8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2.032
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