1 | <?php |
||
14 | class Firefox extends AbstractProvider |
||
15 | { |
||
16 | const NAME = 'firefox'; |
||
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 | 1 | protected static function initBrowser(array $config) |
|
44 | |||
45 | /** |
||
46 | * Split endpoints for batch requests. |
||
47 | * |
||
48 | * @param array $endpoints |
||
49 | * @return array |
||
50 | */ |
||
51 | protected function batch(array $endpoints) |
||
55 | |||
56 | /** |
||
57 | * Send notification. |
||
58 | * |
||
59 | * @param Notification $notification |
||
60 | * @param Subscriber[] $subscribers |
||
61 | */ |
||
62 | 1 | public function send(Notification $notification, array $subscribers) |
|
63 | { |
||
64 | 1 | static::initBrowser($this->config); |
|
65 | |||
66 | 1 | $this->flush( |
|
67 | 1 | $subscribers, |
|
68 | function (Response $response) use ($notification) { |
||
69 | try { |
||
70 | if ($response->getStatusCode() != 201) { |
||
71 | $response = json_decode($response->getBody()); |
||
72 | |||
73 | if (json_last_error() !== JSON_ERROR_NONE) { |
||
74 | throw new \Exception('Firefox: bad json'); |
||
75 | } |
||
76 | |||
77 | throw new \Exception($response->message); |
||
78 | } |
||
79 | |||
80 | Driver::emit(new NotificationSent($notification)); |
||
81 | } catch (\Exception $e) { |
||
82 | Driver::emit(new NotificationFailed($notification)); |
||
83 | } |
||
84 | 1 | }, |
|
85 | 1 | function () use ($notification) { |
|
86 | 1 | Driver::emit(new NotificationFailed($notification)); |
|
87 | 1 | } |
|
88 | 1 | ); |
|
89 | 1 | } |
|
90 | |||
91 | /** |
||
92 | * Send request. |
||
93 | * |
||
94 | * @param array $subscribers |
||
95 | * @param array|null $payload |
||
96 | * @return \Generator |
||
97 | */ |
||
98 | 2 | protected function prepareRequests($subscribers, $payload = null) |
|
109 | |||
110 | /** |
||
111 | * Generate endpoint url. |
||
112 | * |
||
113 | * @param Subscriber $subscriber |
||
114 | * @return string |
||
115 | */ |
||
116 | 2 | protected function getUrl(Subscriber $subscriber) |
|
120 | } |
||
121 |