1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Notimatica\Driver\Providers; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Psr7\Request; |
7
|
|
|
use GuzzleHttp\Psr7\Response; |
8
|
|
|
use Notimatica\Driver\Contracts\Notification; |
9
|
|
|
use Notimatica\Driver\Contracts\Subscriber; |
10
|
|
|
use Notimatica\Driver\Driver; |
11
|
|
|
use Notimatica\Driver\Events\NotificationFailed; |
12
|
|
|
use Notimatica\Driver\Events\NotificationSent; |
13
|
|
|
|
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) |
33
|
|
|
{ |
34
|
1 |
|
if (static::$browser) { |
35
|
1 |
|
return; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
static::$browser = new Client([ |
39
|
|
|
'timeout' => isset($config['timeout']) ? $config['timeout'] : static::DEFAULT_TIMEOUT, |
40
|
|
|
]); |
41
|
|
|
|
42
|
|
|
static::$headers['TTL'] = isset($config['ttl']) ? $config['ttl'] : static::DEFAULT_TTL; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Split endpoints for batch requests. |
47
|
|
|
* |
48
|
|
|
* @param array $endpoints |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
protected function batch(array $endpoints) |
52
|
|
|
{ |
53
|
|
|
return array_chunk($endpoints, (int) $this->config['max_batch_endpoints']); |
54
|
|
|
} |
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) |
99
|
|
|
{ |
100
|
2 |
|
$headers = static::$headers; |
101
|
2 |
|
$content = ''; |
102
|
|
|
|
103
|
2 |
|
foreach ($subscribers as $subscriber) { |
104
|
2 |
|
$headers['Content-Length'] = strlen($content); |
105
|
|
|
|
106
|
2 |
|
yield new Request('POST', $this->getUrl($subscriber), $headers, $content); |
107
|
2 |
|
} |
108
|
2 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Generate endpoint url. |
112
|
|
|
* |
113
|
|
|
* @param Subscriber $subscriber |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
2 |
|
protected function getUrl(Subscriber $subscriber) |
117
|
|
|
{ |
118
|
2 |
|
return $this->config['url'] . '/' . $subscriber->getToken(); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|