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

NotificationTrait::onDeleteNotificationRead()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0067

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
ccs 10
cts 11
cp 0.9091
rs 9.4285
cc 3
eloc 10
nc 3
nop 1
crap 3.0067
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