Completed
Pull Request — master (#13)
by ARCANEDEV
03:34
created

Message   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 120
c 0
b 0
f 0
wmc 7
lcom 2
cbo 3
ccs 21
cts 21
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A discussion() 0 6 1
A author() 0 4 1
A participable() 0 4 1
A participations() 0 8 1
A getRecipientsAttribute() 0 7 2
1
<?php namespace Arcanedev\LaravelMessenger\Models;
2
3
use Arcanedev\LaravelMessenger\Contracts\Message as MessageContract;
4
5
/**
6
 * Class     Message
7
 *
8
 * @package  Arcanedev\LaravelMessenger\Models
9
 * @author   ARCANEDEV <[email protected]>
10
 *
11
 * @property  int                                            id
12
 * @property  int                                            discussion_id
13
 * @property  string                                         participable_type
14
 * @property  int                                            participable_id
15
 * @property  int                                            body
16
 * @property  \Carbon\Carbon                                 created_at
17
 * @property  \Carbon\Carbon                                 updated_at
18
 *
19
 * @property  \Arcanedev\LaravelMessenger\Models\Discussion  discussion
20
 * @property  \Illuminate\Database\Eloquent\Model            participable
21
 * @property  \Illuminate\Database\Eloquent\Model            author
22
 * @property  \Illuminate\Database\Eloquent\Collection       participations
23
 * @property  \Illuminate\Database\Eloquent\Collection       recipients
24
 */
25
class Message extends Model implements MessageContract
26
{
27
    /* -----------------------------------------------------------------
28
     |  Properties
29
     | -----------------------------------------------------------------
30
     */
31
32
    /**
33
     * The relationships that should be touched on save.
34
     *
35
     * @var array
36
     */
37
    protected $touches = ['discussion'];
38
39
    /**
40
     * The attributes that can be set with Mass Assignment.
41
     *
42
     * @var array
43
     */
44
    protected $fillable = ['discussion_id', 'body'];
45
46
    /**
47
     * The attributes that should be cast to native types.
48
     *
49
     * @var array
50
     */
51
    protected $casts = [
52
        'id'              => 'integer',
53
        'discussion_id'   => 'integer',
54
        'participable_id' => 'integer',
55
    ];
56
57
    /* -----------------------------------------------------------------
58
     |  Constructor
59
     | -----------------------------------------------------------------
60
     */
61
62
    /**
63
     * Create a new Eloquent model instance.
64
     *
65
     * @param  array  $attributes
66
     */
67 36
    public function __construct(array $attributes = [])
68
    {
69 36
        $this->setTable(
70 36
            $this->getTableFromConfig('messages', 'messages')
71
        );
72
73 36
        parent::__construct($attributes);
74 36
    }
75
76
    /* -----------------------------------------------------------------
77
     |  Relationships
78
     | -----------------------------------------------------------------
79
     */
80
81
    /**
82
     * Discussion relationship.
83
     *
84
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
85
     */
86 33
    public function discussion()
87
    {
88 33
        return $this->belongsTo(
89 33
            $this->getModelFromConfig('discussions', Discussion::class)
90
        );
91
    }
92
93
    /**
94
     * User/Author relationship (alias).
95
     *
96
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
97
     */
98 6
    public function author()
99
    {
100 6
        return $this->participable();
101
    }
102
103
    /**
104
     * Participable relationship.
105
     *
106
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
107
     */
108 6
    public function participable()
109
    {
110 6
        return $this->morphTo();
111
    }
112
113
    /**
114
     * Participations relationship.
115
     *
116
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
117
     */
118 3
    public function participations()
119
    {
120 3
        return $this->hasMany(
121 3
            $this->getModelFromConfig('participations', Participation::class),
122 3
            'discussion_id',
123 3
            'discussion_id'
124
        );
125
    }
126
127
    /* -----------------------------------------------------------------
128
     |  Getters & Setters
129
     | -----------------------------------------------------------------
130
     */
131
132
    /**
133
     * Recipients of this message.
134
     *
135
     * @return \Illuminate\Database\Eloquent\Collection
136
     */
137
    public function getRecipientsAttribute()
138
    {
139 3
        return $this->participations->reject(function (Participation $participant) {
140 3
            return $participant->participable_id === $this->participable_id
141 3
                && $participant->participable_type === $this->participable_type;
142 3
        });
143
    }
144
}
145