1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fenos\Notifynder\Handler; |
4
|
|
|
|
5
|
|
|
use Fenos\Notifynder\Contracts\NotifyListener; |
6
|
|
|
use Fenos\Notifynder\Notifynder; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class NotifynderHandler. |
10
|
|
|
*/ |
11
|
|
|
class NotifynderHandler |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Handle the event. |
15
|
|
|
* |
16
|
|
|
* @param NotifyListener $eventListener |
17
|
|
|
* @param null $notifynder |
18
|
|
|
* @return mixed |
19
|
|
|
*/ |
20
|
|
|
public function handle(NotifyListener $eventListener, $notifynder = null) |
21
|
|
|
{ |
22
|
|
|
$event = $eventListener->getNotifynderEvent(); |
23
|
|
|
|
24
|
|
|
$eventName = $this->getEventName($event->getEvent()); |
25
|
|
|
|
26
|
|
|
if ($this->listenerIsRegistered($eventName)) { |
27
|
|
|
|
28
|
|
|
// Make sure a notifynder instance is passed to the event |
29
|
|
|
// invoker method |
30
|
|
|
$notifynder = (is_null($notifynder)) ? $this->getNotifynder() : $notifynder; |
31
|
|
|
|
32
|
|
|
// Build the notifications |
33
|
|
|
$builtNotifications = call_user_func_array([$this, $eventName], [$event, $notifynder]); |
34
|
|
|
|
35
|
|
|
// If the listener is the NotifynderEvent that means |
36
|
|
|
// we are triggering this from the Notifynder::fire() |
37
|
|
|
// Event, it will take care of sending the notification |
38
|
|
|
if ($eventListener instanceof NotifynderEvent) { |
39
|
|
|
return $builtNotifications; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// Event has been dispatched manually from the native |
43
|
|
|
// Laravel eventing system then I'll send the notification |
44
|
|
|
// Right here |
45
|
|
|
if ($this->hasNotificationToSend([$builtNotifications])) { |
46
|
|
|
return $notifynder->send($builtNotifications); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Check if the listener exists on the class |
53
|
|
|
* adding when as convention. |
54
|
|
|
* |
55
|
|
|
* ['postAdd'] whenPostAdd] |
56
|
|
|
* |
57
|
|
|
* @param $eventName |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
protected function listenerIsRegistered($eventName) |
61
|
|
|
{ |
62
|
|
|
return method_exists($this, $eventName); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get Event Name from the key |
67
|
|
|
* it use a convention. |
68
|
|
|
* |
69
|
|
|
* given user.post.add -> postAdd |
70
|
|
|
* given user@postAdd -> postAdd |
71
|
|
|
* |
72
|
|
|
* @param $event |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
protected function getEventName($event) |
76
|
|
|
{ |
77
|
|
|
// Remove the Notifynder namespaces for |
78
|
|
|
// the find the method |
79
|
|
|
$event = str_replace(Dispatcher::$defaultWildcard.'.', '', $event); |
80
|
|
|
|
81
|
|
|
$eventNameSpace = (strpos($event, '@')) |
82
|
|
|
? explode('@', $event) |
83
|
|
|
: explode('.', $event); |
84
|
|
|
|
85
|
|
|
// Check if the name has been splitted in 2 |
86
|
|
|
if (count($eventNameSpace) > 1) { |
87
|
|
|
array_shift($eventNameSpace); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$nameMethod = implode('_', $eventNameSpace); |
91
|
|
|
|
92
|
|
|
return camel_case($nameMethod); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get Notifynder Instance. |
97
|
|
|
* |
98
|
|
|
* @return Notifynder |
99
|
|
|
*/ |
100
|
|
|
protected function getNotifynder() |
101
|
|
|
{ |
102
|
|
|
$notifynder = app('notifynder'); |
103
|
|
|
|
104
|
|
|
return $notifynder; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Check if the fired method has some notifications |
109
|
|
|
* to send. |
110
|
|
|
* |
111
|
|
|
* @param $notificationsResult |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
|
View Code Duplication |
protected function hasNotificationToSend($notificationsResult) |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
return is_array($notificationsResult) |
117
|
|
|
and count($notificationsResult) > 0 |
118
|
|
|
and $notificationsResult[0] !== false |
119
|
|
|
and count($notificationsResult[0]) > 0; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
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.