Completed
Push — develop ( 9773af...b9ad6f )
by Kirill
32:11 queued 50s
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\ { Builder, Collection };
7
8
/**
9
 * Class Message
10
 * @package App
11
 *
12
 * @property int $id
13
 * @property string $gitter_id
14
 * @property string $user_id
15
 * @property string $room_id
16
 * @property Text $text
17
 * @property array $sentences
18
 * @property string $html
19
 * @property array $urls
20
 * @property Carbon $created_at
21
 * @property Carbon $updated_at
22
 *
23
 * @property-read Mention[]|Collection $mentions
24
 * @property-read User $user
25
 *
26
 * @method Message forRoom(Room $room)
27
 */
28
class Message extends \Eloquent
29
{
30
    /**
31
     * @var string
32
     */
33
    protected $primaryKey = 'id';
34
35
    /**
36
     * @var string
37
     */
38
    protected $table = 'messages';
39
40
    /**
41
     * @var array
42
     */
43
    protected $fillable = ['gitter_id', 'user_id', 'room_id', 'text', 'html', 'urls', 'created_at', 'updated_at'];
44
45
    /**
46
     * @param GitterMessage $gitter
47
     * @param \Closure|null $new
48
     * @return Message
49
     */
50
    public static function make(GitterMessage $gitter, \Closure $new = null)
51
    {
52
        /** @var Message $message */
53
        $message = static::where('gitter_id', $gitter->id)->first();
54
        if (!$message) {
55
            User::make($gitter->fromUser);
56
57
            // Mentions
58
            foreach ($gitter->mentions as $mention) {
59
                if (property_exists($mention, 'userId')) {
60
                    Mention::firstOrCreate([
61
                        'user_id' => $mention->userId,
62
                        'message_id' => $gitter->id
63
                    ]);
64
                }
65
            }
66
67
68
            $message = Message::create([
69
                'gitter_id' => $gitter->id,
70
                'user_id'   => $gitter->fromUser->id,
71
                'room_id'   => $gitter->room->id,
72
                'text'      => $gitter->text,
73
                'html'      => $gitter->html,
74
                'urls'      => json_encode($gitter->urls),
75
                '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...
76
                    ->setTimezone('Europe/Moscow')
77
                    ->timestamp,
78
                '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...
79
                    ->setTimezone('Europe/Moscow')
80
                    ->timestamp,
81
            ]);
82
        }
83
84
        return $message;
85
    }
86
87
    /**
88
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
89
     */
90
    public function room()
91
    {
92
        return $this->belongsTo(Room::class, 'room_id', 'gitter_id');
93
    }
94
95
96
    /**
97
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
98
     */
99
    public function mentions()
100
    {
101
        return $this->hasMany(Mention::class, 'message_id', 'gitter_id');
102
    }
103
104
    /**
105
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
106
     */
107
    public function user()
108
    {
109
        return $this->belongsTo(User::class, 'user_id', 'gitter_id');
110
    }
111
112
    /**
113
     * @param $content
114
     * @return Text
115
     */
116
    public function getTextAttribute($content)
117
    {
118
        return new Text($content);
119
    }
120
121
    /**
122
     * @param array $urls
123
     */
124
    public function setUrlsAttribute($urls = [])
125
    {
126
        $this->attributes['urls'] = json_encode($urls);
127
    }
128
129
    /**
130
     * @param $urls
131
     * @return array|mixed
132
     */
133
    public function getUrlsAttribute($urls)
134
    {
135
        $result = json_decode($urls);
136
        if (json_last_error() !== JSON_ERROR_NONE) {
137
            return [];
138
        }
139
140
        return $result;
141
    }
142
}
143