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

NotifiableBehavior   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 80
Duplicated Lines 20 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 6
dl 16
loc 80
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 5 1
A attach() 8 12 4
A detach() 8 14 5
A handle() 0 20 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @copyright Anton Tuyakhov <[email protected]>
4
 */
5
6
namespace tuyakhov\notifications\behaviors;
7
8
9
use tuyakhov\notifications\NotifiableInterface;
10
use tuyakhov\notifications\NotificationInterface;
11
use tuyakhov\notifications\Notifier;
12
use yii\base\Behavior;
13
use yii\base\Event;
14
use yii\di\Instance;
15
16
class NotifiableBehavior extends Behavior
17
{
18
    public $notifications = [];
19
20
    /**
21
     * @var Notifier
22
     */
23
    public $notifier = 'notifier';
24
25
    /**
26
     * @throws \yii\base\InvalidConfigException
27
     */
28
    public function init()
29
    {
30
        parent::init();
31
        $this->notifier = Instance::ensure($this->notifier, 'tuyakhov\notifications\Notifier');
32
    }
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public function attach($owner)
38
    {
39
        $this->owner = $owner;
40 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...
41
            if (!is_array($notifications)) {
42
                $notifications = [$notifications];
43
            }
44
            foreach ($notifications as $notification) {
45
                $owner->on($event, [$this, 'handle'], ['notification' => $notification]);
46
            }
47
        }
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function detach()
54
    {
55
        if ($this->owner) {
56 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...
57
                if (!is_array($notifications)) {
58
                    $notifications = [$notifications];
59
                }
60
                foreach ($notifications as $notification) {
61
                    $this->owner->off($event, [$this, 'handle']);
62
                }
63
            }
64
            $this->owner = null;
65
        }
66
    }
67
68
    /**
69
     * Handles the event using public properties.
70
     * @param Event $event
71
     * @throws \yii\base\InvalidConfigException
72
     */
73
    public function handle(Event $event)
74
    {
75
        if (!isset($event->data['notification'])) {
76
            throw new \InvalidArgumentException('Can not find `notification` in event data');
77
        }
78
        if (!$this->owner instanceof NotifiableInterface) {
79
            throw new \RuntimeException('Owner should implement `NotifiableInterface`');
80
        }
81
        $notification = $event->data['notification'];
82
        $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...
83
        foreach (get_object_vars($event) as $param => $value) {
84
            $config[$param] = $value;
85
        }
86
        $config['class'] = $notification;
87
        /**
88
         * @var $notification NotificationInterface
89
         */
90
        $notification = \Yii::createObject($config);
91
        $this->notifier->send($this->owner, $notification);
92
    }
93
94
95
}