1
|
|
|
<?php namespace nyx\notify; |
2
|
|
|
|
3
|
|
|
// External dependencies |
4
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
5
|
|
|
use Illuminate\Contracts\Bus\Dispatcher; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Notification Transport Manager |
9
|
|
|
* |
10
|
|
|
* @package Nyx\Notify |
11
|
|
|
* @version 0.1.0 |
12
|
|
|
* @author Michal Chojnacki <[email protected]> |
13
|
|
|
* @copyright 2012-2017 Nyx Dev Team |
14
|
|
|
* @link https://github.com/unyx/nyx |
15
|
|
|
* @todo Proper eventing (beforeSend, afterSend, failedSend etc.) hooked into Nyx's Events component. |
16
|
|
|
* @todo Add support for core Collections and PHP 7.1 iterables (Notifiable entities). |
17
|
|
|
*/ |
18
|
|
|
class TransportManager extends \Illuminate\Support\Manager implements interfaces\Dispatcher |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritDoc} |
22
|
|
|
*/ |
23
|
|
|
public function send($notifiables, interfaces\Notification $notification) |
24
|
|
|
{ |
25
|
|
|
// In here, as opposed to self::sendNow(), we respect the ShouldQueue interface |
26
|
|
|
// and push the Notification onto the queue if it asks us to. |
27
|
|
|
if ($notification instanceof ShouldQueue) { |
28
|
|
|
$this->enqueue($notifiables, $notification); |
29
|
|
|
return; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$this->sendNow($notifiables, $notification); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritDoc} |
37
|
|
|
*/ |
38
|
|
|
public function sendNow($notifiables, interfaces\Notification $notification) |
39
|
|
|
{ |
40
|
|
|
if (!is_array($notifiables)) { |
41
|
|
|
$notifiables = [$notifiables]; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
foreach ($notifiables as $notifiable) { |
45
|
|
|
|
46
|
|
|
// Iterate over all transports the Notification specifies for the current Notifiable, |
47
|
|
|
// then determine whether it shall be sent and send it. |
48
|
|
|
foreach ($notification->via($notifiable) as $transport) { |
49
|
|
|
|
50
|
|
|
$transport = $this->driver($transport); |
51
|
|
|
|
52
|
|
|
if (!$this->shouldSend($notifiable, $notification, $transport)) { |
53
|
|
|
continue; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// All clear at this point - let's dispatch the Notification. |
57
|
|
|
$transport->send($notifiable, $notification); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritDoc} |
64
|
|
|
*/ |
65
|
|
|
public function getDefaultDriver() : string |
66
|
|
|
{ |
67
|
|
|
return 'mail'; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Determines whether the Notification should be sent at all, given the context. |
72
|
|
|
* |
73
|
|
|
* @param interfaces\Notifiable $notifiable The entity being notified. |
74
|
|
|
* @param interfaces\Notification $notification The Notification being sent. |
75
|
|
|
* @param interfaces\Transport $transport The Transport the Notification should be sent over. |
76
|
|
|
* @return bool True when the Notification should be sent, false otherwise. |
77
|
|
|
* @todo onBeforeSend event allowing listeners to prevent dispatching. |
78
|
|
|
*/ |
79
|
|
|
protected function shouldSend(interfaces\Notifiable $notifiable, interfaces\Notification $notification, interfaces\Transport $transport) : bool |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
if (!$transport->supports($notification)) { |
82
|
|
|
return false; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return true; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Enqueues the given Notification. |
90
|
|
|
* |
91
|
|
|
* @param mixed $notifiables The entities which shall receive the Notification. |
92
|
|
|
* @param interfaces\Notification $notification The Notification to enqueue. |
93
|
|
|
*/ |
94
|
|
|
protected function enqueue($notifiables, interfaces\Notification $notification) |
95
|
|
|
{ |
96
|
|
|
// @todo Laravel's ShouldQueue interface doesn't actually cover access to those properties |
97
|
|
|
// so we'll need a more robust solution later. |
98
|
|
|
$this->app->make(Dispatcher::class)->dispatch( |
99
|
|
|
(new jobs\Enqueue($notifiables, $notification)) |
100
|
|
|
->onConnection($notification->connection) |
|
|
|
|
101
|
|
|
->onQueue($notification->queue) |
|
|
|
|
102
|
|
|
->delay($notification->delay) |
|
|
|
|
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritDoc} |
108
|
|
|
*/ |
109
|
|
|
protected function createDriver($driver) |
110
|
|
|
{ |
111
|
|
|
try { |
112
|
|
|
return parent::createDriver($driver); |
113
|
|
|
} catch (\InvalidArgumentException $exception) { |
114
|
|
|
|
115
|
|
|
// Re-throw if the driver wasn't recognized and isn't a fully-qualified (and existing) class name. |
116
|
|
|
if (!class_exists($driver)) { |
117
|
|
|
throw $exception; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $this->app->make($driver); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Creates a Mail Transport. |
126
|
|
|
* |
127
|
|
|
* @return transports\Mail |
128
|
|
|
*/ |
129
|
|
|
protected function createMailDriver() : transports\Mail |
130
|
|
|
{ |
131
|
|
|
return $this->app->make(transports\Mail::class); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Creates a Slack Transport. |
136
|
|
|
* |
137
|
|
|
* @return transports\Slack |
138
|
|
|
*/ |
139
|
|
|
protected function createSlackDriver() : transports\Slack |
140
|
|
|
{ |
141
|
|
|
return new transports\Slack($this->app->make('config')->get('services.slack'), new \GuzzleHttp\Client); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.