1
|
|
|
<?php namespace nyx\notify\traits; |
2
|
|
|
|
3
|
|
|
// External dependencies |
4
|
|
|
use nyx\utils\str; |
5
|
|
|
|
6
|
|
|
// Internal dependencies |
7
|
|
|
use nyx\notify\interfaces; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Notifiable Trait |
11
|
|
|
* |
12
|
|
|
* Allows for the implementation of the Notifiable Interface - @see \nyx\notify\interfaces\Notifiable. |
13
|
|
|
* |
14
|
|
|
* Important note: Currently relies on Laravel's app() helper method being available and this dependency is not covered |
15
|
|
|
* by Composer's dependencies. The trait itself is not used anywhere in the Notify component but if you decide to use |
16
|
|
|
* it in your own Notifiable, the assumption (currently, subject to change) is that it is in a typical, full Laravel |
17
|
|
|
* installation (@todo). |
18
|
|
|
* |
19
|
|
|
* @package Nyx\Notify |
20
|
|
|
* @version 0.1.0 |
21
|
|
|
* @author Michal Chojnacki <[email protected]> |
22
|
|
|
* @copyright 2012-2017 Nyx Dev Team |
23
|
|
|
* @link https://github.com/unyx/nyx |
24
|
|
|
*/ |
25
|
|
|
trait Notifiable |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @see \nyx\notify\interfaces\Notifiable::notify() |
29
|
|
|
*/ |
30
|
|
|
public function notify(interfaces\Notification $notification) |
31
|
|
|
{ |
32
|
|
|
app(interfaces\Dispatcher::class)->send([$this], $notification); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @see \nyx\notify\interfaces\Notifiable::routeNotification() |
37
|
|
|
* |
38
|
|
|
* Note: This is a catch-all method. You can either override it or provide getNotificationRouteFor* methods |
39
|
|
|
* for each transport supported by the entity. For example, a method returning data for the Slack |
40
|
|
|
* transport would be called: "routeNotificationForSlack()" |
41
|
|
|
* |
42
|
|
|
* For any transport that is not supported, or if you simply want to circumvent notifying an entity |
43
|
|
|
* under certain conditions, return false and/or simply do not implement methods for those transports. |
44
|
|
|
*/ |
45
|
|
|
public function routeNotification(string $transport, $message) |
46
|
|
|
{ |
47
|
|
|
if (method_exists($this, $method = 'routeNotificationFor'.str\Cases::studly($transport))) { |
48
|
|
|
return $this->{$method}($message); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|