1 | <?php |
||
14 | class Chrome extends AbstractProvider |
||
15 | { |
||
16 | const NAME = 'chrome'; |
||
17 | const DEFAULT_TTL = 2419200; |
||
18 | const DEFAULT_TIMEOUT = 30; |
||
19 | |||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | protected static $headers = [ |
||
24 | 'Content-Type' => 'application/json', |
||
25 | ]; |
||
26 | |||
27 | /** |
||
28 | * Init Browser. |
||
29 | * |
||
30 | * @param array $config |
||
31 | */ |
||
32 | 2 | protected static function initBrowser(array $config) |
|
45 | |||
46 | /** |
||
47 | * Split endpoints for batch requests. |
||
48 | * |
||
49 | * @param array $endpoints |
||
50 | * @return array |
||
51 | */ |
||
52 | 2 | protected function batch(array $endpoints) |
|
56 | |||
57 | /** |
||
58 | * Send notification. |
||
59 | * |
||
60 | * @param Notification $notification |
||
61 | * @param Subscriber[] $subscribers |
||
62 | */ |
||
63 | 1 | public function send(Notification $notification, array $subscribers) |
|
64 | { |
||
65 | 1 | static::initBrowser($this->config); |
|
66 | 1 | $total = count($subscribers); |
|
67 | |||
68 | 1 | $this->flush( |
|
69 | 1 | $subscribers, |
|
70 | function (Response $response, $index) use ($notification, $total) { |
||
71 | try { |
||
72 | $response = json_decode($response->getBody()); |
||
73 | |||
74 | if (json_last_error() !== JSON_ERROR_NONE) { |
||
75 | throw new \Exception(); |
||
76 | } |
||
77 | |||
78 | if ($response->success > 0) { |
||
79 | Driver::emit(new NotificationSent($notification, (int) $response->success)); |
||
80 | } |
||
81 | |||
82 | if ($response->failure > 0) { |
||
83 | Driver::emit(new NotificationFailed($notification, (int) $response->failure)); |
||
84 | 1 | } |
|
85 | } catch (\Exception $e) { |
||
86 | Driver::emit(new NotificationFailed($notification, $this->calculateChunkSize($index, $total))); |
||
87 | } |
||
88 | 1 | }, |
|
89 | 1 | function ($reason, $index) use ($notification, $total) { |
|
90 | 1 | Driver::emit(new NotificationFailed($notification, $this->calculateChunkSize($index, $total))); |
|
91 | 1 | } |
|
92 | 1 | ); |
|
93 | 1 | } |
|
94 | |||
95 | /** |
||
96 | * Send request. |
||
97 | * |
||
98 | * @param array $subscribers |
||
99 | * @param null $payload |
||
100 | * @return \Generator |
||
101 | */ |
||
102 | 2 | protected function prepareRequests($subscribers, $payload = null) |
|
114 | |||
115 | /** |
||
116 | * Calculate chunk size. |
||
117 | * Problem is, we don't know the latter chunk size. |
||
118 | * |
||
119 | * @param int $index |
||
120 | * @param int $total |
||
121 | * @return int |
||
122 | */ |
||
123 | 2 | protected function calculateChunkSize($index, $total) |
|
140 | |||
141 | /** |
||
142 | * Send notification to endpoints. |
||
143 | * |
||
144 | * @param Subscriber[] $subscribers |
||
145 | * @return array |
||
146 | */ |
||
147 | 3 | protected function getRequestContent(array $subscribers) |
|
160 | |||
161 | /** |
||
162 | * Generate manifest file text. |
||
163 | * |
||
164 | * @return string |
||
165 | */ |
||
166 | 1 | public function manifest() |
|
173 | } |
||
174 |