1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
<?php
declare(strict_types=1);
/*
* This file is part of the WebPush library.
*
* (c) Louis Lagrange <lagrange.louis@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Minishlink\WebPush;
class Subscription implements SubscriptionInterface
{
/** @var string */
private $endpoint;
/** @var null|string */
private $publicKey;
/** @var null|string */
private $authToken;
/** @var null|string */
private $contentEncoding;
/**
* Subscription constructor.
*
* @param string $endpoint
* @param null|string $publicKey
* @param null|string $authToken
* @param string $contentEncoding (Optional) Must be "aesgcm"
* @throws \ErrorException
*/
public function __construct(
string $endpoint,
?string $publicKey = null,
?string $authToken = null,
?string $contentEncoding = null
) {
$this->endpoint = $endpoint;
if ($publicKey || $authToken || $contentEncoding) {
$supportedContentEncodings = ['aesgcm', 'aes128gcm'];
if ($contentEncoding && !in_array($contentEncoding, $supportedContentEncodings)) {
throw new \ErrorException('This content encoding ('.$contentEncoding.') is not supported.');
}
$this->publicKey = $publicKey;
$this->authToken = $authToken;
$this->contentEncoding = $contentEncoding ?: "aesgcm";
}
}
/**
* Subscription factory.
*
* @param array $associativeArray (with keys endpoint, publicKey, authToken, contentEncoding)
* @return self
* @throws \ErrorException
*/
public static function create(array $associativeArray): self
{
if (array_key_exists('keys', $associativeArray) && is_array($associativeArray['keys'])) {
return new self(
$associativeArray['endpoint'],
$associativeArray['keys']['p256dh'] ?? null,
$associativeArray['keys']['auth'] ?? null,
$associativeArray['contentEncoding'] ?? "aesgcm"
);
}
if (array_key_exists('publicKey', $associativeArray) || array_key_exists('authToken', $associativeArray) || array_key_exists('contentEncoding', $associativeArray)) {
return new self(
$associativeArray['endpoint'],
$associativeArray['publicKey'] ?? null,
$associativeArray['authToken'] ?? null,
$associativeArray['contentEncoding'] ?? "aesgcm"
);
}
return new self(
$associativeArray['endpoint']
);
}
/**
* {@inheritDoc}
*/
public function getEndpoint(): string
{
return $this->endpoint;
}
/**
* {@inheritDoc}
*/
public function getPublicKey(): ?string
{
return $this->publicKey;
}
/**
* {@inheritDoc}
*/
public function getAuthToken(): ?string
{
return $this->authToken;
}
/**
* {@inheritDoc}
*/
public function getContentEncoding(): ?string
{
return $this->contentEncoding;
}
}
|