Completed
Push — develop ( 2f90e1...bffb25 )
by Kirill
07:59
created

Message::user()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace App;
3
4
use Carbon\Carbon;
5
use Gitter\Models\Message as GitterMessage;
6
use Illuminate\Database\Eloquent\Collection;
7
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
8
9
/**
10
 * Class Message
11
 * @package App
12
 *
13
 * @property int $id
14
 * @property string $gitter_id
15
 * @property string $user_id
16
 * @property string $room_id
17
 * @property Text $text
18
 * @property array $sentences
19
 * @property string $html
20
 * @property array $urls
21
 * @property Carbon $created_at
22
 * @property Carbon $updated_at
23
 *
24
 * @property-read User[]|Collection $mentions
25
 * @property-read User $user
26
 *
27
 * @method Message forRoom(Room $room)
28
 */
29
class Message extends \Eloquent
30
{
31
    /**
32
     * @var string
33
     */
34
    protected $primaryKey = 'id';
35
36
    /**
37
     * @var string
38
     */
39
    protected $table = 'messages';
40
41
    /**
42
     * @var array
43
     */
44
    protected $fillable = ['gitter_id', 'user_id', 'room_id', 'text', 'html', 'urls', 'created_at', 'updated_at'];
45
46
    /**
47
     * @param GitterMessage $gitter
48
     * @param \Closure|null $new
49
     * @return Message
50
     */
51
    public static function make(GitterMessage $gitter, \Closure $new = null)
52
    {
53
        if ($new === null) { $new = function(){}; }
54
55
        /** @var Message $message */
56
        $message = static::where('gitter_id', $gitter->id)->first();
57
58
        if (!$message) {
59
            User::make($gitter->fromUser);
60
61
            // Mentions
62
            foreach ($gitter->mentions as $mention) {
63
                if (property_exists($mention, 'userId')) {
64
                    Mention::firstOrCreate([
65
                        'user_id' => $mention->userId,
66
                        'message_id' => $gitter->id
67
                    ]);
68
                }
69
            }
70
71
72
            $message = Message::create([
73
                'gitter_id' => $gitter->id,
74
                'user_id'   => $gitter->fromUser->id,
75
                'room_id'   => $gitter->room->id,
76
                'text'      => $gitter->text,
77
                'html'      => $gitter->html,
78
                'urls'      => json_encode($gitter->urls),
79
                'created_at' => (new Carbon($gitter->sent ?? date('Y-m-d H:i:s', 0)))
0 ignored issues
show
Bug introduced by
It seems like $gitter->sent ?? date('Y-m-d H:i:s', 0) can also be of type object<Carbon\Carbon>; however, Carbon\Carbon::__construct() does only seem to accept string|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
80
                    ->setTimezone('Europe/Moscow')
81
                    ->timestamp,
82
                'updated_at' => (new Carbon($gitter->editedAt ?? date('Y-m-d H:i:s', 0)))
0 ignored issues
show
Bug introduced by
It seems like $gitter->editedAt ?? date('Y-m-d H:i:s', 0) can also be of type object<Carbon\Carbon>; however, Carbon\Carbon::__construct() does only seem to accept string|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
83
                    ->setTimezone('Europe/Moscow')
84
                    ->timestamp,
85
            ]);
86
87
            $new($message);
88
        }
89
90
        return $message;
91
    }
92
93
    /**
94
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
95
     */
96
    public function room()
97
    {
98
        return $this->belongsTo(Room::class, 'room_id', 'gitter_id');
99
    }
100
101
    /**
102
     * @return BelongsToMany
103
     */
104
    public function mentions()
105
    {
106
        $user  = (new User());
107
        $user->primaryKey = 'gitter_id';
108
109
        $parent = (new Message(['gitter_id' => $this->gitter_id]));
110
        $parent->primaryKey = 'gitter_id';
111
112
        return new BelongsToMany($user->newQuery(), $parent, 'mentions', 'message_id', 'user_id');
113
    }
114
115
    /**
116
     * @param User $user
117
     * @return BelongsToMany
118
     */
119
    public function hasMention(User $user)
120
    {
121
        return $this->mentions()->wherePivot('user_id', $user->gitter_id);
122
    }
123
124
    /**
125
     * @return bool
126
     */
127
    public function hasMentions()
128
    {
129
        return (bool)$this->mentions->count();
130
    }
131
132
    /**
133
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
134
     */
135
    public function user()
136
    {
137
        return $this->belongsTo(User::class, 'user_id', 'gitter_id');
138
    }
139
140
    /**
141
     * @param $content
142
     * @return Text
143
     */
144
    public function getTextAttribute($content)
145
    {
146
        return new Text($content);
147
    }
148
149
    /**
150
     * @param array $urls
151
     */
152
    public function setUrlsAttribute($urls = null)
153
    {
154
        $this->attributes['urls'] = '[]';
155
        if ($urls && is_array($urls)) {
156
            $this->attributes['urls'] = json_encode($urls);
157
        }
158
    }
159
160
    /**
161
     * @param $urls
162
     * @return array|mixed
163
     */
164
    public function getUrlsAttribute($urls)
165
    {
166
        $result = json_decode($urls);
167
        if (json_last_error() !== JSON_ERROR_NONE) {
168
            return [];
169
        }
170
171
        return $result;
172
    }
173
}
174