-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Notification.php
177 lines (138 loc) · 3.31 KB
/
Notification.php
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2020-2021 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace WebPush;
use function array_key_exists;
use Assert\Assertion;
class Notification
{
public const URGENCY_VERY_LOW = 'very-low';
public const URGENCY_LOW = 'low';
public const URGENCY_NORMAL = 'normal';
public const URGENCY_HIGH = 'high';
private ?string $payload = null;
private int $ttl = 0;
private string $urgency = self::URGENCY_NORMAL;
private ?string $topic = null;
private bool $respondAsync = false;
/**
* @var array<string, mixed>
*/
private array $metadata = [];
public static function create(): self
{
return new self();
}
public function veryLowUrgency(): self
{
$this->urgency = self::URGENCY_VERY_LOW;
return $this;
}
public function lowUrgency(): self
{
$this->urgency = self::URGENCY_LOW;
return $this;
}
public function normalUrgency(): self
{
$this->urgency = self::URGENCY_NORMAL;
return $this;
}
public function highUrgency(): self
{
$this->urgency = self::URGENCY_HIGH;
return $this;
}
public function withUrgency(string $urgency): self
{
Assertion::inArray($urgency, [
self::URGENCY_VERY_LOW,
self::URGENCY_LOW,
self::URGENCY_NORMAL,
self::URGENCY_HIGH,
], 'Invalid urgency parameter');
$this->urgency = $urgency;
return $this;
}
public function getUrgency(): string
{
return $this->urgency;
}
public function withPayload(string $payload): self
{
$this->payload = $payload;
return $this;
}
public function getPayload(): ?string
{
return $this->payload;
}
public function withTopic(string $topic): self
{
Assertion::notBlank($topic, 'Invalid topic');
$this->topic = $topic;
return $this;
}
public function getTopic(): ?string
{
return $this->topic;
}
public function withTTL(int $ttl): self
{
Assertion::greaterOrEqualThan($ttl, 0, 'Invalid TTL');
$this->ttl = $ttl;
return $this;
}
public function getTTL(): int
{
return $this->ttl;
}
public function sync(): self
{
$this->respondAsync = false;
return $this;
}
public function async(): self
{
$this->respondAsync = true;
return $this;
}
public function isAsync(): bool
{
return $this->respondAsync;
}
/**
* @return array<string, mixed>
*/
public function getMetadata(): array
{
return $this->metadata;
}
/**
* @param mixed $data
*/
public function add(string $key, $data): self
{
$this->metadata[$key] = $data;
return $this;
}
public function has(string $key): bool
{
return array_key_exists($key, $this->metadata);
}
/**
* @return mixed
*/
public function get(string $key)
{
Assertion::true($this->has($key), 'Missing metadata');
return $this->metadata[$key];
}
}