Passed
Push — master ( bf67e8...626507 )
by Milad
02:27
created

EcdsaPrivateKey::getId()   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
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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 EcdsaPrivateKey
9
{
10
    /**
11
     * @var mixed Key file resource handler
12
     */
13
    private $resource;
14
15
    protected ?string $id;
16
17
    /**
18
     * @throws InvalidKeyException
19
     */
20
    public function __construct(string $filePath, string $passphrase = '', ?string $id = null)
21
    {
22
        try {
23
            $this->resource = openssl_pkey_get_private(file_get_contents(realpath($filePath)), $passphrase);
24
        } catch (Throwable $e) {
25
            throw new InvalidKeyException('Failed to read the key.', 0, $e);
26
        }
27
28
        if ($this->resource === false) {
29
            throw new InvalidKeyException(openssl_error_string());
30
        }
31
32
        $this->id = $id;
33
    }
34
35
    /**
36
     * @return mixed
37
     */
38
    public function getResource()
39
    {
40
        return $this->resource;
41
    }
42
43
    public function getId(): ?string
44
    {
45
        return $this->id;
46
    }
47
}
48