zalazdi

Untitled

Mar 8th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.26 KB | None | 0 0
  1. <?PHP
  2.  
  3. namespace App\Repositories;
  4.  
  5. use App\Contracts\WorkerContract;
  6. use App\Entities\User;
  7. use Carbon\Carbon;
  8.  
  9. class WorkerRepository implements WorkerContract
  10. {
  11.     /**
  12.     * User Entity container
  13.      *
  14.     * @var User
  15.     */
  16.     protected $user;
  17.  
  18.     /**
  19.      * User Entity construct
  20.      *
  21.      * @param User $user
  22.      */
  23.     function __construct(User $user)
  24.     {
  25.         $this->user = $user;
  26.     }
  27.  
  28.     /**
  29.      * Return User model
  30.      *
  31.      * @return User
  32.      */
  33.     public function entity()
  34.     {
  35.         return $this->user->worker()->with(['roles', 'tasks']);
  36.     }
  37.  
  38.     /**
  39.      * Find all users, and return collection
  40.      *
  41.      * @return \Illuminate\Database\Eloquent\Collection
  42.      */
  43.     public function findAll()
  44.     {
  45.         return $this->entity()
  46.             ->get();
  47.     }
  48.  
  49.     /**
  50.      * Find all current workers
  51.      *
  52.      * @return \Illuminate\Database\Eloquent\Collection
  53.      */
  54.     public function findCurrent()
  55.     {
  56.         return $this->entity()
  57.             ->whereNull('dismissed_at')
  58.             ->get();
  59.     }
  60.  
  61.     /**
  62.      * Find all dismissed workers
  63.      *
  64.      * @return \Illuminate\Database\Eloquent\Collection
  65.      */
  66.     public function findDismissed()
  67.     {
  68.         return $this->entity()
  69.             ->whereNotNull('dismissed_at')
  70.             ->get();
  71.     }
  72.  
  73.     /**
  74.      * Find all banned workers (soft deleted)
  75.      *
  76.      * @return \Illuminate\Database\Eloquent\Collection
  77.      */
  78.     public function findBanned()
  79.     {
  80.         return $this->entity()
  81.             ->onlyTrashed()
  82.             ->get();
  83.     }
  84.  
  85.     /**
  86.      * Find all users with given first name and/or last name and return collection
  87.      *
  88.      * @param string $name
  89.      * @return \Illuminate\Database\Eloquent\Collection
  90.      */
  91.     public function findByName($name)
  92.     {
  93.         $search = explode(' ', $name);
  94.         return $this->entity()
  95.             ->whereIn('first_name', $search)
  96.             ->orWhereIn('last_name', $search)
  97.             ->orWhereIn('nickname', $search)
  98.             ->withTrashed()
  99.             ->get();
  100.     }
  101.  
  102.     /**
  103.      * Find specific user based on given ID
  104.      *
  105.      * @param integer $id
  106.      * @return User
  107.      */
  108.     public function findById($id)
  109.     {
  110.         return $this->entity()
  111.             ->whereId($id)
  112.             ->withTrashed()
  113.             ->first();
  114.     }
  115.  
  116.     /**
  117.      * Find specific user based on given email
  118.      *
  119.      * @param string $email
  120.      * @return User
  121.      */
  122.     public function findByEmail($email)
  123.     {
  124.         return $this->entity()
  125.             ->whereEmail($email)
  126.             ->withTrashed()
  127.             ->first();
  128.     }
  129.  
  130.     /**
  131.      * Create new user and save to database
  132.      *
  133.      * @param array $data
  134.      * @return User|false
  135.      */
  136.     public function create($data = [])
  137.     {
  138.         return $this->user->create($data);
  139.     }
  140.  
  141.     /**
  142.      * Update given user data
  143.      *
  144.      * @param User $worker
  145.      * @param array $data
  146.      * @return boolean
  147.      */
  148.     public function update(User $worker, $data = [])
  149.     {
  150.         return $worker->update($data);
  151.     }
  152.  
  153.     /**
  154.      * Hire new or former worker
  155.      *
  156.      * @param User $worker
  157.      * @return boolean
  158.      */
  159.     public function hire(User $worker)
  160.     {
  161.         $worker->dismissed_at = null;
  162.         return $worker->save();
  163.     }
  164.  
  165.     /**
  166.      * Dismiss worker
  167.      *
  168.      * @param User $worker
  169.      * @return boolean
  170.      */
  171.     public function dismiss(User $worker)
  172.     {
  173.         $worker->dismissed_at = Carbon::now();
  174.         return $worker->save();
  175.     }
  176.  
  177.     /**
  178.      * Soft delete specific user
  179.      *
  180.      * @param User $worker
  181.      * @return boolean
  182.      */
  183.     public function archive(User $worker)
  184.     {
  185.         return $worker->delete();
  186.     }
  187.  
  188.     /**
  189.      * Restore soft deleted user
  190.      *
  191.      * @param User $worker
  192.      * @return boolean
  193.      */
  194.     public function restore(User $worker)
  195.     {
  196.         return $worker->restore();
  197.     }
  198.  
  199.     /**
  200.      * Complete remove specific user from database
  201.      *
  202.      * @param User $worker
  203.      * @return void
  204.      */
  205.     public function forceDelete(User $worker)
  206.     {
  207.         $worker->tasks()->detach();
  208.         $worker->forceDelete();
  209.     }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment