MessageTrait   C
last analyzed

Complexity

Total Complexity 63

Size/Duplication

Total Lines 251
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 90%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 63
c 1
b 0
f 1
lcom 1
cbo 3
dl 0
loc 251
ccs 99
cts 110
cp 0.9
rs 5.8893

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 getReceivedAt() 0 8 3
A setReceivedAt() 0 8 3
A getReadAt() 0 8 3
A setReadAt() 0 8 3
A onInitReceivedAtAttribute() 0 6 1
A onInitReadAtAttribute() 0 6 1
C onReadAtChanged() 0 21 10
B onReceivedAtChanged() 0 16 7
A onContentChanged() 0 15 3
B onMessageUpdated() 0 13 5
A initMessageEvents() 0 9 1
B getMessageRules() 0 15 7
A rules() 0 4 1
B enabledFields() 0 17 9

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.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\base\models\traits;
14
15
use yii\base\ModelEvent;
16
use yii\db\AfterSaveEvent;
17
18
/**
19
 * This trait should be used in models extended from models used BlameableTrait.
20
 * Notice: The models used [[BlameableTrait]] are also models used [[EntityTrait]].
21
 *
22
 * @property-read array $messageRules
23
 * @property mixed $readAt
24
 * @property mixed $receivedAt
25
 * @version 1.0
26
 * @author vistart <[email protected]>
27
 */
