Completed
Push — development ( e80362...5ad269 )
by Claudio
02:41
created

User   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 322
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 7
dl 0
loc 322
rs 10
c 0
b 0
f 0

21 Methods

Rating   Name   Duplication   Size   Complexity  
A store() 0 21 2
A createData() 0 4 1
A getIsBannedAttribute() 0 4 1
A getIsStaffAttribute() 0 4 2
A getBanDetailsAttribute() 0 4 1
A getCountryAttribute() 0 4 1
A setTraitsAttribute() 0 4 1
A getTraitsAttribute() 0 8 3
A getTrustedAttribute() 0 9 2
A getIdentityTypeAttribute() 0 4 1
A getIdentityVerifiedAttribute() 0 4 1
A getLoginLogIdAttribute() 0 4 1
A getSessionLoginIdAttribute() 0 4 1
A getHabboClubMemberAttribute() 0 4 1
A getBuildersClubMemberAttribute() 0 4 1
A getAccountCreatedAttribute() 0 6 1
A getMemberSinceAttribute() 0 6 1
A getFigureStringAttribute() 0 4 1
A getLastLoginAttribute() 0 6 1
A getEmailVerifiedAttribute() 0 4 1
A getChocolateyId() 0 4 1
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Auth\Authenticatable;
6
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
7
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Support\Facades\Config;
10
use Laravel\Lumen\Auth\Authorizable;
11
use Sofa\Eloquence\Eloquence;
12
use Sofa\Eloquence\Mappable;
13
14
/**
15
 * Class User
16
 * @package App\Models
17
 *
18
 * @property string trusted
19
 * @property int uniqueId
20
 * @property string figureString
21
 * @property string name
22
 * @property string motto
23
 */
