Completed
Push — master ( 242dd2...e210e8 )
by vistart
05:37
created

MessageTrait   B

Complexity

Total Complexity 46

Size/Duplication

Total Lines 212
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 90.68%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 46
c 5
b 0
f 0
lcom 1
cbo 2
dl 0
loc 212
ccs 107
cts 118
cp 0.9068
rs 8.3999

18 Methods

Rating   Name   Duplication   Size   Complexity  
A hasBeenReceived() 0 4 2
A hasBeenRead() 0 4 2
A touchReceived() 0 4 1
A touchRead() 0 4 1
A setReceivedAt() 0 8 2
A setReadAt() 0 8 2
A onInitReceivedAtAttribute() 0 6 1
A onInitReadAtAttribute() 0 6 1
A getReceivedAt() 0 8 2
A getReadAt() 0 8 2
B onReadAtChanged() 0 17 7
B onReceivedAtChanged() 0 13 5
A onContentChanged() 0 12 3
B onMessageUpdated() 0 12 5
A initMessageEvents() 0 9 1
A getMessageRules() 0 17 4
A rules() 0 4 1
A enabledFields() 0 15 4

How to fix   Complexity   

Complex Class

Complex classes like MessageTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use MessageTrait, and based on these observations, apply Extract Interface, too.

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
/**
16
 * This trait should be used in models extended from models used BlameableTrait.
17
 * Notice: The models used BlameableTrait are also models used EntityTrait.
18
 *
19
 * @author vistart <[email protected]>
20
 */
