Completed
Push — master ( 9abbc7...a687e5 )
by Fabrizio
03:03
created

Dispatcher::fire()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 3 Features 1
Metric Value
c 4
b 3
f 1
dl 0
loc 26
rs 8.8571
cc 2
eloc 8
nc 2
nop 4
1
<?php
2
3
namespace Fenos\Notifynder\Handler;
4
5
use Fenos\Notifynder\Contracts\NotifynderDispatcher;
6
use Fenos\Notifynder\Notifynder;
7
use Illuminate\Contracts\Events\Dispatcher as LaravelDispatcher;
8
9
/**
10
 * Class Dispatcher.
11
 */
12
class Dispatcher implements NotifynderDispatcher
13
{
14
    /**
15
     * @var \Illuminate\Events\Dispatcher
16
     */
17
    protected $event;
18
19
    /**
20
     * Default namespace for notifications events.
21
     *
22
     * @var string
23
     */
24
    public static $defaultWildcard = 'Notifynder';
25
26
    /**
27
     * Default sender method.
28
     *
29
     * @var string
30
     */
31
    protected $sender = 'send';
32
33
    /**
34
     * @param LaravelDispatcher $event
35
     */
36
    public function __construct(LaravelDispatcher $event)
37
    {
38
        $this->event = $event;
0 ignored issues
show
Documentation Bug introduced by
$event is of type object<Illuminate\Contracts\Events\Dispatcher>, but the property $event was declared to be of type object<Illuminate\Events\Dispatcher>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
39
    }
40
41
    /**
42
     * It fire the event associated to the passed key,
43
     * trigger the listener method bound with.
44
     *
45
     * @param  Notifynder        $notifynder
46
     * @param  string            $eventName
47
     * @param  string            $categoryName
48
     * @param  mixed|null        $values
49
     * @return mixed|null
50
     */
51
    public function fire(Notifynder $notifynder, $eventName, $categoryName = null, $values = [])
52
    {
53
        // Generate the event from the name given
54
        $eventName = $this->generateEventName($eventName);
55
56
        // Instantiate the Notifynder event Object that will provide
57
        // nice way to get your data on the handler. It will be the first
58
        // parameter
59
        $event = new NotifynderEvent($eventName, $categoryName, $values);
60
61
        // Fire the event given expecting an array of notifications or false
62
        // value to not send the notification
63
        $notificationsResult = $this->event->fire($eventName, [$event, $notifynder]);
64
65
        // if the event return an array of notifications then it will
66
        // send automatically
67
        if ($this->hasNotificationToSend($notificationsResult)) {
68
69
            // Send the notification with the sender
70
            // method specified in the property
71
            return call_user_func_array(
72
                [$notifynder, $this->sender],
73
                [$notificationsResult[0]]
74
            );
75
        }
76
    }
77
78
    /**
79
     * Delegate events to categories.
80
     *
81
     * @param  Notifynder        $notifynder
82
     * @param  array             $data
83
     * @param  array             $events
84
     * @return mixed
85
     */
86
    public function delegate(Notifynder $notifynder, $data, array $events)
87
    {
88
        foreach ($events as $category => $event) {
89
            $this->fire($notifynder, $event, $category, $data);
90
        }
91
    }
92
93
    /**
94
     * Tell the dispatcher to send
95
     * the notification with a custom
96
     * (extended method).
97
     *
98
     * @param $customMethod
99
     * @return $this
100
     */
101
    public function sendWith($customMethod)
102
    {
103
        $this->sender = $customMethod;
104
105
        return $this;
106
    }
107
108
    /**
109
     * Boot The listeners.
110
     *
111
     * @param array $listeners
112
     */
113
    public function boot(array $listeners)
114
    {
115
        if (count($listeners) > 0) {
116
            foreach ($listeners as $key => $listener) {
117
                // Notifynder.name.*
118
                $event = $this->generateEventName($key);
119
                $this->event->listen($event, $listener);
120
            }
121
        }
122
    }
123
124
    /**
125
     * Check if the fired method has some notifications
126
     * to send.
127
     *
128
     * @param $notificationsResult
129
     * @return bool
130
     */
131 View Code Duplication
    public function hasNotificationToSend($notificationsResult)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
    {
133
        return is_array($notificationsResult)
134
                and count($notificationsResult) > 0
135
                and $notificationsResult[0] !== false
136
                and count($notificationsResult[0]) > 0;
137
    }
138
139
    /**
140
     * Get Event name.
141
     *
142
     * @param $eventName
143
     * @return string
144
     */
145
    protected function generateEventName($eventName)
146
    {
147
        return static::$defaultWildcard.'.'.str_replace('@', '.', $eventName);
148
    }
149
}
150