1
|
|
|
<?php |
2
|
|
|
namespace App; |
3
|
|
|
|
4
|
|
|
use Carbon\Carbon; |
5
|
|
|
use Gitter\Models\Room as GitterRoom; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Room |
9
|
|
|
* @package App |
10
|
|
|
* |
11
|
|
|
* @property int $id |
12
|
|
|
* @property string $gitter_id |
13
|
|
|
* @property string $title |
14
|
|
|
* @property string $url |
15
|
|
|
* @property string $type `personal` or `group` |
16
|
|
|
* @property Carbon $created_at |
17
|
|
|
* @property Carbon $updated_at |
18
|
|
|
* |
19
|
|
|
* @property-read Message[] $messages |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
class Room extends \Eloquent |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $table = 'rooms'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $fillable = ['gitter_id', 'title', 'url', 'type']; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param GitterRoom $gitterRoom |
36
|
|
|
* @param \Closure $new |
37
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null|static |
38
|
|
|
*/ |
39
|
|
|
public static function make(GitterRoom $gitterRoom, \Closure $new = null) |
40
|
|
|
{ |
41
|
|
|
if ($new === null) { $new = function(){}; } |
42
|
|
|
|
43
|
|
|
$room = static::where('gitter_id', $gitterRoom->id)->first(); |
44
|
|
|
if (!$room) { |
45
|
|
|
$room = static::create([ |
46
|
|
|
'title' => $gitterRoom->topic, |
47
|
|
|
'gitter_id' => $gitterRoom->id, |
48
|
|
|
'url' => $gitterRoom->url, |
49
|
|
|
'type' => $gitterRoom->githubType |
50
|
|
|
]); |
51
|
|
|
$new($room); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $room; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @DANGEROUS Do not fetch all messages |
59
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
60
|
|
|
*/ |
61
|
|
|
public function messages() |
62
|
|
|
{ |
63
|
|
|
return $this->hasMany(Message::class, 'room_id', 'gitter_id'); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|