WebMoneyMerchantNotifier::subscribe()   B
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.6845
c 0
b 0
f 0
cc 4
eloc 12
nc 1
nop 1
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)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \ActionM\WebMoneyMerchan...hantNotification::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
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