RsaPrivateKey   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 39
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 3 1
A getResource() 0 3 1
A __construct() 0 10 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MiladRahimi\Jwt\Cryptography\Keys;
6
7
use MiladRahimi\Jwt\Exceptions\InvalidKeyException;
8
9
class RsaPrivateKey
10
{
11
    /**
12
     * @var mixed Key resource handler
13
     */
14
    protected $resource;
15
16
    protected ?string $id;
17
18
    /**
19
     * @param string $key Key file path or string content
20
     * @param string $passphrase Key passphrase
21
     * @param string|null $id Key identifier
22
     *
23
     * @throws InvalidKeyException
24
     */
25
    public function __construct(string $key, string $passphrase = '', ?string $id = null)
26
    {
27
        $content = realpath($key) ? file_get_contents(realpath($key)) : $key;
28
29
        $this->resource = openssl_pkey_get_private($content, $passphrase);
30
        if ($this->resource === false) {
31
            throw new InvalidKeyException(openssl_error_string());
32
        }
33
34
        $this->id = $id;
35
    }
36
37
    /**
38
     * @return mixed
39
     */
40
    public function getResource()
41
    {
42
        return $this->resource;
43
    }
44
45
    public function getId(): ?string
46
    {
47
        return $this->id;
48
    }
49
}
50