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

RsaPublicKey   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 38
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 3 1
A getResource() 0 3 1
A __construct() 0 13 3
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 RsaPublicKey
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 $id = null)
21
    {
22
        try {
23
            $this->resource = openssl_pkey_get_public(file_get_contents(realpath($filePath)));
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