PublicKey::fromServerPublicKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php declare(strict_types = 1);
2
3
namespace Link0\Bunq\Domain\Keypair;
4
5
use \InvalidArgumentException;
6
7
final class PublicKey
8
{
9
    /**
10
     * @var string
11
     */
12
    private $key;
13
14
    /**
15
     * @param string $publicKey
16
     */
17
    public function __construct(string $publicKey)
18
    {
19
        if (!preg_match('/^\-\-\-\-\-BEGIN PUBLIC KEY\-\-\-\-\-[\S\n]+\-\-\-\-\-END PUBLIC KEY\-\-\-\-\-$/', $publicKey)) {
20
            throw new InvalidArgumentException("Invalid PublicKey format. Please use a shielded format");
21
        }
22
23
        $this->key = $publicKey;
24
    }
25
26
    /**
27
     * @param array $serverPublicKey
28
     * @return PublicKey
29
     */
30
    public static function fromServerPublicKey(array $serverPublicKey)
31
    {
32
        return new self($serverPublicKey['server_public_key']);
33
    }
34
35
    /**
36
     * @return string
37
     */
38
    public function __toString()
39
    {
40
        return $this->key;
41
    }
42
}
43