1 | <?php |
||
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 | 16 | public function setStorage(Filesystem $filesStorage) |
|
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 | 1 | } |
|
60 | |||
61 | 1 | $stream->close(); |
|
62 | 1 | } |
|
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 | 1 | } catch (\Exception $e) { |
|
77 | // Skip catch, because we don't need to handle if subscriber has an invalid token. |
||
78 | } |
||
79 | 1 | } |
|
80 | 1 | } |
|
81 | |||
82 | /** |
||
83 | * Distribute connection package. |
||
84 | * |
||
85 | * @param array $extra |
||
86 | * @return ZipStream |
||
87 | */ |
||
88 | public function connectionPackage($extra = []) |
||
107 | } |
||
108 |