Passed
Push — master ( f1f97e...65b71b )
by Anton
02:35
created

NotifiableTrait::viaChannels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * @copyright Anton Tuyakhov <[email protected]>
4
 */
5
6
namespace tuyakhov\notifications;
7
8
9
use yii\helpers\Inflector;
10
11
trait NotifiableTrait
12
{
13
    /**
14
     * Determines if notifiable entity should receive the notification by checking in notification settings.
15
     * @param NotificationInterface $notification
16
     * @return bool
17
     */
18
    public function shouldReceiveNotification(NotificationInterface $notification)
19
    {
20
        $notificationAlias = Inflector::camel2id(get_class($notification));
21
        if (isset($this->notificationSettings[$notificationAlias])) {
22
            return (bool) $this->notificationSettings[$notificationAlias];
0 ignored issues
show
Bug introduced by
The property notificationSettings does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
23
        }
24
        return true;
25
    }
26
27
    /**
28
     * Send notifications via email by default
29
     * @return array
30
     */
31
    public function viaChannels()
32
    {
33
        return ['mail'];
34
    }
35
36
    /**
37
     * Return the notification routing information for the given channel.
38
     * ```php
39
     * public function routeNotificationForMail() {
40
     *      return $this->email;
41
     * }
42
     * ```
43
     * @param $channel string
44
     * @return mixed
45
     */
46
    public function routeNotificationFor($channel)
47
    {
48
        if (method_exists($this, $method = 'routeNotificationFor'.Inflector::id2camel($channel))) {
49
            return $this->{$method}();
50
        }
51
        switch ($channel) {
52
            case 'mail':
53
                return $this->email;
0 ignored issues
show
Bug introduced by
The property email does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
54
            case 'nexmo':
55
                return $this->phone_number;
0 ignored issues
show
Bug introduced by
The property phone_number does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Coding Style introduced by
Variable "phone_number" is not in valid camel caps format
Loading history...
56
        }
57
    }
58
}