Completed
Push — master ( 8e3c70...7c4bfd )
by vistart
07:52 queued 01:35
created

NotificationTrait   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 58.97%

Importance

Changes 9
Bugs 1 Features 1
Metric Value
wmc 13
c 9
b 1
f 1
lcom 1
cbo 2
dl 0
loc 77
ccs 23
cts 39
cp 0.5897
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getLink() 0 8 2
A setLink() 0 7 2
A getNotificationRules() 0 11 2
A enabledFields() 0 11 3
A rules() 0 4 1
A onDeleteNotificationRead() 0 15 3
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.name/
9
 * @copyright Copyright (c) 2016 vistart
10
 * @license https://vistart.name/license/
11
 */
12
13
namespace vistart\Models\traits;
14
15
use yii\base\ModelEvent;
16
17
/**
18
 * This trait is used for building notification model.
19
 *
20
 * @version 2.0
21
 * @author vistart <[email protected]>
22
 */
23
trait NotificationTrait
24
{
25
    use NotificationRangeTrait;
26
27
    public $notificationReadClass;
28
    public $linkAttribute = false;
29
30
    public function getLink()
31
    {
32
        $linkAttribute = $this->linkAttribute;
33
        if (is_string($linkAttribute)) {
34
            return $this->$linkAttribute;
35
        }
36
        return null;
37
    }
38
39
    public function setLink($link)
40
    {
41
        $linkAttribute = $this->linkAttribute;
42
        if (is_string($linkAttribute)) {
43
            return $this->$linkAttribute = $link;
44
        }
45
    }
46
47 4
    public function getNotificationRules()
48
    {
49 4
        $rules = $this->getNotificationRangeRules();
50
51 4
        if (is_string($this->linkAttribute)) {
52
            $rules[] = [
53
                $this->linkAttribute, 'string',
54
            ];
55
        }
56 4
        return $rules;
57
    }
58
59 3
    public function enabledFields()
60
    {
61 3
        $fields = parent::enabledFields();
62 3
        if (is_string($this->linkAttribute)) {
63
            $fields[] = $this->linkAttribute;
64
        }
65 3
        if (is_string($this->rangeAttribute)) {
66 3
            $fields[] = $this->rangeAttribute;
67 3
        }
68 3
        return $fields;
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74 4
    public function rules()
75
    {
76 4
        return array_merge(parent::rules(), $this->getNotificationRules());
77
    }
78
79
    /**
80
     * 
81
     * @param ModelEvent $event
82
     * @return integer|false
83
     */
84 4
    public function onDeleteNotificationRead($event)
85
    {
86 4
        $sender = $event->sender;
87
        /* @var $sender static */
88 4
        if (!is_string($sender->notificationReadClass)) {
89
            return false;
90
        }
91 4
        $notificationReadClass = $sender->notificationReadClass;
92 4
        $notificationReadModels = $notificationReadClass::find()->content($sender->guid)->all();
0 ignored issues
show
Bug introduced by
The property guid 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...
93 4
        $count = 0;
94 4
        foreach ($notificationReadModels as $model) {
95 1
            $count += $model->delete();
96 4
        }
97 4
        return $count;
98
    }
99
}
100