Completed
Push — master ( 9aa93d...bd75c7 )
by vistart
08:03
created

MessageTrait::getReadAt()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 4
cts 5
cp 0.8
rs 9.4285
cc 3
eloc 5
nc 2
nop 0
crap 3.072
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 2
    public function hasBeenReceived()
47
    {
48 2
        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 2
    public function hasBeenRead()
58
    {
59 2
        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 1
    public function touchReceived()
63
    {
64 1
        return $this->setReceivedAt(static::currentDatetime());
65
    }
66
    
67 1
    public function touchRead()
68
    {
69 1
        return $this->setReadAt(static::currentDatetime());
70
    }
71
    
72 2
    public function getReceivedAt()
73
    {
74 2
        if (is_string($this->receivedAtAttribute) && !empty($this->receivedAtAttribute)) {
75 2
            $raAttribute = $this->receivedAtAttribute;
76 2
            return $this->$raAttribute;
77
        }
78
        return null;
79
    }
80
    
81 4
    public function setReceivedAt($receivedAt)
82
    {
83 4
        if (is_string($this->receivedAtAttribute) && !empty($this->receivedAtAttribute)) {
84 4
            $raAttribute = $this->receivedAtAttribute;
85 4
            return $this->$raAttribute = $receivedAt;
86
        }
87
        return null;
88
    }
89
    
90 2
    public function getReadAt()
91
    {
92 2
        if (is_string($this->readAtAttribute) && !empty($this->readAtAttribute)) {
93 2
            $raAttribute = $this->readAtAttribute;
94 2
            return $this->$raAttribute;
95
        }
96
        return null;
97
    }
98
    
99 4
    public function setReadAt($readAt)
100
    {
101 4
        if (is_string($this->readAtAttribute) && !empty($this->readAtAttribute)) {
102 4
            $raAttribute = $this->readAtAttribute;
103 4
            return $this->$raAttribute = $readAt;
104
        }
105
        return null;
106
    }
107
    
108
    /**
109
     *
110
     * @param ModelEvent $event
111
     */
112 4
    public function onInitReceivedAtAttribute($event)
113
    {
114 4
        $sender = $event->sender;
115
        /* @var $sender static */
116 4
        $sender->setReceivedAt(static::getInitDatetime($event));
117 4
    }
118
    
119
    /**
120
     *
121
     * @param ModelEvent $event
122
     */
123 4
    public function onInitReadAtAttribute($event)
124
    {
125 4
        $sender = $event->sender;
126
        /* @var $sender static */
127 4
        $sender->setReadAt(static::getInitDatetime($event));
128 4
    }
129
    
130
    /**
131
     * We consider you have received the message if you read it.
132
     * @param ModelEvent $event
133
     */
134 2
    public function onReadAtChanged($event)
135
    {
136 2
        $sender = $event->sender;
137
        /* @var $sender static */
138 2
        $raAttribute = $sender->readAtAttribute;
139 2
        if (!is_string($raAttribute) || empty($raAttribute)) {
140
            return;
141
        }
142 2
        $reaAttribute = $sender->receivedAtAttribute;
143 2
        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 1
            $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 2
        if ($sender->permitChangeReadAt) {
148
            return;
149
        }
150 2
        $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 2
        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 2
    }
155
    
156
    /**
157
     * You are not allowed to change receiving time if you have received it.
158
     * @param ModelEvent $event
159
     */
160 2
    public function onReceivedAtChanged($event)
161
    {
162 2
        $sender = $event->sender;
163 2
        $raAttribute = $sender->receivedAtAttribute;
164 2
        if (!is_string($raAttribute) || empty($raAttribute)) {
165
            return;
166
        }
167
        // If it is permitted to change receiving time, then it will return directly.
168 2
        if ($sender->permitChangeReceivedAt) {
169
            return;
170
        }
171 2
        $oldRa = $sender->getOldAttribute($raAttribute);
172 2
        if ($oldRa != null && !$sender->isInitDatetime($oldRa) && $sender->$raAttribute != $oldRa) {
173
            $sender->$raAttribute = $oldRa;
174
        }
175 2
    }
176
    
177
    /**
178
     * You are not allowed to change the content if it is not new message.
179
     * @param ModelEvent $event
180
     */
181 2
    public function onContentChanged($event)
182
    {
183 2
        $sender = $event->sender;
184
        // If it is permitted to change content, then it will return directly.
185 2
        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 2
        $cAttribute = $sender->contentAttribute;
191 2
        $oldContent = $sender->getOldAttribute($cAttribute);
192 2
        if ($oldContent != $sender->$cAttribute) {
193 1
            $sender->$cAttribute = $oldContent;
194
        }
195 2
    }
196
    
197
    /**
198
     *
199
     * @param AfterSaveEvent $event
200
     */
201 2
    public function onMessageUpdated($event)
202
    {
203 2
        $sender = $event->sender;
204
        /* @var $sender static */
205 2
        $reaAttribute = $sender->receivedAtAttribute;
206 2
        if (isset($event->changedAttributes[$reaAttribute]) && $event->changedAttributes[$reaAttribute] != $sender->$reaAttribute) {
207 2
            $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 2
        $raAttribute = $sender->readAtAttribute;
210 2
        if (isset($event->changedAttributes[$raAttribute]) && $event->changedAttributes[$raAttribute] != $sender->$raAttribute) {
211 1
            $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 2
    }
214
    
215
    /**
216
     *
217
     */
218 4
    public function initMessageEvents()
219
    {
220 4
        $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 4
        $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 4
        $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 4
        $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 4
        $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 4
        $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 4
    }
227
    
228
    /**
229
     * Return rules associated with message.
230
     * @return array
231
     */
232 4
    public function getMessageRules()
233
    {
234 4
        $rules = [];
235 4
        $rules = array_merge($rules, $this->getMutualRules());
236 4
        if (is_string($this->attachmentAttribute) && !empty($this->attachmentAttribute)) {
237 4
            $rules[] = [$this->attachmentAttribute, 'safe'];
238
        }
239 4
        if (is_string($this->receivedAtAttribute) && !empty($this->receivedAtAttribute)) {
240 4
            $rules[] = [$this->receivedAtAttribute, 'safe'];
241
        }
242 4
        if (is_string($this->readAtAttribute) && !empty($this->readAtAttribute)) {
243 4
            $rules[] = [$this->readAtAttribute, 'safe'];
244
        }
245 4
        return $rules;
246
    }
247
    
248
    /**
249
     * @inheritdoc
250
     * @return array
251
     */
252 4
    public function rules()
253
    {
254 4
        return array_merge(parent::rules(), $this->getMessageRules());
255
    }
256
    
257
    /**
258
     * @inheritdoc
259
     * @return array
260
     */
261 4
    public function enabledFields()
262
    {
263 4
        $fields = parent::enabledFields();
264 4
        if (is_string($this->otherGuidAttribute) && !empty($this->otherGuidAttribute)) {
265 4
            $fields[] = $this->otherGuidAttribute;
266
        }
267 4
        if (is_string($this->attachmentAttribute) && !empty($this->attachmentAttribute)) {
268 4
            $fields[] = $this->attachmentAttribute;
269
        }
270 4
        if (is_string($this->receivedAtAttribute) && !empty($this->receivedAtAttribute)) {
271 4
            $fields[] = $this->receivedAtAttribute;
272
        }
273 4
        if (is_string($this->readAtAttribute) && !empty($this->readAtAttribute)) {
274 4
            $fields[] = $this->readAtAttribute;
275
        }
276 4
        return $fields;
277
    }
278
}
279