1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Notimatica\Driver; |
4
|
|
|
|
5
|
|
|
use Notimatica\Driver\Contracts\Notification; |
6
|
|
|
use Notimatica\Driver\Contracts\PayloadStorage as PayloadStorageContract; |
7
|
|
|
use Notimatica\Driver\Contracts\Subscriber; |
8
|
|
|
use Notimatica\Driver\Providers\AbstractProvider; |
9
|
|
|
use Notimatica\Driver\Support\EventsEmitter; |
10
|
|
|
|
11
|
|
|
class Driver |
12
|
|
|
{ |
13
|
|
|
use EventsEmitter; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var Project |
17
|
|
|
*/ |
18
|
|
|
protected $project; |
19
|
|
|
/** |
20
|
|
|
* @var Notification |
21
|
|
|
*/ |
22
|
|
|
protected $notification; |
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $subscribers; |
27
|
|
|
/** |
28
|
|
|
* @var PayloadStorageContract |
29
|
|
|
*/ |
30
|
|
|
protected $payloadStorage; |
31
|
|
|
/** |
32
|
|
|
* @var Statistics |
33
|
|
|
*/ |
34
|
|
|
protected $statisticsStorage; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Create a new Driver. |
38
|
|
|
* |
39
|
|
|
* @param Project $project |
40
|
|
|
* @param PayloadStorageContract $payloadStorage |
41
|
|
|
* @param Statistics $statisticsStorage |
42
|
|
|
*/ |
43
|
8 |
|
public function __construct(Project $project, PayloadStorageContract $payloadStorage = null, Statistics $statisticsStorage = null) |
44
|
|
|
{ |
45
|
8 |
|
$this->project = $project; |
46
|
8 |
|
$this->payloadStorage = $payloadStorage; |
47
|
8 |
|
$this->statisticsStorage = $statisticsStorage; |
48
|
|
|
|
49
|
8 |
|
$this->boot(); |
50
|
8 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Boot driver. |
54
|
|
|
*/ |
55
|
8 |
|
public function boot() |
56
|
|
|
{ |
57
|
8 |
|
$this->bootEvents(); |
58
|
8 |
|
$this->bootListeners(); |
59
|
8 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Send notification. |
63
|
|
|
* |
64
|
|
|
* @param Notification $notification |
65
|
|
|
* @return $this |
66
|
|
|
*/ |
67
|
5 |
|
public function send(Notification $notification) |
68
|
|
|
{ |
69
|
5 |
|
$this->notification = $notification; |
70
|
|
|
|
71
|
5 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Endpoints to send to. |
76
|
|
|
* |
77
|
|
|
* @param array $subscribers |
78
|
|
|
* @return $this |
79
|
|
|
*/ |
80
|
5 |
|
public function to(array $subscribers) |
81
|
|
|
{ |
82
|
5 |
|
$this->subscribers = $subscribers; |
83
|
|
|
|
84
|
5 |
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Send notification. |
89
|
|
|
*/ |
90
|
5 |
|
public function flush() |
91
|
|
|
{ |
92
|
5 |
|
$this->validate(); |
93
|
|
|
|
94
|
3 |
|
$partials = $this->splitSubscribers(); |
95
|
|
|
|
96
|
3 |
|
foreach ($partials as $provider => $subscribers) { |
97
|
|
|
try { |
98
|
3 |
|
$this->provider($provider)->send($this->notification, $subscribers); |
99
|
3 |
|
} catch (\RuntimeException $e) { |
100
|
|
|
static::emit('flush.exception', $e); |
101
|
|
|
} |
102
|
3 |
|
} |
103
|
3 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Cast provider. |
107
|
|
|
* |
108
|
|
|
* @param string $name |
109
|
|
|
* @return AbstractProvider |
110
|
|
|
* @throws \RuntimeException |
111
|
|
|
*/ |
112
|
3 |
|
public function provider($name) |
113
|
|
|
{ |
114
|
3 |
|
return $this->project->getProvider($name); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Prepare notifications. |
119
|
|
|
* Split subscribers by their providers and prepare payload. |
120
|
|
|
*/ |
121
|
4 |
|
protected function splitSubscribers() |
122
|
|
|
{ |
123
|
4 |
|
$partials = []; |
124
|
|
|
|
125
|
|
|
/** @var Subscriber $subscriber */ |
126
|
4 |
|
foreach ($this->subscribers as $subscriber) { |
127
|
4 |
|
$provider = $subscriber->getProvider(); |
128
|
|
|
|
129
|
4 |
|
if (! $this->project->providerConnected($provider)) { |
130
|
1 |
|
continue; |
131
|
|
|
} |
132
|
|
|
|
133
|
4 |
|
if (! isset($partials[$provider])) { |
134
|
4 |
|
$partials[$provider] = []; |
135
|
4 |
|
} |
136
|
|
|
|
137
|
4 |
|
$partials[$provider][] = $subscriber; |
138
|
|
|
|
139
|
4 |
|
if ($this->payloadStorage) { |
140
|
4 |
|
$this->payloadStorage->assignNotificationToSubscriber( |
141
|
4 |
|
$subscriber, |
142
|
4 |
|
$this->notification, |
143
|
4 |
|
$this->project->config['payload']['subscriber_lifetime'] |
144
|
4 |
|
); |
145
|
4 |
|
} |
146
|
4 |
|
} |
147
|
|
|
|
148
|
4 |
|
return $partials; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Validate data. |
153
|
|
|
* |
154
|
|
|
* @throws \RuntimeException |
155
|
|
|
*/ |
156
|
5 |
|
protected function validate() |
157
|
|
|
{ |
158
|
5 |
|
if (is_null($this->notification)) { |
159
|
1 |
|
throw new \RuntimeException("Notification wasn't set."); |
160
|
|
|
} |
161
|
|
|
|
162
|
4 |
|
if (is_null($this->subscribers)) { |
163
|
1 |
|
throw new \RuntimeException('No subscribers set.'); |
164
|
|
|
} |
165
|
3 |
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Set project manually. |
169
|
|
|
* |
170
|
|
|
* @param Project $project |
171
|
|
|
* @return $this |
172
|
|
|
*/ |
173
|
1 |
|
public function setProject(Project $project) |
174
|
|
|
{ |
175
|
1 |
|
$this->project = $project; |
176
|
|
|
|
177
|
1 |
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Get project instance. |
182
|
|
|
* |
183
|
|
|
* @return Project |
184
|
|
|
*/ |
185
|
1 |
|
public function getProject() |
186
|
|
|
{ |
187
|
1 |
|
return $this->project; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Boot event listeners. |
192
|
|
|
*/ |
193
|
8 |
|
protected function bootListeners() |
194
|
|
|
{ |
195
|
8 |
|
if (! is_null($this->statisticsStorage)) { |
196
|
8 |
|
static::$events->useListenerProvider($this->statisticsStorage); |
197
|
8 |
|
} |
198
|
8 |
|
} |
199
|
|
|
} |
200
|
|
|
|