Complex classes like User often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use User, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | class User extends Model implements AuthenticatableContract, CanResetPasswordContract, Auditable |
||
41 | { |
||
42 | use Authenticatable, Authorizable, CanResetPassword, SoftDeletes, Sluggable, Notifiable, HasApiTokens, Impersonate, RoleTrait, \OwenIt\Auditing\Auditable; |
||
43 | |||
44 | /** |
||
45 | * The database table used by the model. |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $table = 'users'; |
||
50 | protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
||
51 | /** |
||
52 | * The attributes that are mass assignable. |
||
53 | * |
||
54 | * @var array |
||
55 | */ |
||
56 | protected $guarded = ['id', 'password_confirmation']; |
||
57 | /** |
||
58 | * The attributes excluded from the model's JSON form. |
||
59 | * |
||
60 | * @var array |
||
61 | */ |
||
62 | protected $hidden = ['password', 'remember_token']; |
||
63 | |||
64 | /** |
||
65 | * Boot the model. |
||
66 | * |
||
67 | * @return void |
||
68 | */ |
||
69 | 52 | public static function boot() |
|
98 | |||
99 | /** |
||
100 | * Return the sluggable configuration array for this model. |
||
101 | * |
||
102 | * @return array |
||
103 | */ |
||
104 | 51 | public function sluggable() |
|
112 | |||
113 | /** |
||
114 | * @return string |
||
115 | */ |
||
116 | 8 | public function getRouteKeyName() |
|
117 | { |
||
118 | 8 | return 'slug'; |
|
119 | } |
||
120 | |||
121 | /** |
||
122 | * Add geoData based on IP |
||
123 | */ |
||
124 | 50 | public function addGeoData() |
|
125 | { |
||
126 | 50 | $ip = request()->ip(); |
|
127 | 50 | $location = geoip($ip); |
|
128 | 50 | $country = Country::where('name', '=', $location->country)->first(); |
|
129 | 50 | if (is_null($country)) { |
|
130 | 50 | $this->country_id = config('constants.COUNTRY_ID_DEFAULT'); |
|
131 | 50 | $this->city = "Paris"; |
|
132 | 50 | $this->latitude = 48.858222; |
|
133 | 50 | $this->longitude = 2.2945; |
|
134 | 50 | return; |
|
135 | } |
||
136 | $this->country_id = $country->id; |
||
137 | $this->city = $location['city']; |
||
138 | $this->latitude = $location['lat']; |
||
139 | $this->longitude = $location['lon']; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Confirm the user. |
||
144 | * |
||
145 | * @return void |
||
146 | */ |
||
147 | 1 | public function confirmEmail() |
|
148 | { |
||
149 | 1 | $this->verified = true; |
|
150 | 1 | $this->token = null; |
|
151 | 1 | $this->save(); |
|
152 | 1 | } |
|
153 | |||
154 | /** |
||
155 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
156 | */ |
||
157 | 1 | public function grade() |
|
158 | { |
||
159 | 1 | return $this->belongsTo('App\Grade'); |
|
160 | } |
||
161 | |||
162 | /** |
||
163 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
164 | */ |
||
165 | 1 | public function role() |
|
166 | { |
||
167 | 1 | return $this->belongsTo('App\Role'); |
|
168 | } |
||
169 | |||
170 | /** |
||
171 | * @return \Illuminate\Database\Eloquent\Relations\HasOne |
||
172 | */ |
||
173 | public function settings() |
||
177 | |||
178 | /** |
||
179 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
180 | */ |
||
181 | 17 | public function invites() |
|
182 | { |
||
183 | 17 | return $this->hasMany('App\Invite', 'email', 'email'); |
|
184 | } |
||
185 | |||
186 | /** |
||
187 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
188 | */ |
||
189 | 5 | public function country() |
|
193 | |||
194 | /** |
||
195 | * Get all user's created (owned) tournmanents |
||
196 | * |
||
197 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
198 | */ |
||
199 | 18 | public function tournaments() |
|
200 | { |
||
201 | 18 | return $this->hasMany('App\Tournament'); |
|
202 | } |
||
203 | |||
204 | /** |
||
205 | * Get all deleted user's tournmanents |
||
206 | * |
||
207 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
208 | */ |
||
209 | 17 | public function tournamentsDeleted() |
|
210 | { |
||
211 | 17 | return $this->hasMany('App\Tournament')->onlyTrashed(); |
|
212 | } |
||
213 | |||
214 | /** |
||
215 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
216 | */ |
||
217 | 3 | public function championships() |
|
218 | { |
||
219 | 3 | return $this->belongsToMany(Championship::class, 'competitor') |
|
220 | 3 | ->withTimestamps(); |
|
221 | } |
||
222 | |||
223 | /** |
||
224 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
225 | */ |
||
226 | public function federation() |
||
227 | { |
||
228 | return $this->belongsTo(Federation::class); |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
233 | */ |
||
234 | public function association() |
||
235 | { |
||
236 | return $this->belongsTo(Association::class); |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * A president of federation owns a federation |
||
241 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
242 | */ |
||
243 | 4 | public function federationOwned() |
|
244 | { |
||
245 | 4 | return $this->belongsTo(Federation::class, 'id', 'president_id'); |
|
246 | } |
||
247 | |||
248 | /** |
||
249 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
250 | */ |
||
251 | 5 | public function associationOwned() |
|
252 | { |
||
253 | 5 | return $this->belongsTo(Association::class, 'id', 'president_id'); |
|
254 | } |
||
255 | |||
256 | /** |
||
257 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
258 | */ |
||
259 | 4 | public function clubOwned() |
|
260 | { |
||
261 | 4 | return $this->belongsTo(Club::class, 'id', 'president_id'); |
|
262 | } |
||
263 | |||
264 | |||
265 | /** |
||
266 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
267 | */ |
||
268 | public function club() |
||
269 | { |
||
270 | return $this->belongsTo(Club::class); |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
275 | */ |
||
276 | 2 | public function competitors() // Used to delete competitors when soft deleting |
|
277 | { |
||
278 | 2 | return $this->hasMany(Competitor::class); |
|
279 | } |
||
280 | |||
281 | /** |
||
282 | * Tournament where I have participated as competitor |
||
283 | * @return mixed |
||
284 | */ |
||
285 | 1 | public function myTournaments() |
|
286 | { |
||
287 | 1 | return Tournament::leftJoin('championship', 'championship.tournament_id', '=', 'tournament.id') |
|
288 | 1 | ->leftJoin('competitor', 'competitor.championship_id', '=', 'championship.id') |
|
289 | 1 | ->where('competitor.user_id', '=', $this->id) |
|
290 | 1 | ->select('tournament.*') |
|
291 | 1 | ->distinct(); |
|
292 | } |
||
293 | |||
294 | |||
295 | /** |
||
296 | * @return bool |
||
297 | */ |
||
298 | 2 | public function isDeleted() |
|
299 | { |
||
300 | 2 | return $this->deleted_at != null; |
|
301 | } |
||
302 | |||
303 | /** |
||
304 | * @param $firstname |
||
305 | * @param $lastname |
||
306 | */ |
||
307 | 1 | public function updateUserFullName($firstname, $lastname) |
|
308 | { |
||
309 | 1 | $this->firstname = $firstname; |
|
310 | 1 | $this->lastname = $lastname; |
|
311 | 1 | $this->save(); |
|
312 | 1 | } |
|
313 | |||
314 | /** |
||
315 | * @return Collection |
||
316 | */ |
||
317 | 2 | public function fillSelect() |
|
318 | { |
||
319 | 2 | return User::forUser($this) |
|
320 | 2 | ->pluck('name', 'id') |
|
321 | 2 | ->prepend('-', 0); |
|
322 | } |
||
323 | |||
324 | /** |
||
325 | * Check if a user is registered to a tournament |
||
326 | * @param Tournament $tournament |
||
327 | * @return bool |
||
328 | */ |
||
329 | public function isRegisteredTo(Tournament $tournament) |
||
330 | { |
||
331 | $ids = $tournament->championships->pluck('id'); |
||
332 | $isRegistered = Competitor::where('user_id', $this->id) |
||
333 | ->whereIn('championship_id', $ids) |
||
334 | ->get(); |
||
335 | |||
336 | return sizeof($isRegistered) > 0; |
||
337 | } |
||
338 | |||
339 | /** |
||
340 | * @return bool |
||
341 | */ |
||
342 | public function canImpersonate() |
||
346 | |||
347 | /** |
||
348 | * @return bool |
||
349 | */ |
||
350 | public function canBeImpersonated() |
||
354 | |||
355 | |||
356 | /** |
||
357 | * @return string |
||
358 | */ |
||
359 | public function getFullName() |
||
363 | |||
364 | /** |
||
365 | * @param $query |
||
366 | * @param User $user |
||
367 | * @return Builder |
||
368 | * @throws AuthorizationException |
||
369 | * @throws NotOwningAssociationException |
||
370 | * @throws NotOwningClubException |
||
371 | * @throws NotOwningFederationException |
||
372 | */ |
||
373 | 3 | public function scopeForUser($query, User $user) |
|
374 | { |
||
375 | // If user manage a structure, he will be limited to see entity of this structure |
||
376 | // If user has the role but manage no structure --> AuthorizationException |
||
377 | switch (true) { |
||
378 | 3 | case $user->isSuperAdmin(): |
|
379 | 1 | return $query; |
|
380 | 2 | case $user->isFederationPresident() && $user->federationOwned != null: |
|
381 | return $query->federationPresident($user); |
||
382 | 2 | case $user->isAssociationPresident() && $user->associationOwned: |
|
383 | 1 | return $query->associationPresident($user); |
|
384 | 1 | case $user->isClubPresident() && $user->clubOwned != null: |
|
385 | 1 | return $query->clubPresident($user); |
|
386 | case $user->isFederationPresident() && !$user->federationOwned != null: |
||
387 | throw new NotOwningFederationException(); |
||
388 | case $user->isAssociationPresident() && !$user->associationOwned: |
||
389 | throw new NotOwningAssociationException(); |
||
390 | case $user->isClubPresident() && $user->clubOwned == null: |
||
391 | throw new NotOwningClubException(); |
||
392 | default: |
||
393 | throw new AuthorizationException(); |
||
394 | } |
||
395 | } |
||
396 | |||
397 | public function scopeFederationPresident($query, User $user) |
||
401 | |||
402 | 1 | public function scopeAssociationPresident($query, User $user) |
|
406 | |||
407 | 1 | public function scopeClubPresident($query, User $user) |
|
411 | |||
412 | |||
413 | 29 | public function getAvatarAttribute($avatar) |
|
426 | } |
||
427 |