Browse Source

Add docblocks for user related models and classes

pull/18/head
Nafies Luthfi 7 years ago
parent
commit
e9ae375f61
  1. 29
      app/Entities/Users/Event.php
  2. 24
      app/Entities/Users/Role.php
  3. 60
      app/Entities/Users/User.php
  4. 30
      app/Entities/Users/UserRole.php

29
app/Entities/Users/Event.php

@ -6,19 +6,48 @@ use App\Entities\Projects\Project;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
/** /**
* User event model.
*
* @author Nafies Luthfi <nafiesL@gmail.com> * @author Nafies Luthfi <nafiesL@gmail.com>
*/ */
class Event extends Model class Event extends Model
{ {
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'user_events'; protected $table = 'user_events';
/**
* The attributes that aren't mass assignable.
*
* @var array
*/
protected $guarded = ['id', 'created_at', 'updated_at']; protected $guarded = ['id', 'created_at', 'updated_at'];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = ['is_allday' => 'boolean']; protected $casts = ['is_allday' => 'boolean'];
/**
* Event belongs to User relation.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user() public function user()
{ {
return $this->belongsTo(User::class); return $this->belongsTo(User::class);
} }
/**
* Event belongs to Project relation.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function project() public function project()
{ {
return $this->belongsTo(Project::class); return $this->belongsTo(Project::class);

24
app/Entities/Users/Role.php

@ -5,25 +5,47 @@ namespace App\Entities\Users;
use App\Entities\ReferenceAbstract; use App\Entities\ReferenceAbstract;
/** /**
* Role Class.
* Role reference class.
*/ */
class Role extends ReferenceAbstract class Role extends ReferenceAbstract
{ {
/**
* List of user role.
*
* @var array
*/
protected static $lists = [ protected static $lists = [
1 => 'admin', 1 => 'admin',
2 => 'worker', 2 => 'worker',
]; ];
/**
* Get role name by id.
*
* @param int $singleId
* @return string
*/
public static function getNameById($roleId) public static function getNameById($roleId)
{ {
return trans('user.roles.'.static::$lists[$roleId]); return trans('user.roles.'.static::$lists[$roleId]);
} }
/**
* Get role id by name.
*
* @param string $roleName
* @return string
*/
public static function getIdByName($roleName) public static function getIdByName($roleName)
{ {
return array_search($roleName, static::$lists); return array_search($roleName, static::$lists);
} }
/**
* Get user roles in array format.
*
* @return array
*/
public static function toArray() public static function toArray()
{ {
$lists = []; $lists = [];

60
app/Entities/Users/User.php

@ -9,14 +9,35 @@ class User extends Authenticatable
{ {
use Notifiable; use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'email', 'password', 'api_token', 'lang']; protected $fillable = ['name', 'email', 'password', 'api_token', 'lang'];
/**
* The attributes that should be hidden for serialization.
*
* @var array
*/
protected $hidden = ['password', 'remember_token', 'api_token']; protected $hidden = ['password', 'remember_token', 'api_token'];
/**
* Set user password attribute on save.
*
* @param void
*/
public function setPasswordAttribute($value) public function setPasswordAttribute($value)
{ {
$this->attributes['password'] = bcrypt($value); $this->attributes['password'] = bcrypt($value);
} }
/**
* Show user name with link to user detail.
*
* @return Illuminate\Support\HtmlString
*/
public function nameLink() public function nameLink()
{ {
return link_to_route('users.show', $this->name, [$this]); return link_to_route('users.show', $this->name, [$this]);
@ -35,8 +56,7 @@ class User extends Authenticatable
/** /**
* Assign the given role to the user. * Assign the given role to the user.
* *
* @param string $role
*
* @param string $roleName
* @return void * @return void
*/ */
public function assignRole(string $roleName) public function assignRole(string $roleName)
@ -52,8 +72,7 @@ class User extends Authenticatable
/** /**
* Remove the given role from the user. * Remove the given role from the user.
* *
* @param string $role
*
* @param string $roleName
* @return void * @return void
*/ */
public function removeRole(string $roleName) public function removeRole(string $roleName)
@ -69,8 +88,7 @@ class User extends Authenticatable
/** /**
* Determine if the user has the given role. * Determine if the user has the given role.
* *
* @param string $role
*
* @param string $roleName
* @return bool * @return bool
*/ */
public function hasRole(string $roleName) public function hasRole(string $roleName)
@ -83,8 +101,7 @@ class User extends Authenticatable
/** /**
* Determine if the user has the given array of role. * Determine if the user has the given array of role.
* *
* @param array $role
*
* @param array $roleNameArray
* @return bool * @return bool
*/ */
public function hasRoles(array $roleNameArray) public function hasRoles(array $roleNameArray)
@ -101,6 +118,13 @@ class User extends Authenticatable
}); });
} }
/**
* User query scope based on role names.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param array $roleNameArray
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeHasRoles($query, array $roleNameArray) public function scopeHasRoles($query, array $roleNameArray)
{ {
return $query->whereHas('roles', function ($q) use ($roleNameArray) { return $query->whereHas('roles', function ($q) use ($roleNameArray) {
@ -114,6 +138,11 @@ class User extends Authenticatable
}); });
} }
/**
* List of user roles in html list items.
*
* @return string
*/
public function roleList() public function roleList()
{ {
$roleList = '<ul>'; $roleList = '<ul>';
@ -125,11 +154,21 @@ class User extends Authenticatable
return $roleList; return $roleList;
} }
/**
* User has many Jobs relation.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function jobs() public function jobs()
{ {
return $this->hasMany('App\Entities\Projects\Job', 'worker_id'); return $this->hasMany('App\Entities\Projects\Job', 'worker_id');
} }
/**
* User belongs to many Projects based on Job assignments.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function projects() public function projects()
{ {
return $this->belongsToMany('App\Entities\Projects\Project', 'jobs', 'worker_id') return $this->belongsToMany('App\Entities\Projects\Project', 'jobs', 'worker_id')
@ -137,6 +176,11 @@ class User extends Authenticatable
->groupBy('project_id'); ->groupBy('project_id');
} }
/**
* User has many Payment as fee.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function payments() public function payments()
{ {
return $this->morphMany('App\Entities\Payments\Payment', 'partner'); return $this->morphMany('App\Entities\Payments\Payment', 'partner');

30
app/Entities/Users/UserRole.php

@ -5,15 +5,45 @@ namespace App\Entities\Users;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
/** /**
* User Role model.
*
* @author Nafies Luthfi <nafiesL@gmail.com> * @author Nafies Luthfi <nafiesL@gmail.com>
*/ */
class UserRole extends Model class UserRole extends Model
{ {
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'user_roles'; protected $table = 'user_roles';
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = false; public $timestamps = false;
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = ['name']; protected $appends = ['name'];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['user_id', 'role_id']; protected $fillable = ['user_id', 'role_id'];
/**
* Role name attribute.
*
* @return string
*/
public function getNameAttribute() public function getNameAttribute()
{ {
return Role::getNameById($this->role_id); return Role::getNameById($this->role_id);

Loading…
Cancel
Save