Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?PHP
- namespace App\Repositories;
- use App\Contracts\WorkerContract;
- use App\Entities\User;
- use Carbon\Carbon;
- class WorkerRepository implements WorkerContract
- {
- /**
- * User Entity container
- *
- * @var User
- */
- protected $user;
- /**
- * User Entity construct
- *
- * @param User $user
- */
- function __construct(User $user)
- {
- $this->user = $user;
- }
- /**
- * Return User model
- *
- * @return User
- */
- public function entity()
- {
- return $this->user->worker()->with(['roles', 'tasks']);
- }
- /**
- * Find all users, and return collection
- *
- * @return \Illuminate\Database\Eloquent\Collection
- */
- public function findAll()
- {
- return $this->entity()
- ->get();
- }
- /**
- * Find all current workers
- *
- * @return \Illuminate\Database\Eloquent\Collection
- */
- public function findCurrent()
- {
- return $this->entity()
- ->whereNull('dismissed_at')
- ->get();
- }
- /**
- * Find all dismissed workers
- *
- * @return \Illuminate\Database\Eloquent\Collection
- */
- public function findDismissed()
- {
- return $this->entity()
- ->whereNotNull('dismissed_at')
- ->get();
- }
- /**
- * Find all banned workers (soft deleted)
- *
- * @return \Illuminate\Database\Eloquent\Collection
- */
- public function findBanned()
- {
- return $this->entity()
- ->onlyTrashed()
- ->get();
- }
- /**
- * Find all users with given first name and/or last name and return collection
- *
- * @param string $name
- * @return \Illuminate\Database\Eloquent\Collection
- */
- public function findByName($name)
- {
- $search = explode(' ', $name);
- return $this->entity()
- ->whereIn('first_name', $search)
- ->orWhereIn('last_name', $search)
- ->orWhereIn('nickname', $search)
- ->withTrashed()
- ->get();
- }
- /**
- * Find specific user based on given ID
- *
- * @param integer $id
- * @return User
- */
- public function findById($id)
- {
- return $this->entity()
- ->whereId($id)
- ->withTrashed()
- ->first();
- }
- /**
- * Find specific user based on given email
- *
- * @param string $email
- * @return User
- */
- public function findByEmail($email)
- {
- return $this->entity()
- ->whereEmail($email)
- ->withTrashed()
- ->first();
- }
- /**
- * Create new user and save to database
- *
- * @param array $data
- * @return User|false
- */
- public function create($data = [])
- {
- return $this->user->create($data);
- }
- /**
- * Update given user data
- *
- * @param User $worker
- * @param array $data
- * @return boolean
- */
- public function update(User $worker, $data = [])
- {
- return $worker->update($data);
- }
- /**
- * Hire new or former worker
- *
- * @param User $worker
- * @return boolean
- */
- public function hire(User $worker)
- {
- $worker->dismissed_at = null;
- return $worker->save();
- }
- /**
- * Dismiss worker
- *
- * @param User $worker
- * @return boolean
- */
- public function dismiss(User $worker)
- {
- $worker->dismissed_at = Carbon::now();
- return $worker->save();
- }
- /**
- * Soft delete specific user
- *
- * @param User $worker
- * @return boolean
- */
- public function archive(User $worker)
- {
- return $worker->delete();
- }
- /**
- * Restore soft deleted user
- *
- * @param User $worker
- * @return boolean
- */
- public function restore(User $worker)
- {
- return $worker->restore();
- }
- /**
- * Complete remove specific user from database
- *
- * @param User $worker
- * @return void
- */
- public function forceDelete(User $worker)
- {
- $worker->tasks()->detach();
- $worker->forceDelete();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment