|
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; |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
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
|
|
|
|
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.