1 | <?php |
||
2 | /** |
||
3 | * @copyright Anton Tuyakhov <[email protected]> |
||
4 | */ |
||
5 | |||
6 | namespace tuyakhov\notifications; |
||
7 | |||
8 | |||
9 | use tuyakhov\notifications\models\Notification; |
||
10 | use yii\db\BaseActiveRecord; |
||
11 | use yii\helpers\Inflector; |
||
12 | |||
13 | trait NotifiableTrait |
||
14 | { |
||
15 | /** |
||
16 | * Determines if notifiable entity should receive the notification by checking in notification settings. |
||
17 | * @param NotificationInterface $notification |
||
18 | * @return bool |
||
19 | */ |
||
20 | public function shouldReceiveNotification(NotificationInterface $notification) |
||
21 | { |
||
22 | $alias = Inflector::camel2id(get_class($notification)); |
||
23 | if (isset($this->notificationSettings)) { |
||
24 | $settings = $this->notificationSettings; |
||
25 | if (array_key_exists($alias, $settings)) { |
||
26 | if ($settings[$alias] instanceof \Closure) { |
||
27 | return call_user_func($settings[$alias], $notification); |
||
28 | } |
||
29 | return (bool) $settings[$alias]; |
||
30 | } |
||
31 | } |
||
32 | return true; |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * Send notifications via email by default |
||
37 | * @return array |
||
38 | */ |
||
39 | public function viaChannels() |
||
40 | { |
||
41 | return ['mail']; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Return the notification routing information for the given channel. |
||
46 | * ```php |
||
47 | * public function routeNotificationForMail() { |
||
48 | * return $this->email; |
||
49 | * } |
||
50 | * ``` |
||
51 | * @param $channel string |
||
52 | * @return mixed |
||
53 | */ |
||
54 | public function routeNotificationFor($channel) |
||
55 | { |
||
56 | if (method_exists($this, $method = 'routeNotificationFor'.Inflector::id2camel($channel))) { |
||
57 | return $this->{$method}(); |
||
58 | } |
||
59 | switch ($channel) { |
||
60 | case 'mail': |
||
61 | return $this->email; |
||
62 | case 'twilio': |
||
63 | return $this->phone_number; |
||
64 | case 'database': |
||
65 | return [get_class($this), $this->id]; |
||
66 | } |
||
67 | } |
||
68 | |||
69 | public function getNotifications() |
||
70 | { |
||
71 | /** @var $this BaseActiveRecord */ |
||
72 | return $this->hasMany(Notification::className(), ['notifiable_id' => 'id']) |
||
0 ignored issues
–
show
|
|||
73 | ->andOnCondition(['notifiable_type' => get_class($this)]); |
||
74 | } |
||
75 | |||
76 | public function getUnreadNotifications() |
||
77 | { |
||
78 | return $this->getNotifications()->andOnCondition(['read_at' => null]); |
||
79 | } |
||
80 | } |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.