24
class User extends Model implements AuthenticatableContract, AuthorizableContract
25
{
26
    use Authenticatable, Authorizable, Eloquence, Mappable;
27
28
    /**
29
     * Disable Timestamps.
30
     *
31
     * @var bool
32
     */
33
    public $timestamps = false;
34
35
    /**
36
     * User Traits.
37
     *
38
     * @var array
39
     */
40
    public $traits = array('USER');
41
42
    /**
43
     * The table associated with the model.
44
     *
45
     * @var string
46
     */
47
    protected $table = 'users';
48
49
    /**
50
     * Primary Key of the Table.
51
     *
52
     * @var string
53
     */
54
    protected $primaryKey = 'id';
55
56
    /**
57
     * The attributes that will be mapped.
58
     *
59
     * @var array
60
     */
61
    protected $maps = array('uniqueId' => 'id', 'name' => 'username', 'figureString' => 'look', 'lastWebAccess' => 'last_login', 'creationTime' => 'account_created', 'email' => 'mail', 'identityId' => 'id', 'accountId' => 'id');
62
63
    /**
64
     * The Appender(s) of the Model.
65
     *
66
     * @var array
67
     */
68
    protected $appends = array('habboClubMember', 'buildersClubMember', 'sessionLoginId', 'loginLogId', 'identityVerified', 'identityType', 'trusted', 'country', 'traits',
69
        'uniqueId', 'name', 'figureString', 'lastWebAccess', 'creationTime', 'email', 'identityId', 'emailVerified', 'accountId', 'memberSince', 'isBanned', 'banDetails', 'isStaff');
70
71
    /**
72
     * The attributes that are mass assignable.
73
     *
74
     * @var array
75
     */
76
    protected $fillable = array('mail', 'id', 'username', 'auth_ticket', 'last_login', 'ip_current', 'ip_register', 'mail_verified', 'account_day_of_birth',
77
        'real_name', 'look', 'gender', 'credits', 'pixels', 'home_room');
78
79
    /**
80
     * The attributes excluded from the model's JSON form.
81
     *
82
     * @var array
83
     */
84
    protected $hidden = array('id', 'username', 'mail', 'account_created', 'password', 'mail_verified', 'real_name', 'account_day_of_birth',
85
        'last_online', 'last_login', 'ip_register', 'auth_ticket', 'home_room', 'points', 'look', 'ip_current', 'online', 'pixels', 'credits', 'gender', 'points', 'rank');
86
87
    /**
88
     * The attributes that should be casted to native types.
89
     *
90
     * @var array
91
     */
92
    protected $casts = array('traits' => 'string');
93
94
    /**
95
     * Store an User on the Database.
96
     *
97
     * @param string $username
98
     * @param string $email
99
     * @param string $address
100
     * @param bool $newUser
101
     * @return User
102
     */
103
    public function store(string $username, string $email, string $address = '', bool $newUser = true): User
104
    {
105
        $this->attributes['username'] = $username;
106
        $this->attributes['mail'] = $email;
107
108
        $this->attributes['motto'] = Config::get('chocolatey.motto');
109
        $this->attributes['look'] = Config::get('chocolatey.figure');
110
        $this->attributes['auth_ticket'] = '';
111
112
        $this->attributes['password'] = hash(Config::get('chocolatey.security.hash'), openssl_random_pseudo_bytes(50));
113
        $this->attributes['account_created'] = time();
114
115
        $this->attributes['ip_current'] = $address;
116
117
        $this->traits = $newUser ? ['NEW_USER', 'USER'] : ['USER'];
118
119
        $this->save();
120
        $this->createData();
121
122
        return $this;
123
    }
124
125
    /**
126
     * Store an User Alias Set on Database.
127
     */
128
    public function createData()
129
    {
130
        (new UserPreferences)->store($this->attributes['id']);
131
    }
132
133
    /**
134
     * Get Is User is Banned.
135
     *
136
     * @return bool
137
     */
138
    public function getIsBannedAttribute(): bool
139
    {
140
        return Ban::where('user_id', $this->attributes['id'])->first() !== null;
141
    }
142
143
    /**
144
     * Check if Is Staff.
145
     *
146
     * @return bool
147
     */
148
    public function getIsStaffAttribute(): bool
149
    {
150
        return array_key_exists('rank', $this->attributes) ? $this->attributes['rank'] >= 5 : false;
151
    }
152
153
    /**
154
     * Get Ban Details.
155
     *
156
     * @return Ban
157
     */
158
    public function getBanDetailsAttribute()
159
    {
160
        return Ban::where('user_id', $this->attributes['id'])->first();
161
    }
162
163
    /**
164
     * Get Current User Country.
165
     *
166
     * @TODO: Implement this in a proper way
167
     *
168
     * @return string
169
     */
170
    public function getCountryAttribute(): string
171
    {
172
        return 'com';
173
    }
174
175
    /**
176
     * Set the Trait Attribute.
177
     *
178
     * @param array $accountType
179
     */
180
    public function setTraitsAttribute(array $accountType)
181
    {
182
        $this->traits = $accountType;
183
    }
184
185
    /**
186
     * What is this field?
187
     *
188
     * @return array
189
     */
190
    public function getTraitsAttribute(): array
191
    {
192
        if (array_key_exists('rank', $this->attributes) && $this->attributes['rank'] >= 6) {
193
            return array('STAFF');
194
        }
195
196
        return $this->traits;
197
    }
198
199
    /**
200
     * We don't care about this?
201
     *
202
     * @return bool
203
     */
204
    public function getTrustedAttribute(): bool
205
    {
206
        if (UserSecurity::find($this->attributes['id']) == null) {
207
            return true;
208
        }
209
210
        return in_array($this->attributes['ip_current'],
211
            UserSecurity::find($this->attributes['id'])->trustedDevices);
212
    }
213
214
    /**
215
     * What is this field?
216
     *
217
     * @return string
218
     */
219
    public function getIdentityTypeAttribute(): string
220
    {
221
        return 'HABBO';
222
    }
223
224
    /**
225
     * We don't care about this, every user is trusted.
226
     *
227
     * @return bool
228
     */
229
    public function getIdentityVerifiedAttribute(): bool
230
    {
231
        return true;
232
    }
233
234
    /**
235
     * We don't care about this.
236
     *
237
     * @return int
238
     */
239
    public function getLoginLogIdAttribute(): int
240
    {
241
        return 1;
242
    }
243
244
    /**
245
     * We don't care about this.
246
     *
247
     * @return int
248
     */
249
    public function getSessionLoginIdAttribute(): int
250
    {
251
        return 1;
252
    }
253
254
    /**
255
     * Get the HabboClub Attribute
256
     * In a Retro Habbo everyone is HC, yeah?
257
     *
258
     * @WARNING: This is used for Advertisement
259
     *
260
     * @return bool
261
     */
262
    public function getHabboClubMemberAttribute(): bool
263
    {
264
        return Config::get('chocolatey.ads.enabled') == false;
265
    }
266
267
    /**
268
     * Get the Builders Club Attribute
269
     * In a Retro Habbo everyone is BC, yeah?
270
     *
271
     * @WARNING: This is used for Advertisement
272
     *
273
     * @return bool
274
     */
275
    public function getBuildersClubMemberAttribute(): bool
276
    {
277
        return Config::get('chocolatey.ads.enabled') == false;
278
    }
279
280
    /**
281
     * Get GTimestamp in Habbo UserCurrency.
282
     *
283
     * @return string
284
     */
285
    public function getAccountCreatedAttribute(): string
286
    {
287
        $accountCreated = $this->attributes['account_created'] ?? time();
288
289
        return date('Y-m-d', $accountCreated) . 'T' . date('H:i:s.ZZZZ+ZZZZ', $accountCreated);
290
    }
291
292
    /**
293
     * Get GTimestamp in Habbo UserCurrency.
294
     *
295
     * @return string
296
     */
297
    public function getMemberSinceAttribute(): string
298
    {
299
        $accountCreated = $this->attributes['account_created'] ?? time();
300
301
        return date('Y-m-d', $accountCreated) . 'T' . date('H:i:s.ZZZZ+ZZZZ', $accountCreated);
302
    }
303
304
    /**
305
     * Retrieve User Figure String.
306
     *
307
     * @return string
308
     */
309
    public function getFigureStringAttribute(): string
310
    {
311
        return $this->attributes['look'] ?? 'hr-115-42.hd-195-19.ch-3030-82.lg-275-1408.fa-1201.ca-1804-64';
312
    }
313
314
    /**
315
     * Get GTimestamp in Habbo UserCurrency.
316
     *
317
     * @return false|string
318
     */
319
    public function getLastLoginAttribute(): string
320
    {
321
        $lastLogin = $this->attributes['last_login'] ?? time();
322
323
        return date('Y-m-d', $lastLogin) . 'T' . date('H:i:s.ZZZZ+ZZZZ', $lastLogin);
324
    }
325
326
    /**
327
     * Get E-Mail Verified Attribute.
328
     *
329
     * @return bool
330
     */
331
    public function getEmailVerifiedAttribute(): bool
332
    {
333
        return $this->getChocolateyId()->mail_verified;
334
    }
335
336
    /**
337
     * Get Account Chocolatey Id
338
     *
339
     * @return ChocolateyId
340
     */
341
    public function getChocolateyId(): ChocolateyId
342
    {
343
        return ChocolateyId::find($this->attributes['mail']);
344
    }
345
}
346