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