28
trait MessageTrait
29
{
30
    use MutualTrait;
31
    
32
    public $attachmentAttribute = 'attachment';
33
    public $receivedAtAttribute = 'received_at';
34
    public $readAtAttribute = 'read_at';
35
    public static $eventMessageReceived = 'messageReceived';
36
    public static $eventMessageRead = 'messageRead';
37
    public $permitChangeContent = false;
38
    public $permitChangeReceivedAt = false;
39
    public $permitChangeReadAt = false;
40
    
41
    /**
42
     * Whether the message has been received.
43
     * Note: This trait should be used for models which use [[TimestampTrait]].
44
     * @return boolean
45
     */
46 4
    public function hasBeenReceived()
47
    {
48 4
        return is_string($this->receivedAtAttribute) ? !$this->isInitDatetime($this->getReceivedAt()) : false;
0 ignored issues
show
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...
49
    }
50
    
51
    /**
52
     * Whether the message has been read.
53
     * If a message has been read, it must have been received.
54
     * Note: This trait should be used for models which use [[TimestampTrait]].
55
     * @return boolean
56
     */
57 4
    public function hasBeenRead()
58
    {
59 4
        return is_string($this->readAtAttribute) ? !$this->isInitDatetime($this->getReadAt()) : false;
0 ignored issues
show
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...
60
    }
61
    
62 2
    public function touchReceived()
63
    {
64 2
        return $this->setReceivedAt(static::currentDatetime());
65
    }
66
    
67 2
    public function touchRead()
68
    {
69 2
        return $this->setReadAt(static::currentDatetime());
70
    }
71
    
72 4
    public function getReceivedAt()
73
    {
74 4
        if (is_string($this->receivedAtAttribute) && !empty($this->receivedAtAttribute)) {
75 4
            $raAttribute = $this->receivedAtAttribute;
76 4
            return $this->$raAttribute;
77
        }
78
        return null;
79
    }
80
    
81 20
    public function setReceivedAt($receivedAt)
82
    {
83 20
        if (is_string($this->receivedAtAttribute) && !empty($this->receivedAtAttribute)) {
84 20
            $raAttribute = $this->receivedAtAttribute;
85 20
            return $this->$raAttribute = $receivedAt;
86
        }
87
        return null;
88
    }
89
    
90 4
    public function getReadAt()
91
    {
92 4
        if (is_string($this->readAtAttribute) && !empty($this->readAtAttribute)) {
93 4
            $raAttribute = $this->readAtAttribute;
94 4
            return $this->$raAttribute;
95
        }
96
        return null;
97
    }
98
    
99 20
    public function setReadAt($readAt)
100
    {
101 20
        if (is_string($this->readAtAttribute) && !empty($this->readAtAttribute)) {
102 20
            $raAttribute = $this->readAtAttribute;
103 20
            return $this->$raAttribute = $readAt;
104
        }
105
        return null;
106
    }
107
    
108
    /**
109
     *
110
     * @param ModelEvent $event
111
     */
112 20
    public function onInitReceivedAtAttribute($event)
113
    {
114 20
        $sender = $event->sender;
115
        /* @var $sender static */
116 20
        $sender->setReceivedAt(static::getInitDatetime($event));
117 20
    }
118
    
119
    /**
120
     *
121
     * @param ModelEvent $event
122
     */
123 20
    public function onInitReadAtAttribute($event)
124
    {
125 20
        $sender = $event->sender;
126
        /* @var $sender static */
127 20
        $sender->setReadAt(static::getInitDatetime($event));
128 20
    }
129
    
130
    /**
131
     * We consider you have received the message if you read it.
132
     * @param ModelEvent $event
133
     */
134 4
    public function onReadAtChanged($event)
135
    {
136 4
        $sender = $event->sender;
137
        /* @var $sender static */
138 4
        $raAttribute = $sender->readAtAttribute;
139 4
        if (!is_string($raAttribute) || empty($raAttribute)) {
140
            return;
141
        }
142 4
        $reaAttribute = $sender->receivedAtAttribute;
143 4
        if (is_string($reaAttribute) && !$sender->isInitDatetime($sender->$raAttribute) && $sender->isInitDatetime($sender->$reaAttribute)) {
0 ignored issues
show
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...
144 2
            $sender->$reaAttribute = $sender->currentDatetime();
0 ignored issues
show
Bug introduced by
It seems like currentDatetime() 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...
145
        }
146
        // If it is permitted to change read time, it will return directly.
147 4
        if ($sender->permitChangeReadAt) {
148
            return;
149
        }
150 4
        $oldRa = $sender->getOldAttribute($raAttribute);
0 ignored issues
show
Bug introduced by
It seems like getOldAttribute() 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...
151 4
        if ($oldRa != null && !$sender->isInitDatetime($oldRa) && $sender->$raAttribute != $oldRa) {
0 ignored issues
show
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...
152
            $sender->$raAttribute = $oldRa;
153
        }
154 4
    }
155
    
156
    /**
157
     * You are not allowed to change receiving time if you have received it.
158
     * @param ModelEvent $event
159
     */
160 4
    public function onReceivedAtChanged($event)
161
    {
162 4
        $sender = $event->sender;
163 4
        $raAttribute = $sender->receivedAtAttribute;
164 4
        if (!is_string($raAttribute) || empty($raAttribute)) {
165
            return;
166
        }
167
        // If it is permitted to change receiving time, then it will return directly.
168 4
        if ($sender->permitChangeReceivedAt) {
169
            return;
170
        }
171 4
        $oldRa = $sender->getOldAttribute($raAttribute);
172 4
        if ($oldRa != null && !$sender->isInitDatetime($oldRa) && $sender->$raAttribute != $oldRa) {
173
            $sender->$raAttribute = $oldRa;
174
        }
175 4
    }
176
    
177
    /**
178
     * You are not allowed to change the content if it is not new message.
179
     * @param ModelEvent $event
180
     */
181 4
    public function onContentChanged($event)
182
    {
183 4
        $sender = $event->sender;
184
        // If it is permitted to change content, then it will return directly.
185 4
        if ($sender->permitChangeContent) {
186
            return;
187
        }
188
        // The messgage will be reversed if it changed (current message isn't
189
        // same as the old).
190 4
        $cAttribute = $sender->contentAttribute;
191 4
        $oldContent = $sender->getOldAttribute($cAttribute);
192 4
        if ($oldContent != $sender->$cAttribute) {
193 2
            $sender->$cAttribute = $oldContent;
194
        }
195 4
    }
196
    
197
    /**
198
     *
199
     * @param AfterSaveEvent $event
200
     */
201 4
    public function onMessageUpdated($event)
202
    {
203 4
        $sender = $event->sender;
204
        /* @var $sender static */
205 4
        $reaAttribute = $sender->receivedAtAttribute;
206 4
        if (isset($event->changedAttributes[$reaAttribute]) && $event->changedAttributes[$reaAttribute] != $sender->$reaAttribute) {
207 4
            $sender->trigger(static::$eventMessageReceived);
0 ignored issues
show
Bug introduced by
It seems like trigger() 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...
208
        }
209 4
        $raAttribute = $sender->readAtAttribute;
210 4
        if (isset($event->changedAttributes[$raAttribute]) && $event->changedAttributes[$raAttribute] != $sender->$raAttribute) {
211 2
            $sender->trigger(static::$eventMessageRead);
0 ignored issues
show
Bug introduced by
It seems like trigger() 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...
212
        }
213 4
    }
214
    
215
    /**
216
     *
217
     */
218 20
    public function initMessageEvents()
219
    {
220 20
        $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...
221 20
        $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...
222 20
        $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...
223 20
        $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...
224 20
        $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...
225 20
        $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...
226 20
    }
227
    
228
    /**
229
     * Return rules associated with message.
230
     * @return array
231
     */
232 20
    public function getMessageRules()
233
    {
234 20
        $rules = [];
235 20
        $rules = array_merge($rules, $this->getMutualRules());
236 20
        if (is_string($this->attachmentAttribute) && !empty($this->attachmentAttribute)) {
237 20
            $rules[] = [$this->attachmentAttribute, 'safe'];
238
        }
239 20
        if (is_string($this->receivedAtAttribute) && !empty($this->receivedAtAttribute)) {
240 20
            $rules[] = [$this->receivedAtAttribute, 'safe'];
241
        }
242 20
        if (is_string($this->readAtAttribute) && !empty($this->readAtAttribute)) {
243 20
            $rules[] = [$this->readAtAttribute, 'safe'];
244
        }
245 20
        return $rules;
246
    }
247
    
248
    /**
249
     * @inheritdoc
250
     * @return array
251
     */
252 20
    public function rules()
253
    {
254 20
        return array_merge(parent::rules(), $this->getMessageRules());
255
    }
256
    
257
    /**
258
     * @inheritdoc
259
     * @return array
260
     */
261 20
    public function enabledFields()
262
    {
263 20
        $fields = parent::enabledFields();
264 20
        if (is_string($this->otherGuidAttribute) && !empty($this->otherGuidAttribute)) {
265 20
            $fields[] = $this->otherGuidAttribute;
266
        }
267 20
        if (is_string($this->attachmentAttribute) && !empty($this->attachmentAttribute)) {
268 20
            $fields[] = $this->attachmentAttribute;
269
        }
270 20
        if (is_string($this->receivedAtAttribute) && !empty($this->receivedAtAttribute)) {
271 20
            $fields[] = $this->receivedAtAttribute;
272
        }
273 20
        if (is_string($this->readAtAttribute) && !empty($this->readAtAttribute)) {
274 20
            $fields[] = $this->readAtAttribute;
275
        }
276 20
        return $fields;
277
    }
278
}
279