|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ActionM\WebMoneyMerchant; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Events\Dispatcher; |
|
6
|
|
|
use ActionM\WebMoneyMerchant\Events\WebMoneyMerchantEvent; |
|
7
|
|
|
use ActionM\WebMoneyMerchant\Exceptions\InvalidConfiguration; |
|
8
|
|
|
|
|
9
|
|
|
class WebMoneyMerchantNotifier |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* register Notifier. |
|
13
|
|
|
*/ |
|
14
|
|
|
public function subscribe(Dispatcher $events) |
|
15
|
|
|
{ |
|
16
|
|
|
// Listen events and send notification |
|
17
|
|
|
$events->listen(WebMoneyMerchantEvent::class, function ($event) { |
|
18
|
|
|
$event->type = str_replace('webmoneymerchant.', '', $event->type); |
|
19
|
|
|
|
|
20
|
|
|
if (! in_array($event->type, ['info', 'success', 'error'])) { |
|
21
|
|
|
$event->type = 'error'; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
$notifiable = app(config('webmoney-merchant.notifiable')); |
|
25
|
|
|
|
|
26
|
|
|
$notification = app(config('webmoney-merchant.notification')); |
|
27
|
|
|
$notification->setEvent($event); |
|
28
|
|
|
|
|
29
|
|
|
if (! $this->isValidNotificationClass($notification)) { |
|
30
|
|
|
throw InvalidConfiguration::notificationClassInvalid(get_class($notification)); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
if ($this->shouldSendNotification($notification)) { |
|
34
|
|
|
$notifiable->notify($notification); |
|
35
|
|
|
} |
|
36
|
|
|
}); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function isValidNotificationClass($notification) |
|
40
|
|
|
{ |
|
41
|
|
|
if (get_class($notification) === WebMoneyMerchantNotification::class) { |
|
42
|
|
|
return true; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
if (is_subclass_of($notification, WebMoneyMerchantNotification::class)) { |
|
|
|
|
|
|
46
|
|
|
return true; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return false; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function shouldSendNotification($notification) |
|
53
|
|
|
{ |
|
54
|
|
|
$callable = config('webmoney-merchant.notificationFilter'); |
|
55
|
|
|
|
|
56
|
|
|
if (! is_callable($callable)) { |
|
57
|
|
|
return true; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $callable($notification); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|