-
Notifications
You must be signed in to change notification settings - Fork 312
Expand file tree
/
Copy pathSubscription.php
More file actions
103 lines (92 loc) · 3.44 KB
/
Subscription.php
File metadata and controls
103 lines (92 loc) · 3.44 KB
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
<?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
{
public const defaultContentEncoding = ContentEncoding::aesgcm; // Default for legacy input. The next major will use "aes128gcm" as defined to rfc8291.
protected ?ContentEncoding $contentEncoding = null;
/**
* This is a data class. No key validation is done.
* @param string|\Minishlink\WebPush\ContentEncoding|null $contentEncoding (Optional) defaults to "aesgcm". The next major will use "aes128gcm" as defined to rfc8291.
*/
public function __construct(
private string $endpoint,
private ?string $publicKey = null,
private ?string $authToken = null,
ContentEncoding|string|null $contentEncoding = null,
) {
if ($publicKey || $authToken || $contentEncoding) {
if (is_string($contentEncoding)) {
try {
if (empty($contentEncoding)) {
$contentEncoding = self::defaultContentEncoding;
} else {
$contentEncoding = ContentEncoding::from($contentEncoding);
}
} catch (\ValueError) {
throw new \ValueError('This content encoding ('.$contentEncoding.') is not supported.');
}
} elseif ($contentEncoding === null) {
$contentEncoding = self::defaultContentEncoding;
}
$this->contentEncoding = $contentEncoding;
}
}
/**
* @param array $associativeArray (with keys endpoint, publicKey, authToken, contentEncoding)
* @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'] ?? 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'] ?? ContentEncoding::aesgcm,
);
}
return new self(
$associativeArray['endpoint']
);
}
#[\Override]
public function getEndpoint(): string
{
return $this->endpoint;
}
#[\Override]
public function getPublicKey(): ?string
{
return $this->publicKey;
}
#[\Override]
public function getAuthToken(): ?string
{
return $this->authToken;
}
#[\Override]
public function getContentEncoding(): ?string
{
return $this->contentEncoding?->value;
}
public function getContentEncodingTyped(): ?ContentEncoding
{
return $this->contentEncoding;
}
}