Test Failed
Pull Request — master (#5)
by Anton
02:48
created

NotifiableTrait::shouldReceiveNotification()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 4
nc 4
nop 1
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;
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...
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;
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...
62
            case 'twilio':
63
                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...
64
            case 'database':
65
                return [get_class($this), $this->id];
0 ignored issues
show
Bug introduced by
The property id 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...
66
        }
67
    }
68
69
    public function getNotifications()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
70
    {
71
        /** @var $this BaseActiveRecord */
72
        return $this->hasMany(Notification::className(), ['notifiable_id' => 'id'])
0 ignored issues
show
Bug introduced by
The method addOnCondition() does not seem to exist on object<yii\db\ActiveQueryInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
73
            ->addOnCondition(['notifiable_type' => get_class($this)]);
74
    }
75
76
    public function getUnreadNotifications()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
77
    {
78
        return $this->getNotifications()->addOnCondition(['read_at' => null]);
79
    }
80
}