Passed
Push — master ( b831b6...96f132 )
by John
06:04 queued 21s
created

Message   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 239
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 122
dl 0
loc 239
rs 9.6
c 1
b 0
f 1
wmc 35

10 Methods

Rating   Name   Duplication   Size   Complexity  
A removeAllRead() 0 6 1
C send() 0 86 16
A allRead() 0 4 1
A read() 0 8 2
A unread() 0 8 1
A remove() 0 19 5
A getContentAttribute() 0 22 6
A list() 0 8 1
A receiver_user() 0 3 1
A sender_user() 0 3 1
1
<?php
2
3
namespace App\Models\Eloquent;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\SoftDeletes;
7
8
class Message extends Model
9
{
10
    use SoftDeletes;
11
    protected $table='message';
12
13
    /**
14
     * @access public
15
     * @param array $config the config of the message. must including sender,receiver,title,content. //TODO:Can contain reply.
16
     * @return boolean the result.
17
     */
18
    public static function send($config)
19
    {
20
        if(!empty($config['type'])){
21
            if($config['type'] == 1) { //to a leader that member apply to join the group
22
                $messages = Message::where([
23
                    'receiver' => $config['receiver'],
24
                    'type'     => $config['type'],
25
                    'unread'   => 1
26
                ])->get();
27
                if(!empty($messages)) {
28
                    foreach($messages as $message) {
29
                        $data = json_decode($message->data,true);
30
                        if($data['group']['gcode'] == $config['data']['group']['gcode']) {
31
                            array_push($data['user'],$config['data']['user'][0]);
32
                            $message->data = json_encode($data);
33
                            $message->save();
34
                            return true;
35
                        }
36
                    }
37
                }
38
            }elseif ($config['type'] == 2) { //to a leader that member agree to join the group
39
                $messages = Message::where([
40
                    'receiver' => $config['receiver'],
41
                    'type'     => $config['type'],
42
                    'unread'   => 1
43
                ])->get();
44
                if(!empty($messages)) {
45
                    foreach($messages as $message) {
46
                        $data = json_decode($message->data,true);
47
                        if($data['group'] == $config['data']['group']) {
48
                            array_push($data['user'],$config['data']['user'][0]);
49
                            $message->data = json_encode($data);
50
                            $message->save();
51
                            return true;
52
                        }
53
                    }
54
                }
55
            }elseif ($config['type'] == 3) { //to a person that solution was passed
56
                $message = Message::where([
57
                    'receiver' => $config['receiver'],
58
                    'type'     => $config['type'],
59
                    'unread'   => 1
60
                ])->first();
61
                if(!empty($message)) {
62
                    $data = json_decode($message->data,true);
63
                    array_push($data,$config['data']);
64
                    $message->data = json_encode($data);
65
                    $message->save();
66
                    return true;
67
                }
68
            }elseif ($config['type'] == 4) { //to a person that solution was blocked
69
                $message = Message::where([
70
                    'receiver' => $config['receiver'],
71
                    'type'     => $config['type'],
72
                    'unread'   => 1
73
                ])->first();
74
                if(!empty($message)) {
75
                    $data = json_decode($message->data,true);
76
                    array_push($data,$config['data']);
77
                    $message->data = json_encode($data);
78
                    $message->save();
79
                    return true;
80
                }
81
            }
82
        }
83
        $message = new Message;
84
        $message->sender = $config['sender'];
85
        $message->receiver = $config['receiver'];
86
        $message->title = $config['title'];
87
        if(isset($config['data']) && isset($config['type'])){
88
            $message->type = $config['type'] ?? null;
89
            $message->data = json_encode($config['data']);
90
        }else{
91
            $message->content = $config['content'];
92
        }
93
        /*
94
        if(isset($config['reply'])){
95
            $message->reply = $config['reply'];
96
        }
97
        if(isset($config['allow_reply'])){
98
            $message->reply = $config['allow_reply'];
99
        }
100
        */
101
        $message->official = 1;
102
        $message->save();
103
        return true;
104
    }
105
106
    /**
107
     * to get a unread message liist of a user
108
     *
109
     * @access public
110
     * @param integer message receiver id
111
     * @return array the result.
112
     */
113
    public static function unread($uid)
114
    {
115
        return static::where([
116
            'receiver' => $uid,
117
            'unread' => 1,
118
        ])
119
        ->orderByDesc('created_at')
120
        ->get()->all();
121
    }
122
123
124
    /**
125
     * to get a user's all messages' pagnition
126
     *
127
     * @access public
128
     * @param integer id
0 ignored issues
show
Bug introduced by
The type App\Models\Eloquent\id was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
129
     * @return array result.
130
     */
131
    public static function list($uid)
132
    {
133
134
        return static::with('sender_user')
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::with('sen..., 'desc')->paginate(15) returns the type Illuminate\Pagination\LengthAwarePaginator which is incompatible with the documented return type array.
Loading history...
135
            ->where('receiver',$uid)
136
            ->orderBy('unread','desc')
137
            ->orderBy('updated_at','desc')
138
            ->paginate(15);
139
    }
140
141
    /**
142
     * to get a message's detail.
143
     *
144
     * @access public
145
     * @param integer id
146
     * @return array result.
147
     */
148
    public static function read($mid)
149
    {
150
        $message = static::with('sender_user')->find($mid);
151
        if(!empty($message)){
152
            $message->unread = 0;
153
            $message->save();
154
        }
155
        return $message;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $message also could return the type App\Models\Eloquent\Message which is incompatible with the documented return type array.
Loading history...
156
    }
157
158
    /**
159
     * to get a message's detail.
160
     *
161
     * @access public
162
     * @param integer uid
0 ignored issues
show
Bug introduced by
The type App\Models\Eloquent\uid was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
163
     * @return integer result.
164
     */
165
    public static function allRead($uid)
166
    {
167
        return static::where('receiver',$uid)
168
            ->update(['unread' => 0]);
169
    }
170
171
    /**
172
     * to remove a user's all read-ed message.
173
     *
174
     * @access public
175
     * @param integer uid
176
     * @return integer result.
177
     */
178
    public static function removeAllRead($uid)
179
    {
180
        return static::where([
181
            'receiver' => $uid,
182
            'unread' => 0
183
        ])->delete();
184
    }
185
186
    /**
187
     * to soft delete the message
188
     *
189
     * @access public
190
     * @param integer|array the id of the message or the array with ids of the messages.
0 ignored issues
show
Bug introduced by
The type App\Models\Eloquent\the was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
191
     * @return bool result.
192
     */
193
    public static function remove($messages)
194
    {
195
        $del_count = 0;
196
        if(is_array($messages)){
197
            foreach ($messages as $mid) {
198
                $message = static::find($mid);
199
                if(!empty($message)){
200
                    $message->delete();
201
                    $del_count ++;
202
                }
203
            }
204
        }else{
205
            $message = static::find($messages);
206
            if(!empty($message)){
207
                $message->delete();
208
                $del_count ++;
209
            }
210
        }
211
        return $del_count;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $del_count returns the type integer which is incompatible with the documented return type boolean.
Loading history...
212
    }
213
214
    public function getContentAttribute($value)
215
    {
216
        if(!empty($this->type)){
217
            $data = json_decode($this->data,true);
218
            $content = '';
219
            if($this->type == 1) {
220
                foreach($data['user'] as $user) {
221
                    $content .= "[{$user['name']}]({$user['url']}), ";
222
                }
223
                $content = substr($content,0,strlen($content)-2);
224
                $content .= " want to join your group [{$data['group']['name']}]({$data['group']['url']})";
225
                return $content;
226
            }elseif($this->type == 2) {
227
                foreach($data['user'] as $user) {
228
                    $content .= "[{$user['name']}]({$user['url']}), ";
229
                }
230
                $content = substr($content,0,strlen($content)-2);
231
                $content .= " have agreed to join your group [{$data['group']['name']}]({$data['group']['url']})";
232
                return $content;
233
            } //todo
234
        }else{
235
            return $value;
236
        }
237
    }
238
239
    public function sender_user()
240
    {
241
        return $this->belongsTo('App\Models\Eloquent\UserModel','sender','id');
242
    }
243
244
    public function receiver_user()
245
    {
246
        return $this->belongsTo('App\Models\Eloquent\UserModel','receiver','id');
247
    }
248
}
249