Completed
Push — master ( c29406...3a3831 )
by vistart
05:37
created

MessageTrait   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 69.01%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 26
c 2
b 0
f 0
lcom 1
cbo 1
dl 0
loc 132
ccs 49
cts 71
cp 0.6901
rs 10

14 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 getReadAt() 0 8 2
A setReceivedAt() 0 8 2
A setReadAt() 0 8 2
A onInitReceivedAtAttribute() 0 6 1
A onInitReadAtAttribute() 0 6 1
A initMessageEvents() 0 5 1
A getMessageRules() 0 17 4
A rules() 0 4 1
A enabledFields() 0 15 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 1
    public function setReceivedAt($receivedAt)
62
    {
63 1
        if (is_string($this->receivedAtAttribute)) {
64 1
            $raAttribute = $this->receivedAtAttribute;
65 1
            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 1
    public function setReadAt($readAt)
80
    {
81 1
        if (is_string($this->readAtAttribute)) {
82 1
            $raAttribute = $this->readAtAttribute;
83 1
            return $this->$raAttribute = $readAt;
84
        }
85
        return null;
86
    }
87
88
    /**
89
     * @param \yii\base\ModelEvent $event
90
     */
91 1
    public function onInitReceivedAtAttribute($event)
92
    {
93 1
        $sender = $event->sender;
94
        /* @var $sender static */
95 1
        $sender->setReceivedAt(static::getInitDatetime($event));
96 1
    }
97
98
    /**
99
     * @param \yii\base\ModelEvent $event
100
     */
101 1
    public function onInitReadAtAttribute($event)
102
    {
103 1
        $sender = $event->sender;
104
        /* @var $sender static */
105 1
        $sender->setReadAt(static::getInitDatetime($event));
106 1
    }
107
    
108 1
    public function initMessageEvents()
109
    {
110 1
        $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...
111 1
        $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...
112 1
    }
113
114 1
    public function getMessageRules()
115
    {
116
        $rules = [
117 1
            [$this->recipientGuidAttribute, 'required'],
118 1
            [$this->recipientGuidAttribute, 'string', 'max' => 36],
119 1
        ];
120 1
        if (is_string($this->attachmentAttribute)) {
121 1
            $rules[] = [$this->attachmentAttribute, 'safe'];
122 1
        }
123 1
        if (is_string($this->receivedAtAttribute)) {
124 1
            $rules[] = [$this->receivedAtAttribute, 'safe'];
125 1
        }
126 1
        if (is_string($this->readAtAttribute)) {
127 1
            $rules[] = [$this->readAtAttribute, 'safe'];
128 1
        }
129 1
        return $rules;
130
    }
131
132 1
    public function rules()
133
    {
134 1
        return array_merge(parent::rules(), $this->getMessageRules());
135
    }
136
137 1
    public function enabledFields()
138
    {
139 1
        $fields = parent::enabledFields();
140 1
        $fields[] = $this->recipientGuidAttribute;
141 1
        if (is_string($this->attachmentAttribute)) {
142 1
            $fields[] = $this->attachmentAttribute;
143 1
        }
144 1
        if (is_string($this->receivedAtAttribute)) {
145 1
            $fields[] = $this->receivedAtAttribute;
146 1
        }
147 1
        if (is_string($this->readAtAttribute)) {
148 1
            $fields[] = $this->readAtAttribute;
149 1
        }
150 1
        return $fields;
151
    }
152
}
153