21
trait MessageTrait
22
{
23
24
    public $otherGuidAttribute = 'other_guid';
25
    public $attachmentAttribute = 'attachment';
26
    public $expiration = 604800; // 7 days
27
    public $receivedAtAttribute = 'received_at';
28
    public $readAtAttribute = 'read_at';
29
    public static $eventMessageReceived = 'messageReceived';
30
    public static $eventMessageRead = 'messageRead';
31
    public $permitChangeContent = false;
32
    public $permitChangeReceivedAt = false;
33
34 4
    public function hasBeenReceived()
35
    {
36 4
        return is_string($this->receivedAtAttribute) ? !$this->isInitDatetime($this->receivedAt) : false;
0 ignored issues
show
Bug introduced by
The property receivedAt does not seem to exist. Did you mean receivedAtAttribute?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
It seems like isInitDatetime() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
37
    }
38
39 4
    public function hasBeenRead()
40
    {
41 4
        return is_string($this->readAtAttribute) ? !$this->isInitDatetime($this->readAt) : false;
0 ignored issues
show
Bug introduced by
The property readAt does not seem to exist. Did you mean readAtAttribute?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
It seems like isInitDatetime() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
42
    }
43
44 2
    public function touchReceived()
45
    {
46 2
        return $this->setReceivedAt(static::currentDatetime());
47
    }
48
49 2
    public function touchRead()
50
    {
51 2
        return $this->setReadAt(static::currentDatetime());
52
    }
53
54 4
    public function getReceivedAt()
55
    {
56 4
        if (is_string($this->receivedAtAttribute)) {
57 4
            $raAttribute = $this->receivedAtAttribute;
58 4
            return $this->$raAttribute;
59
        }
60
        return null;
61
    }
62
63 6
    public function setReceivedAt($receivedAt)
64
    {
65 6
        if (is_string($this->receivedAtAttribute)) {
66 6
            $raAttribute = $this->receivedAtAttribute;
67 6
            return $this->$raAttribute = $receivedAt;
68
        }
69
        return null;
70
    }
71
72 4
    public function getReadAt()
73
    {
74 4
        if (is_string($this->readAtAttribute)) {
75 4
            $raAttribute = $this->readAtAttribute;
76 4
            return $this->$raAttribute;
77
        }
78
        return null;
79
    }
80
81 6
    public function setReadAt($readAt)
82
    {
83 6
        if (is_string($this->readAtAttribute)) {
84 6
            $raAttribute = $this->readAtAttribute;
85 6
            return $this->$raAttribute = $readAt;
86
        }
87
        return null;
88
    }
89
90
    /**
91
     * @param \yii\base\ModelEvent $event
92
     */
93 6
    public function onInitReceivedAtAttribute($event)
94
    {
95 6
        $sender = $event->sender;
96
        /* @var $sender static */
97 6
        $sender->setReceivedAt(static::getInitDatetime($event));
98 6
    }
99
100
    /**
101
     * @param \yii\base\ModelEvent $event
102
     */
103 6
    public function onInitReadAtAttribute($event)
104
    {
105 6
        $sender = $event->sender;
106
        /* @var $sender static */
107 6
        $sender->setReadAt(static::getInitDatetime($event));
108 6
    }
109
110
    /**
111
     * We consider you have received the message if you read it.
112
     * @param \yii\base\ModelEvent $event
113
     */
114 4
    public function onReadAtChanged($event)
115
    {
116 4
        $sender = $event->sender;
117 4
        if (!is_string($sender->readAtAttribute)) {
118
            return;
119
        }
120 4
        $raAttribute = $sender->readAtAttribute;
121 4
        $reaAttribute = $sender->receivedAtAttribute;
122 4
        if ($sender->$raAttribute != $sender->initDatetime() && $sender->$reaAttribute == $sender->initDatetime()) {
123 2
            $sender->$reaAttribute = $sender->currentDatetime();
124 2
        }
125 4
        $oldRa = $sender->getOldAttribute($raAttribute);
126 4
        if ($oldRa != null && $oldRa != $sender->initDatetime() && $sender->$raAttribute != $oldRa) {
127
            $sender->$raAttribute = $oldRa;
128
            return;
129
        }
130 4
    }
131
132
    /**
133
     * You are not allowed to change receive time if you have received it.
134
     * @param \yii\base\ModelEvent $event
135
     */
136 4
    public function onReceivedAtChanged($event)
137
    {
138 4
        $sender = $event->sender;
139 4
        if ($sender->permitChangeReceivedAt) {
140
            return;
141
        }
142 4
        $raAttribute = $sender->receivedAtAttribute;
143 4
        $oldRa = $sender->getOldAttribute($raAttribute);
144 4
        if ($oldRa != null && $oldRa != $sender->initDatetime() && $sender->$raAttribute != $oldRa) {
145
            $sender->$raAttribute = $oldRa;
146
            return;
147
        }
148 4
    }
149
150
    /**
151
     * You are not allowed to change the content if it is not new message.
152
     * @param \yii\base\ModelEvent $event
153
     */
154 4
    public function onContentChanged($event)
155
    {
156 4
        $sender = $event->sender;
157 4
        if ($sender->permitChangeContent) {
158
            return;
159
        }
160 4
        $cAttribute = $sender->contentAttribute;
161 4
        $oldContent = $sender->getOldAttribute($cAttribute);
162 4
        if ($oldContent != $sender->$cAttribute) {
163 2
            $sender->$cAttribute = $oldContent;
164 2
        }
165 4
    }
166
167
    /**
168
     * Trigger message received or read events.
169
     * @param \yii\db\AfterSaveEvent $event
170
     */
171 4
    public function onMessageUpdated($event)
172
    {
173 4
        $sender = $event->sender;
174 4
        $reaAttribute = $sender->receivedAtAttribute;
175 4
        if (isset($event->changedAttributes[$reaAttribute]) && $event->changedAttributes[$reaAttribute] != $sender->$reaAttribute) {
176 4
            $sender->trigger(static::$eventMessageReceived);
177 4
        }
178 4
        $raAttribute = $sender->readAtAttribute;
179 4
        if (isset($event->changedAttributes[$raAttribute]) && $event->changedAttributes[$raAttribute] != $sender->$raAttribute) {
180 2
            $sender->trigger(static::$eventMessageRead);
181 2
        }
182 4
    }
183
184 6
    public function initMessageEvents()
185
    {
186 6
        $this->on(static::EVENT_BEFORE_INSERT, [$this, 'onInitReceivedAtAttribute']);
0 ignored issues
show
Bug introduced by
It seems like on() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
187 6
        $this->on(static::EVENT_BEFORE_INSERT, [$this, 'onInitReadAtAttribute']);
0 ignored issues
show
Bug introduced by
It seems like on() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
188 6
        $this->on(static::EVENT_BEFORE_UPDATE, [$this, 'onReceivedAtChanged']);
0 ignored issues
show
Bug introduced by
It seems like on() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
189 6
        $this->on(static::EVENT_BEFORE_UPDATE, [$this, 'onReadAtChanged']);
0 ignored issues
show
Bug introduced by
It seems like on() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
190 6
        $this->on(static::EVENT_BEFORE_UPDATE, [$this, 'onContentChanged']);
0 ignored issues
show
Bug introduced by
It seems like on() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
191 6
        $this->on(static::EVENT_AFTER_UPDATE, [$this, 'onMessageUpdated']);
0 ignored issues
show
Bug introduced by
It seems like on() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
192 6
    }
193
194 6
    public function getMessageRules()
195
    {
196
        $rules = [
197 6
            [$this->otherGuidAttribute, 'required'],
198 6
            [$this->otherGuidAttribute, 'string', 'max' => 36],
199 6
        ];
200 6
        if (is_string($this->attachmentAttribute)) {
201 6
            $rules[] = [$this->attachmentAttribute, 'safe'];
202 6
        }
203 6
        if (is_string($this->receivedAtAttribute)) {
204 6
            $rules[] = [$this->receivedAtAttribute, 'safe'];
205 6
        }
206 6
        if (is_string($this->readAtAttribute)) {
207 6
            $rules[] = [$this->readAtAttribute, 'safe'];
208 6
        }
209 6
        return $rules;
210
    }
211
212 6
    public function rules()
213
    {
214 6
        return array_merge(parent::rules(), $this->getMessageRules());
215
    }
216
217 6
    public function enabledFields()
218
    {
219 6
        $fields = parent::enabledFields();
220 6
        $fields[] = $this->otherGuidAttribute;
221 6
        if (is_string($this->attachmentAttribute)) {
222 6
            $fields[] = $this->attachmentAttribute;
223 6
        }
224 6
        if (is_string($this->receivedAtAttribute)) {
225 6
            $fields[] = $this->receivedAtAttribute;
226 6
        }
227 6
        if (is_string($this->readAtAttribute)) {
228 6
            $fields[] = $this->readAtAttribute;
229 6
        }
230 6
        return $fields;
231
    }
232
}
233