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\Foundation\Auth\Access\Authorizable; |
9
|
|
|
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; |
10
|
|
|
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; |
11
|
|
|
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class User |
16
|
|
|
* @package App |
17
|
|
|
* |
18
|
|
|
* @property int $id |
19
|
|
|
* @property string $gitter_id |
20
|
|
|
* @property string $name |
21
|
|
|
* @property string $avatar |
22
|
|
|
* @property string $url |
23
|
|
|
* @property string $login |
24
|
|
|
* @property string $email |
25
|
|
|
* @property string $password |
26
|
|
|
* @property string $remember_token |
27
|
|
|
* @property Carbon $created_at |
28
|
|
|
* @property Carbon $updated_at |
29
|
|
|
* |
30
|
|
|
*/ |
31
|
|
|
class User extends \Eloquent implements |
|
|
|
|
32
|
|
|
AuthenticatableContract, |
33
|
|
|
AuthorizableContract, |
34
|
|
|
CanResetPasswordContract |
35
|
|
|
{ |
36
|
|
|
use Authenticatable, |
37
|
|
|
Authorizable, |
38
|
|
|
CanResetPassword; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The database table used by the model. |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $table = 'users'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The attributes that are mass assignable. |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $fillable = ['gitter_id', 'url', 'login', 'name', 'avatar', 'email', 'password']; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The attributes excluded from the model's JSON form. |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
protected $hidden = ['password', 'remember_token']; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param GitterUser $gitterUser |
60
|
|
|
* @param \Closure $new |
61
|
|
|
* @return User |
62
|
|
|
*/ |
63
|
|
|
public static function make(GitterUser $gitterUser, \Closure $new = null) |
64
|
|
|
{ |
65
|
|
|
if ($new === null) { $new = function(){}; } |
66
|
|
|
|
67
|
|
|
$user = static::where('gitter_id', $gitterUser->id)->first(); |
68
|
|
|
if (!$user) { |
69
|
|
|
$user = static::create([ |
70
|
|
|
'gitter_id' => $gitterUser->id, |
71
|
|
|
'name' => $gitterUser->displayName, |
72
|
|
|
'avatar' => $gitterUser->avatarUrlMedium, |
73
|
|
|
'url' => $gitterUser->url, |
74
|
|
|
'login' => $gitterUser->username, |
75
|
|
|
'email' => null, |
76
|
|
|
'password' => null, |
77
|
|
|
]); |
78
|
|
|
|
79
|
|
|
$new($user); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $user; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param $roomId |
87
|
|
|
* @return Carbon |
88
|
|
|
*/ |
89
|
|
|
public function getLastKarmaTimeForRoom($roomId) |
90
|
|
|
{ |
91
|
|
|
$result = Karma::query() |
92
|
|
|
->where('user_target_id', $this->gitter_id) |
93
|
|
|
->where('room_id', $roomId) |
94
|
|
|
->orderBy('created_at', 'desc') |
95
|
|
|
->first(); |
96
|
|
|
|
97
|
|
|
if ($result) { |
98
|
|
|
return $result->created_at; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return Carbon::createFromTimestamp(0); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function addKarmaToUser(User $user, Carbon $time = null) |
105
|
|
|
{ |
106
|
|
|
if ($time === null) { $time = Carbon::now(); } |
|
|
|
|
107
|
|
|
|
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
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.