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

NotifiableBehavior::handle()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.2
c 0
b 0
f 0
cc 4
eloc 12
nc 4
nop 1
1
<?php
2
/**
3
 * @copyright Anton Tuyakhov <[email protected]>
4
 */
5
6
namespace tuyakhov\notifications;
7
8
9
use yii\base\Behavior;
10
use yii\base\Event;
11
use yii\di\Instance;
12
13
class NotifiableBehavior extends Behavior
14
{
15
    public $notifications = [];
16
17
    /**
18
     * @var Notifier
19
     */
20
    public $notifier = 'notifier';
21
22
    public function init()
23
    {
24
        parent::init();
25
        $this->notifier = Instance::of($this->notifier);
0 ignored issues
show
Documentation introduced by
$this->notifier is of type object<tuyakhov\notifications\Notifier>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation Bug introduced by
It seems like \yii\di\Instance::of($this->notifier) of type object<yii\di\Instance> is incompatible with the declared type object<tuyakhov\notifications\Notifier> of property $notifier.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
26
    }
27
28
    /**
29
     * @inheritdoc
30
     */
31
    public function attach($owner)
32
    {
33
        $this->owner = $owner;
34 View Code Duplication
        foreach ($this->notifications as $event => $notifications) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
            if (!is_array($notifications)) {
36
                $notifications = [$notifications];
37
            }
38
            foreach ($notifications as $notification) {
39
                $owner->on($event, [$this, 'handle'], ['notification' => $notification]);
40
            }
41
        }
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function detach()
48
    {
49
        if ($this->owner) {
50 View Code Duplication
            foreach ($this->notifications as $event => $notifications) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
                if (!is_array($notifications)) {
52
                    $notifications = [$notifications];
53
                }
54
                foreach ($notifications as $notification) {
55
                    $this->owner->off($event, [$this, 'handle']);
56
                }
57
            }
58
            $this->owner = null;
59
        }
60
    }
61
62
    /**
63
     * Handles the event using public properties.
64
     * @param Event $event
65
     * @throws \InvalidArgumentException
66
     * @throws \RuntimeException
67
     */
68
    public function handle(Event $event)
69
    {
70
        if (!isset($event->data['notification'])) {
71
            throw new \InvalidArgumentException('Can not find `notification` in event data');
72
        }
73
        if (!$this->owner instanceof NotifiableInterface) {
74
            throw new \RuntimeException('Owner should implement `NotifiableInterface`');
75
        }
76
        $notification = $event->data['notification'];
77
        $config = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
78
        foreach (get_object_vars($event) as $param) {
79
            $config[$param] = $event->$param;
80
        }
81
        $config['class'] = $notification;
82
        /**
83
         * @var $notification NotificationInterface
84
         */
85
        $notification = \Yii::createObject($config);
86
        $this->notifier->send($this->owner, $notification);
87
    }
88
89
90
}