|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Notimatica\Driver\Providers; |
|
4
|
|
|
|
|
5
|
|
|
use League\Flysystem\Filesystem; |
|
6
|
|
|
use Notimatica\Driver\Apns\Certificate; |
|
7
|
|
|
use Notimatica\Driver\Apns\Package; |
|
8
|
|
|
use Notimatica\Driver\Apns\Payload; |
|
9
|
|
|
use Notimatica\Driver\Apns\Streamer; |
|
10
|
|
|
use Notimatica\Driver\Contracts\Notification; |
|
11
|
|
|
use Notimatica\Driver\Contracts\Subscriber; |
|
12
|
|
|
use Notimatica\Driver\Driver; |
|
13
|
|
|
use Notimatica\Driver\Events\NotificationFailed; |
|
14
|
|
|
use Notimatica\Driver\Events\NotificationSent; |
|
15
|
|
|
use ZipStream\ZipStream; |
|
16
|
|
|
|
|
17
|
|
|
class Safari extends AbstractProvider |
|
18
|
|
|
{ |
|
19
|
|
|
const NAME = 'safari'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var Filesystem |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $storage; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Set files storage. |
|
28
|
|
|
* |
|
29
|
|
|
* @param Filesystem $filesStorage |
|
30
|
|
|
* @return $this |
|
31
|
|
|
*/ |
|
32
|
17 |
|
public function setStorage(Filesystem $filesStorage) |
|
33
|
|
|
{ |
|
34
|
17 |
|
$this->storage = $filesStorage; |
|
35
|
|
|
|
|
36
|
17 |
|
return $this; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Send notification. |
|
41
|
|
|
* |
|
42
|
|
|
* @param Notification $notification |
|
43
|
|
|
* @param Subscriber[] $subscribers |
|
44
|
|
|
*/ |
|
45
|
1 |
|
public function send(Notification $notification, array $subscribers) |
|
46
|
|
|
{ |
|
47
|
1 |
|
$certificate = new Certificate($this->storage); |
|
48
|
1 |
|
$stream = new Streamer($certificate, $this->config['url']); |
|
49
|
1 |
|
$payload = new Payload($notification); |
|
50
|
1 |
|
$payload = json_encode($payload); |
|
51
|
|
|
|
|
52
|
1 |
|
foreach ($this->prepareRequests($subscribers, $payload) as $message) { |
|
53
|
|
|
try { |
|
54
|
1 |
|
$stream->write($message); |
|
55
|
|
|
Driver::emit(new NotificationSent($notification)); |
|
56
|
1 |
|
} catch (\Exception $e) { |
|
57
|
1 |
|
Driver::emit(new NotificationFailed($notification)); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$stream->close(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Send request. |
|
66
|
|
|
* |
|
67
|
|
|
* @param Subscriber[] $subscribers |
|
68
|
|
|
* @param mixed $payload |
|
69
|
|
|
* @return \Generator |
|
70
|
|
|
*/ |
|
71
|
1 |
|
protected function prepareRequests($subscribers, $payload = null) |
|
72
|
|
|
{ |
|
73
|
1 |
|
foreach ($subscribers as $subscriber) { |
|
74
|
|
|
try { |
|
75
|
1 |
|
yield chr(0) . chr(0) . chr(32) . pack('H*', $subscriber->getToken()) . chr(0) . chr(strlen($payload)) . $payload; |
|
76
|
|
|
} catch (\Exception $e) { |
|
77
|
|
|
// Skip catch, because we don't need to handle if subscriber has an invalid token. |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Distribute connection package. |
|
84
|
|
|
* |
|
85
|
|
|
* @param array $extra |
|
86
|
|
|
* @return ZipStream |
|
87
|
|
|
*/ |
|
88
|
|
|
public function connectionPackage($extra = []) |
|
89
|
|
|
{ |
|
90
|
|
|
$certificate = new Certificate($this->storage); |
|
91
|
|
|
$website = [ |
|
92
|
|
|
'websiteName' => $this->project->name, |
|
93
|
|
|
'websitePushID' => $this->config['website_push_id'], |
|
94
|
|
|
'allowedDomains' => [$this->project->baseUrl], |
|
95
|
|
|
'urlFormatString' => "{$this->project->baseUrl}/go/%@", |
|
96
|
|
|
'webServiceURL' => $this->project->baseUrl . '/' . $this->config['subscribe_url'], |
|
97
|
|
|
]; |
|
98
|
|
|
|
|
99
|
|
|
array_merge($website, $extra); |
|
100
|
|
|
|
|
101
|
|
|
$package = new Package( |
|
102
|
|
|
$website, $certificate, $this->storage |
|
103
|
|
|
); |
|
104
|
|
|
|
|
105
|
|
|
return $package->generate(); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|