|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Notifier; |
|
4
|
|
|
|
|
5
|
|
|
use App\Facades\Log; |
|
6
|
|
|
use Ronanchilvers\Foundation\Config; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Notifier manager class |
|
10
|
|
|
* |
|
11
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class Manager |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $adaptors = []; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Register a notifier with the manager |
|
22
|
|
|
* |
|
23
|
|
|
* @param \App\Notifier\AdaptorInterface |
|
24
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
public function registerAdaptor(AdaptorInterface $adaptor) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->adaptors[get_class($adaptor)] = $adaptor; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Send a notification through all adaptors |
|
33
|
|
|
* |
|
34
|
|
|
* @param \App\Notifier\Notification $notification |
|
35
|
|
|
* @param array $options An array of options for all notifiers, keyed by notifier key |
|
36
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
37
|
|
|
*/ |
|
38
|
|
|
public function sendNotification(Notification $notification, array $options) |
|
39
|
|
|
{ |
|
40
|
|
|
Log::debug(sprintf('Sending notification to %d adaptors', count($this->adaptors)), [ |
|
41
|
|
|
'message' => $notification->getMessage(), |
|
42
|
|
|
]); |
|
43
|
|
|
foreach ($this->adaptors as $adaptor) { |
|
44
|
|
|
Log::debug(sprintf('Using %s adaptor', get_class($adaptor))); |
|
45
|
|
|
$key = $adaptor->getKey(); |
|
46
|
|
|
if (!isset($options[$key])) { |
|
47
|
|
|
continue; |
|
48
|
|
|
} |
|
49
|
|
|
$adaptorOptions = $options[$key]; |
|
50
|
|
|
$adaptor->send($notification, $adaptorOptions); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Send a string message |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $message |
|
58
|
|
|
* @param array $options An array of options for all notifiers, keyed by notifier key |
|
59
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
60
|
|
|
*/ |
|
61
|
|
|
public function send($message, array $options) |
|
62
|
|
|
{ |
|
63
|
|
|
$notification = new Notification($message); |
|
64
|
|
|
$this->sendNotification($notification, $options); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|