Completed
Push — master ( a53812...c29406 )
by vistart
06:25
created

MessageTrait   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 25
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 125
ccs 0
cts 67
cp 0
rs 10

13 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 2
A setReceivedAt() 0 8 2
A getReadAt() 0 8 2
A setReadAt() 0 8 2
A onInitReceivedAtAttribute() 0 6 1
A onInitReadAtAttribute() 0 6 1
A getMessageRules() 0 17 4
A rules() 0 4 1
A enabledFields() 0 14 4
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 $recipientGuidAttribute = '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
32
    public function hasBeenReceived()
33
    {
34
        return is_string($this->receivedAtAttribute) ? $this->isInitDatetime($this->receivedAtAttribute) : 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...
35
    }
36
37
    public function hasBeenRead()
38
    {
39
        return is_string($this->readAtAttribute) ? $this->isInitDatetim($this->readAtAttribute) : false;
0 ignored issues
show
Bug introduced by
It seems like isInitDatetim() 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...
40
    }
41
42
    public function touchReceived($event)
43
    {
44
        $this->setReceivedAt(static::getCurrentDatetime($event));
45
    }
46
47
    public function touchRead($event)
48
    {
49
        $this->setReadAt(static::getCurrentDatetime($event));
50
    }
51
52
    public function getReceivedAt()
53
    {
54
        if (is_string($this->receivedAtAttribute)) {
55
            $raAttribute = $this->receivedAtAttribute;
56
            return $this->$raAttribute;
57
        }
58
        return null;
59
    }
60
61
    public function setReceivedAt($receivedAt)
62
    {
63
        if (is_string($this->receivedAtAttribute)) {
64
            $raAttribute = $this->receivedAtAttribute;
65
            return $this->$raAttribute = $receivedAt;
66
        }
67
        return null;
68
    }
69
70
    public function getReadAt()
71
    {
72
        if (is_string($this->readAtAttribute)) {
73
            $raAttribute = $this->readAtAttribute;
74
            return $this->$raAttribute;
75
        }
76
        return null;
77
    }
78
79
    public function setReadAt($readAt)
80
    {
81
        if (is_string($this->readAtAttribute)) {
82
            $raAttribute = $this->readAtAttribute;
83
            return $this->$raAttribute = $readAt;
84
        }
85
        return null;
86
    }
87
88
    /**
89
     * @param \yii\base\ModelEvent $event
90
     */
91
    public function onInitReceivedAtAttribute($event)
92
    {
93
        $sender = $event->sender;
94
        /* @var $sender static */
95
        $sender->setReceivedAt(static::getInitDatetime($event));
96
    }
97
98
    /**
99
     * @param \yii\base\ModelEvent $event
100
     */
101
    public function onInitReadAtAttribute($event)
102
    {
103
        $sender = $event->sender;
104
        /* @var $sender static */
105
        $sender->setReadAt(static::getInitDatetime($event));
106
    }
107
108
    public function getMessageRules()
109
    {
110
        $rules = [
111
            [$this->recipientGuidAttribute, 'required'],
112
            [$this->recipientGuidAttribute, 'string', 'max' => 36],
113
        ];
114
        if (is_string($this->attachmentAttribute)) {
115
            $rules[] = [$this->attachmentAttribute, 'safe'];
116
        }
117
        if (is_string($this->receivedAtAttribute)) {
118
            $rules[] = [$this->receivedAtAttribute, 'safe'];
119
        }
120
        if (is_string($this->readAtAttribute)) {
121
            $rules[] = [$this->readAtAttribute, 'safe'];
122
        }
123
        return $rules;
124
    }
125
126
    public function rules()
127
    {
128
        return array_merge(parent::rules(), $this->getMessageRules());
129
    }
130
131
    public function enabledFields()
132
    {
133
        $fields = parent::enabledFields();
134
        $fields[] = $this->recipientGuidAttribute;
135
        if (is_string($this->attachmentAttribute)) {
136
            $fields[] = $this->attachmentAttribute;
137
        }
138
        if (is_string($this->receivedAtAttribute)) {
139
            $fields[] = $this->receivedAtAttribute;
140
        }
141
        if (is_string($this->readAtAttribute)) {
142
            $fields[] = $this->readAtAttribute;
143
        }
144
    }
145
}
146