Passed
Pull Request — master (#264)
by
unknown
05:25
created

MessageModel::send()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
cc 1
eloc 8
c 3
b 0
f 2
nc 1
nop 1
dl 0
loc 18
rs 10
1
<?php
2
3
namespace App\Models\Eloquent;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\SoftDeletes;
7
8
class MessageModel 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 array the result. has 'result' ['ret' => 'success/faild', 'message' => null/'faild reason'].
17
     */
18
    public static function send($config)
19
    {
20
        $message = new MessageModel;
21
        $message->sender = $config['sender'];
22
        $message->receiver = $config['receiver'];
23
        $message->title = $config['title'];
24
        $message->content = $config['content'];
25
        /*
26
        if(isset($config['reply'])){
27
            $message->reply = $config['reply'];
28
        }
29
        if(isset($config['allow_reply'])){
30
            $message->reply = $config['allow_reply'];
31
        }
32
        */
33
        $message->official = 1;
34
        $message->save();
35
        return true;
0 ignored issues
show
Bug Best Practice introduced by
The expression return true returns the type true which is incompatible with the documented return type array.
Loading history...
36
    }
37
38
    /**
39
     * to get a unread message liist of a user
40
     *
41
     * @access public
42
     * @param integer message receiver id
43
     * @return array the result.
44
     */
45
    public static function unread($uid)
46
    {
47
        return static::where([
48
            'receiver' => $uid,
49
            'unread' => 1,
50
        ])
51
        ->orderByDesc('created_at')
52
        ->get()->all();
53
    }
54
55
    /**
56
     * to check if a message allows replies
57
     *
58
     * @access public
59
     * @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...
60
     * @return array result.
61
     */
62
    public static function allowReply($id)
63
    {
64
        $message = static::where('id',$id)->first();
65
        if(empty($message)){
66
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type array.
Loading history...
67
        }
68
        return $message['allow_reply'] ? true : false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $message['allow_reply'] ? true : false returns the type boolean which is incompatible with the documented return type array.
Loading history...
69
    }
70
71
    /**
72
     * to get a user's all messages' pagnition
73
     *
74
     * @access public
75
     * @param integer id
76
     * @return array result.
77
     */
78
    public static function list($uid)
79
    {
80
        return static::join('users','message.sender','=','users.id')
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::join('use....unread')->paginate(30) returns the type Illuminate\Pagination\LengthAwarePaginator which is incompatible with the documented return type array.
Loading history...
81
            ->where('receiver',$uid)
82
            ->select(
83
                'message.id as id',
84
                'users.name as sender_name',
85
                'users.avatar as sender_avatar',
86
                'message.sender as sender',
87
                'message.title as title',
88
                'message.created_at as time',
89
                'message.official as official',
90
                'message.unread as unread'
91
            )
92
            ->orderByDesc('message.created_at')
93
            ->orderByDesc('message.unread')
94
            ->paginate(30);
95
    }
96
97
    /**
98
     * to get a message's detail.
99
     *
100
     * @access public
101
     * @param integer id
102
     * @return array result.
103
     */
104
    public static function read($mid)
105
    {
106
        $message = static::find($mid);
107
        if(!empty($message)){
108
            $message->unread = 0;
109
            $message->save();
110
        }
111
        return static::join('users','message.sender','=','users.id')
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::join('use... allow_reply')->first() also could return the type App\Models\Eloquent\MessageModel which is incompatible with the documented return type array.
Loading history...
112
            ->where('message.id',$mid)
113
            ->select(
114
                'message.id as id',
115
                'users.name as sender_name',
116
                'users.avatar as sender_avatar',
117
                'message.sender as sender',
118
                'message.title as title',
119
                'message.content as content',
120
                'message.created_at as time',
121
                'message.official as official',
122
                'message.unread as unread',
123
                'message.allow_reply as allow_reply'
124
            )->first();
125
    }
126
127
    /**
128
     * to get a message's detail.
129
     *
130
     * @access public
131
     * @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...
132
     * @return integer result.
133
     */
134
    public static function allRead($uid)
135
    {
136
        return static::where('receiver',$uid)
137
            ->update(['unread' => 0]);
138
    }
139
140
    /**
141
     * to remove a user's all read-ed message.
142
     *
143
     * @access public
144
     * @param integer uid
145
     * @return integer result.
146
     */
147
    public static function removeAllRead($uid)
148
    {
149
        return static::where([
150
            'receiver' => $uid,
151
            'unread' => 0
152
        ])->delete();
153
    }
154
155
    /**
156
     * to soft delete the message
157
     *
158
     * @access public
159
     * @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...
160
     * @return bool result.
161
     */
162
    public static function remove($messages)
163
    {
164
        $del_count = 0;
165
        if(is_array($messages)){
166
            foreach ($messages as $mid) {
167
                $message = static::find($mid);
168
                if(!empty($message)){
169
                    $message->delete();
170
                    $del_count ++;
171
                }
172
            }
173
        }else{
174
            $message = static::find($messages);
175
            if(!empty($message)){
176
                $message->delete();
177
                $del_count ++;
178
            }
179
        }
180
        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...
181
    }
182
}
183