Completed
Push — master ( 5109c0...5f03d9 )
by vistart
05:55
created

MessageTrait::touchRead()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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
 * @property $recipient
20
 * @version 2.0
21
 * @author vistart <[email protected]>
22
 */
23
trait MessageTrait
24
{
25
26
    public $otherGuidAttribute = 'other_guid';
27
    public $attachmentAttribute = 'attachment';
28
    public $receivedAtAttribute = 'received_at';
29
    public $readAtAttribute = 'read_at';
30
    public static $eventMessageReceived = 'messageReceived';
31
    public static $eventMessageRead = 'messageRead';
32
    public $permitChangeContent = false;
33
    public $permitChangeReceivedAt = false;
34
    public $permitChangeReadAt = false;
35
36 4
    public function hasBeenReceived()
37
    {
38 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...
39
    }
40
41 4
    public function hasBeenRead()
42
    {
43 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...
44
    }
45
46 2
    public function touchReceived()
47
    {
48 2
        return $this->setReceivedAt(static::currentDatetime());
49
    }
50
51 2
    public function touchRead()
52
    {
53 2
        return $this->setReadAt(static::currentDatetime());
54
    }
55
56 4
    public function getReceivedAt()
57
    {
58 4
        if (is_string($this->receivedAtAttribute)) {
59 4
            $raAttribute = $this->receivedAtAttribute;
60 4
            return $this->$raAttribute;
61
        }
62
        return null;
63
    }
64
65 8
    public function setReceivedAt($receivedAt)
66
    {
67 8
        if (is_string($this->receivedAtAttribute)) {
68 8
            $raAttribute = $this->receivedAtAttribute;
69 8
            return $this->$raAttribute = $receivedAt;
70
        }
71
        return null;
72
    }
73
74 4
    public function getReadAt()
75
    {
76 4
        if (is_string($this->readAtAttribute)) {
77 4
            $raAttribute = $this->readAtAttribute;
78 4
            return $this->$raAttribute;
79
        }
80
        return null;
81
    }
82
83 8
    public function setReadAt($readAt)
84
    {
85 8
        if (is_string($this->readAtAttribute)) {
86 8
            $raAttribute = $this->readAtAttribute;
87 8
            return $this->$raAttribute = $readAt;
88
        }
89
        return null;
90
    }
91
92
    /**
93
     * @param \yii\base\ModelEvent $event
94
     */
95 8
    public function onInitReceivedAtAttribute($event)
96
    {
97 8
        $sender = $event->sender;
98
        /* @var $sender static */
99 8
        $sender->setReceivedAt(static::getInitDatetime($event));
100 8
    }
101
102
    /**
103
     * @param \yii\base\ModelEvent $event
104
     */
105 8
    public function onInitReadAtAttribute($event)
106
    {
107 8
        $sender = $event->sender;
108
        /* @var $sender static */
109 8
        $sender->setReadAt(static::getInitDatetime($event));
110 8
    }
111
112
    /**
113
     * We consider you have received the message if you read it.
114
     * @param \yii\base\ModelEvent $event
115
     */
116 4
    public function onReadAtChanged($event)
117
    {
118 4
        $sender = $event->sender;
119 4
        $raAttribute = $sender->readAtAttribute;
120 4
        if (!is_string($raAttribute)) {
121
            return;
122
        }
123 4
        $reaAttribute = $sender->receivedAtAttribute;
124 4
        if (is_string($reaAttribute) && !$sender->isInitDatetime($sender->$raAttribute) && $sender->isInitDatetime($sender->$reaAttribute)) {
125 2
            $sender->$reaAttribute = $sender->currentDatetime();
126 2
        }
127 4
        if ($sender->permitChangeReadAt) {
128
            return;
129
        }
130 4
        $oldRa = $sender->getOldAttribute($raAttribute);
131 4
        if ($oldRa != null && !$sender->isInitDatetime($oldRa) && $sender->$raAttribute != $oldRa) {
132
            $sender->$raAttribute = $oldRa;
133
            return;
134
        }
135 4
    }
136
137
    /**
138
     * You are not allowed to change receive time if you have received it.
139
     * @param \yii\base\ModelEvent $event
140
     */
141 4
    public function onReceivedAtChanged($event)
142
    {
143 4
        $sender = $event->sender;
144 4
        $raAttribute = $sender->receivedAtAttribute;
145 4
        if (!is_string($raAttribute)) {
146
            return;
147
        }
148 4
        if ($sender->permitChangeReceivedAt) {
149
            return;
150
        }
151 4
        $oldRa = $sender->getOldAttribute($raAttribute);
152 4
        if ($oldRa != null && !$sender->isInitDatetime($oldRa) && $sender->$raAttribute != $oldRa) {
153
            $sender->$raAttribute = $oldRa;
154
            return;
155
        }
156 4
    }
157
158
    /**
159
     * You are not allowed to change the content if it is not new message.
160
     * @param \yii\base\ModelEvent $event
161
     */
162 4
    public function onContentChanged($event)
163
    {
164 4
        $sender = $event->sender;
165 4
        if ($sender->permitChangeContent) {
166
            return;
167
        }
168 4
        $cAttribute = $sender->contentAttribute;
169 4
        $oldContent = $sender->getOldAttribute($cAttribute);
170 4
        if ($oldContent != $sender->$cAttribute) {
171 2
            $sender->$cAttribute = $oldContent;
172 2
        }
173 4
    }
174
175
    /**
176
     * Trigger message received or read events.
177
     * @param \yii\db\AfterSaveEvent $event
178
     */
179 4
    public function onMessageUpdated($event)
180
    {
181 4
        $sender = $event->sender;
182 4
        $reaAttribute = $sender->receivedAtAttribute;
183 4
        if (isset($event->changedAttributes[$reaAttribute]) && $event->changedAttributes[$reaAttribute] != $sender->$reaAttribute) {
184 4
            $sender->trigger(static::$eventMessageReceived);
185 4
        }
186 4
        $raAttribute = $sender->readAtAttribute;
187 4
        if (isset($event->changedAttributes[$raAttribute]) && $event->changedAttributes[$raAttribute] != $sender->$raAttribute) {
188 2
            $sender->trigger(static::$eventMessageRead);
189 2
        }
190 4
    }
191
192 8
    public function initMessageEvents()
193
    {
194 8
        $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...
195 8
        $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...
196 8
        $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...
197 8
        $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...
198 8
        $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...
199 8
        $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...
200 8
    }
201
202 8
    public function getMessageRules()
203
    {
204 8
        $rules = [];
205 8
        if (is_string($this->otherGuidAttribute)) {
206
            $rules = [
207 8
                [$this->otherGuidAttribute, 'required'],
208 8
                [$this->otherGuidAttribute, 'string', 'max' => 36],
209 8
            ];
210 8
        }
211 8
        if (is_string($this->attachmentAttribute)) {
212 8
            $rules[] = [$this->attachmentAttribute, 'safe'];
213 8
        }
214 8
        if (is_string($this->receivedAtAttribute)) {
215 8
            $rules[] = [$this->receivedAtAttribute, 'safe'];
216 8
        }
217 8
        if (is_string($this->readAtAttribute)) {
218 8
            $rules[] = [$this->readAtAttribute, 'safe'];
219 8
        }
220 8
        return $rules;
221
    }
222
223 8
    public function rules()
224
    {
225 8
        return array_merge(parent::rules(), $this->getMessageRules());
226
    }
227
228 8
    public function enabledFields()
229
    {
230 8
        $fields = parent::enabledFields();
231 8
        if (is_string($this->otherGuidAttribute)) {
232 8
            $fields[] = $this->otherGuidAttribute;
233 8
        }
234 8
        if (is_string($this->attachmentAttribute)) {
235 8
            $fields[] = $this->attachmentAttribute;
236 8
        }
237 8
        if (is_string($this->receivedAtAttribute)) {
238 8
            $fields[] = $this->receivedAtAttribute;
239 8
        }
240 8
        if (is_string($this->readAtAttribute)) {
241 8
            $fields[] = $this->readAtAttribute;
242 8
        }
243 8
        return $fields;
244
    }
245
246
    /**
247
     * Get initiator.
248
     * @return \vistart\Models\queries\BaseUserQuery
249
     */
250
    public function getInitiator()
251
    {
252
        return $this->getUser();
0 ignored issues
show
Bug introduced by
It seems like getUser() 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...
253
    }
254
255
    /**
256
     * Get recipient.
257
     * @return \vistart\Models\queries\BaseUserQuery
258
     */
259
    public function getRecipient()
260
    {
261
        if (!is_string($this->otherGuidAttribute)) {
262
            return null;
263
        }
264
        $userClass = $this->userClass;
0 ignored issues
show
Bug introduced by
The property userClass 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...
265
        $model = $userClass::buildNoInitModel();
266
        return $this->hasOne($userClass::className(), [$model->guidAttribute => $this->otherGuidAttribute]);
0 ignored issues
show
Bug introduced by
It seems like hasOne() 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...
267
    }
268
269
    public function setRecipient($user)
270
    {
271
        if (!is_string($this->otherGuidAttribute)) {
272
            return null;
273
        }
274
        $otherGuidAttribute = $this->otherGuidAttribute;
275
        return $this->$otherGuidAttribute = $user->guid;
276
    }
277
}
278