|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AlexLisenkov\LaravelWebPush; |
|
4
|
|
|
|
|
5
|
|
|
use AlexLisenkov\LaravelWebPush\Contracts\PushMessageContract; |
|
6
|
|
|
use AlexLisenkov\LaravelWebPush\Contracts\PushSubscriptionContract; |
|
7
|
|
|
use AlexLisenkov\LaravelWebPush\Contracts\WebPushContract; |
|
8
|
|
|
use GuzzleHttp\Promise\PromiseInterface; |
|
9
|
|
|
use GuzzleHttp\Psr7\Request; |
|
10
|
|
|
use Illuminate\Support\Facades\App; |
|
11
|
|
|
|
|
12
|
|
|
class PushSubscription implements PushSubscriptionContract |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
private $endpoint; |
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
private $p256dh; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
private $auth; |
|
26
|
|
|
|
|
27
|
8 |
|
public function __construct(string $endpoint, string $p256dh, string $auth) |
|
28
|
|
|
{ |
|
29
|
8 |
|
$this->endpoint = $endpoint; |
|
30
|
8 |
|
$this->p256dh = $p256dh; |
|
31
|
8 |
|
$this->auth = $auth; |
|
32
|
8 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param PushMessageContract $push_message |
|
36
|
|
|
* |
|
37
|
|
|
* @return PromiseInterface |
|
38
|
|
|
*/ |
|
39
|
1 |
|
public function sendMessage(PushMessageContract $push_message): PromiseInterface |
|
40
|
|
|
{ |
|
41
|
|
|
/** @var WebPushContract $web_push */ |
|
42
|
1 |
|
$web_push = App::make(WebPushContract::class); |
|
43
|
|
|
|
|
44
|
1 |
|
return $web_push->sendMessage($push_message, $this); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Define public key for the subscriber |
|
49
|
|
|
* |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
2 |
|
public function getP256dh(): string |
|
53
|
|
|
{ |
|
54
|
2 |
|
return $this->p256dh; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Set P256dh |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $p256dh |
|
61
|
|
|
* |
|
62
|
|
|
* @return PushSubscription |
|
63
|
|
|
*/ |
|
64
|
1 |
|
public function setP256dh(string $p256dh): PushSubscription |
|
65
|
|
|
{ |
|
66
|
1 |
|
$this->p256dh = $p256dh; |
|
67
|
|
|
|
|
68
|
1 |
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Define the auth string |
|
73
|
|
|
* |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
2 |
|
public function getAuth(): string |
|
77
|
|
|
{ |
|
78
|
2 |
|
return $this->auth; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Set Auth |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $auth |
|
85
|
|
|
* |
|
86
|
|
|
* @return PushSubscription |
|
87
|
|
|
*/ |
|
88
|
1 |
|
public function setAuth(string $auth): PushSubscription |
|
89
|
|
|
{ |
|
90
|
1 |
|
$this->auth = $auth; |
|
91
|
|
|
|
|
92
|
1 |
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
1 |
|
public function getAudience(): string |
|
99
|
|
|
{ |
|
100
|
1 |
|
$audience = new Request('get', $this->getEndpoint()); |
|
101
|
|
|
|
|
102
|
1 |
|
return $audience->getUri()->getScheme() . '://' . $audience->getUri()->getHost(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Define the endpoint |
|
107
|
|
|
* |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
3 |
|
public function getEndpoint(): string |
|
111
|
|
|
{ |
|
112
|
3 |
|
return $this->endpoint; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Set Endpoint |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $endpoint |
|
119
|
|
|
* |
|
120
|
|
|
* @return PushSubscription |
|
121
|
|
|
*/ |
|
122
|
1 |
|
public function setEndpoint(string $endpoint): PushSubscription |
|
123
|
|
|
{ |
|
124
|
1 |
|
$this->endpoint = $endpoint; |
|
125
|
|
|
|
|
126
|
1 |
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|