EmployeeRepository.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace App\Shop\Employees\Repositories;
  3. use Jsdecena\Baserepo\BaseRepository;
  4. use App\Shop\Employees\Employee;
  5. use App\Shop\Employees\Exceptions\EmployeeNotFoundException;
  6. use App\Shop\Employees\Repositories\Interfaces\EmployeeRepositoryInterface;
  7. use Illuminate\Database\Eloquent\ModelNotFoundException;
  8. use Illuminate\Support\Collection;
  9. use Illuminate\Support\Facades\Auth;
  10. use Illuminate\Support\Facades\Hash;
  11. class EmployeeRepository extends BaseRepository implements EmployeeRepositoryInterface
  12. {
  13. /**
  14. * EmployeeRepository constructor.
  15. *
  16. * @param Employee $employee
  17. */
  18. public function __construct(Employee $employee)
  19. {
  20. parent::__construct($employee);
  21. $this->model = $employee;
  22. }
  23. /**
  24. * List all the employees
  25. *
  26. * @param string $order
  27. * @param string $sort
  28. *
  29. * @return Collection
  30. */
  31. public function listEmployees(string $order = 'id', string $sort = 'desc'): Collection
  32. {
  33. return $this->all(['*'], $order, $sort);
  34. }
  35. /**
  36. * Create the employee
  37. *
  38. * @param array $data
  39. *
  40. * @return Employee
  41. */
  42. public function createEmployee(array $data): Employee
  43. {
  44. $data['password'] = Hash::make($data['password']);
  45. return $this->create($data);
  46. }
  47. /**
  48. * Find the employee by id
  49. *
  50. * @param int $id
  51. *
  52. * @return Employee
  53. */
  54. public function findEmployeeById(int $id): Employee
  55. {
  56. try {
  57. return $this->findOneOrFail($id);
  58. } catch (ModelNotFoundException $e) {
  59. throw new EmployeeNotFoundException;
  60. }
  61. }
  62. /**
  63. * Update employee
  64. *
  65. * @param array $params
  66. *
  67. * @return bool
  68. */
  69. public function updateEmployee(array $params): bool
  70. {
  71. if (isset($params['password'])) {
  72. $params['password'] = Hash::make($params['password']);
  73. }
  74. return $this->update($params);
  75. }
  76. /**
  77. * @param array $roleIds
  78. */
  79. public function syncRoles(array $roleIds)
  80. {
  81. $this->model->roles()->sync($roleIds);
  82. }
  83. /**
  84. * @return Collection
  85. */
  86. public function listRoles(): Collection
  87. {
  88. return $this->model->roles()->get();
  89. }
  90. /**
  91. * @param string $roleName
  92. *
  93. * @return bool
  94. */
  95. public function hasRole(string $roleName): bool
  96. {
  97. return $this->model->hasRole($roleName);
  98. }
  99. /**
  100. * @param Employee $employee
  101. *
  102. * @return bool
  103. */
  104. public function isAuthUser(Employee $employee): bool
  105. {
  106. $isAuthUser = false;
  107. if (Auth::guard('employee')->user()->id == $employee->id) {
  108. $isAuthUser = true;
  109. }
  110. return $isAuthUser;
  111. }
  112. /**
  113. * @return bool
  114. * @throws \Exception
  115. */
  116. public function deleteEmployee() : bool
  117. {
  118. return $this->delete();
  119. }
  120. }