P256EncryptedMessageBuilder   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 22
c 1
b 0
f 0
dl 0
loc 61
ccs 22
cts 22
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A withPublicKey() 0 5 1
A __construct() 0 4 1
A withAuthToken() 0 5 1
A build() 0 16 1
1
<?php
2
3
namespace AlexLisenkov\LaravelWebPush;
4
5
use AlexLisenkov\LaravelWebPush\Contracts\P256EncryptedMessageBuilderContract;
6
use AlexLisenkov\LaravelWebPush\Contracts\P256EncryptedMessageContract;
7
use Base64Url\Base64Url;
8
use Elliptic\EC;
9
use Illuminate\Contracts\Container\Container;
10
11
class P256EncryptedMessageBuilder implements P256EncryptedMessageBuilderContract
12
{
13
    /**
14
     * @var string
15
     */
16
    private $public_key;
17
    /**
18
     * @var string
19
     */
20
    private $auth_token;
21
    /**
22
     * @var EC
23
     */
24
    private $ec;
25
    /**
26
     * @var Container
27
     */
28
    private $container;
29
30
    /**
31
     * P256EncryptedMessageBuilder constructor.
32
     *
33
     * @param EC $ec
34
     * @param Container $container
35
     */
36 9
    public function __construct(EC $ec, Container $container)
37
    {
38 9
        $this->ec = $ec;
39 9
        $this->container = $container;
40 9
    }
41
42 8
    public function withPublicKey(string $public_key): P256EncryptedMessageBuilderContract
43
    {
44 8
        $this->public_key = $public_key;
45
46 8
        return $this;
47
    }
48
49 8
    public function withAuthToken(string $auth_token): P256EncryptedMessageBuilderContract
50
    {
51 8
        $this->auth_token = $auth_token;
52
53 8
        return $this;
54
    }
55
56 7
    public function build(string $payload): P256EncryptedMessageContract
57
    {
58 7
        $subscriber_public_key = Base64Url::decode($this->public_key);
59 7
        $subscriber_auth_token = Base64Url::decode($this->auth_token);
60
61 7
        $subscriber_p256 = $this->ec->keyFromPublic(bin2hex($subscriber_public_key), 'hex');
62 7
        $server_p256 = $this->ec->genKeyPair();
63
64 7
        $salt = random_bytes(Constants::SALT_BYTE_LENGTH);
65
66 7
        return $this->container->make(P256EncryptedMessageContract::class, [
67 7
            'private' => $server_p256,
68 7
            'subscriber' => $subscriber_p256,
69 7
            'auth_token' => $subscriber_auth_token,
70 7
            'salt' => $salt,
71 7
            'payload' => $payload,
72
        ]);
73
    }
74
}
75