1
|
|
|
<?php |
|
|
|
|
2
|
|
|
namespace App; |
3
|
|
|
|
4
|
|
|
use Carbon\Carbon; |
5
|
|
|
use Gitter\Models\User as GitterUser; |
6
|
|
|
use Illuminate\Auth\Authenticatable; |
7
|
|
|
use Illuminate\Auth\Passwords\CanResetPassword; |
8
|
|
|
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; |
9
|
|
|
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; |
10
|
|
|
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; |
11
|
|
|
use Illuminate\Foundation\Auth\Access\Authorizable; |
12
|
|
|
use Illuminate\Support\Collection; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class User |
17
|
|
|
* @package App |
18
|
|
|
* |
19
|
|
|
* @property int $id |
20
|
|
|
* @property string $gitter_id |
21
|
|
|
* @property string $name |
22
|
|
|
* @property string $avatar |
23
|
|
|
* @property string $url |
24
|
|
|
* @property string $login |
25
|
|
|
* @property string $email |
26
|
|
|
* @property string $password |
27
|
|
|
* @property string $remember_token |
28
|
|
|
* @property Carbon $created_at |
29
|
|
|
* @property Carbon $updated_at |
30
|
|
|
* |
31
|
|
|
* @property-read Karma[]|Collection $karma |
32
|
|
|
* |
33
|
|
|
*/ |
34
|
|
|
class User extends \Eloquent implements |
|
|
|
|
35
|
|
|
AuthenticatableContract, |
36
|
|
|
AuthorizableContract, |
37
|
|
|
CanResetPasswordContract |
38
|
|
|
{ |
39
|
|
|
use Authenticatable, |
40
|
|
|
Authorizable, |
41
|
|
|
CanResetPassword; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The database table used by the model. |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $table = 'users'; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The attributes that are mass assignable. |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
protected $fillable = ['gitter_id', 'url', 'login', 'name', 'avatar', 'email', 'password']; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* The attributes excluded from the model's JSON form. |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
protected $hidden = ['password', 'remember_token']; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param GitterUser $gitterUser |
63
|
|
|
* @param \Closure $new |
64
|
|
|
* @return User |
65
|
|
|
*/ |
66
|
|
|
public static function make(GitterUser $gitterUser, \Closure $new = null) |
67
|
|
|
{ |
68
|
|
|
if ($new === null) { |
69
|
|
|
$new = function () { |
70
|
|
|
}; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$user = static::where('gitter_id', $gitterUser->id)->first(); |
74
|
|
|
if (!$user) { |
75
|
|
|
$user = static::create([ |
76
|
|
|
'gitter_id' => $gitterUser->id, |
77
|
|
|
'name' => $gitterUser->displayName, |
78
|
|
|
'avatar' => $gitterUser->avatarUrlMedium, |
79
|
|
|
'url' => $gitterUser->url, |
80
|
|
|
'login' => $gitterUser->username, |
81
|
|
|
'email' => null, |
82
|
|
|
'password' => null, |
83
|
|
|
]); |
84
|
|
|
|
85
|
|
|
$new($user); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $user; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param $roomId |
93
|
|
|
* @return Carbon |
94
|
|
|
*/ |
95
|
|
|
public function getLastKarmaTimeForRoom($roomId) |
96
|
|
|
{ |
97
|
|
|
$result = Karma::query() |
98
|
|
|
->where('user_target_id', $this->gitter_id) |
99
|
|
|
->where('room_id', $roomId) |
100
|
|
|
->orderBy('created_at', 'desc') |
101
|
|
|
->first(); |
102
|
|
|
|
103
|
|
|
if ($result) { |
104
|
|
|
return $result->created_at; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return Carbon::createFromTimestamp(0); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
112
|
|
|
*/ |
113
|
|
|
public function karma() |
114
|
|
|
{ |
115
|
|
|
return $this->hasMany(Karma::class, 'user_target_id', 'gitter_id'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param User $user |
120
|
|
|
* @param Message $message |
121
|
|
|
* @return static |
122
|
|
|
*/ |
123
|
|
|
public function addKarmaToUser(User $user, Message $message) |
124
|
|
|
{ |
125
|
|
|
return Karma::create([ |
126
|
|
|
'room_id' => $message->room_id, |
127
|
|
|
'message_id' => $message->gitter_id, |
128
|
|
|
'user_id' => $this->gitter_id, |
129
|
|
|
'user_target_id' => $user->gitter_id, |
130
|
|
|
'created_at' => $message->created_at, |
131
|
|
|
]); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
|
|
public function isBot() |
138
|
|
|
{ |
139
|
|
|
return $this->gitter_id === \Auth::user()->gitter_id; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.