Issues (15)

src/NotifiableTrait.php (2 issues)

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;
0 ignored issues
show
Variable "phone_number" is not in valid camel caps format
Loading history...
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
Deprecated Code introduced by Anton Tuyakhov
The function yii\base\BaseObject::className() has been deprecated: since 2.0.14. On PHP >=5.5, use `::class` instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

72
        return $this->hasMany(/** @scrutinizer ignore-deprecated */ Notification::className(), ['notifiable_id' => 'id'])

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.

Loading history...
73
            ->andOnCondition(['notifiable_type' => get_class($this)]);
74
    }
75
76
    public function getUnreadNotifications()
77
    {
78
        return $this->getNotifications()->andOnCondition(['read_at' => null]);
79
    }
80
}