Passed
Branch master (f1e8df)
by Milad
05:36 queued 02:45
created

RsaPrivateKey::close()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace MiladRahimi\Jwt\Cryptography\Keys;
4
5
use MiladRahimi\Jwt\Exceptions\InvalidKeyException;
6
use Throwable;
7
8
class RsaPrivateKey
9
{
10
    /**
11
     * @var mixed Key file resource handler
12
     */
13
    private $resource;
14
15
    /**
16
     * @throws InvalidKeyException
17
     */
18
    public function __construct(string $filePath, string $passphrase = '')
19
    {
20
        try {
21
            $this->resource = openssl_pkey_get_private(file_get_contents(realpath($filePath)), $passphrase);
22
        } catch (Throwable $e) {
23
            throw new InvalidKeyException('Cannot load the private key.', 0, $e);
24
        }
25
26
        if ($this->resource === false) {
27
            throw new InvalidKeyException(openssl_error_string());
28
        }
29
    }
30
31
    /**
32
     * @return mixed
33
     */
34
    public function getResource()
35
    {
36
        return $this->resource;
37
    }
38
}
39