Completed
Pull Request — master (#46)
by Antonio Oertel
04:53
created

Certificate::sign()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 5
cts 7
cp 0.7143
rs 9.4285
cc 3
eloc 7
nc 3
nop 2
crap 3.2098
1
<?php
2
3
namespace NFePHP\Common;
4
5
use NFePHP\Common\Exception\CertificateException;
6
7
class Certificate
8
{
9
    /**
10
     * @var string
11
     */
12
    public $companyName;
13
14
    /**
15
     * @var string
16
     */
17
    public $privateKey;
18
19
    /**
20
     * @var string
21
     */
22
    public $publicKey;
23
24
    /**
25
     * @var \DateTime
26
     */
27
    public $validFrom;
28
29
    /**
30
     * @var \DateTime
31
     */
32
    public $validTo;
33
34 1
    public function __construct($content, $password = '')
35
    {
36 1
        $this->read($content, $password);
37 1
        $this->load();
38 1
    }
39
40 1
    private function read($content, $password)
41
    {
42 1
        $certs = [];
43 1
        if (!openssl_pkcs12_read($content, $certs, $password)) {
44
            throw CertificateException::unableToRead();
45
        }
46 1
        $this->privateKey = $certs['pkey'];
47 1
        $this->publicKey = $certs['cert'];
48 1
    }
49
    
50
    /**
51
     * Load info from certificate
52
     * @throws CertificateException
53
     */
54 1
    private function load()
55
    {
56 1
        if (!$resource = openssl_x509_read($this->publicKey)) {
57
            throw CertificateException::unableToOpen();
58
        }
59
60 1
        $detail = openssl_x509_parse($resource, false);
61 1
        $this->companyName = $detail['subject']['commonName'];
62 1
        $this->validFrom = \DateTime::createFromFormat('ymdHis\Z', $detail['validFrom']);
0 ignored issues
show
Documentation Bug introduced by
It seems like \DateTime::createFromFor..., $detail['validFrom']) can also be of type false. However, the property $validFrom is declared as type object<DateTime>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
63 1
        $this->validTo = \DateTime::createFromFormat('ymdHis\Z', $detail['validTo']);
0 ignored issues
show
Documentation Bug introduced by
It seems like \DateTime::createFromFor...Z', $detail['validTo']) can also be of type false. However, the property $validTo is declared as type object<DateTime>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
64 1
    }
65
66
    /**
67
     * Check if certificate has been expired.
68
     * @return bool Returns true when it is truth, otherwise false.
69
     */
70 1
    public function isExpired()
71
    {
72 1
        $now = new \DateTime('now');
73 1
        return $this->validFrom <= $now && $this->validTo >= $now;
74
    }
75
76 1
    public function sign($content, $algorithm = OPENSSL_ALGO_SHA1)
77
    {
78 1
        if (!$privateResource = openssl_pkey_get_private($this->privateKey)) {
79
            throw CertificateException::getPrivateKey();
80
        }
81
82 1
        $encryptedData = '';
83 1
        if (!openssl_sign($content, $encryptedData, $privateResource, $algorithm)) {
84
            throw CertificateException::signContent();
85
        }
86 1
        return $encryptedData;
87
    }
88
}
89