RoleRepository.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace App\Shop\Roles\Repositories;
  3. use Jsdecena\Baserepo\BaseRepository;
  4. use App\Shop\Permissions\Permission;
  5. use App\Shop\Roles\Exceptions\CreateRoleErrorException;
  6. use App\Shop\Roles\Exceptions\DeleteRoleErrorException;
  7. use App\Shop\Roles\Exceptions\RoleNotFoundErrorException;
  8. use App\Shop\Roles\Exceptions\UpdateRoleErrorException;
  9. use App\Shop\Roles\Role;
  10. use Illuminate\Database\QueryException;
  11. use Illuminate\Support\Collection;
  12. class RoleRepository extends BaseRepository implements RoleRepositoryInterface
  13. {
  14. /**
  15. * @var Role
  16. */
  17. protected $model;
  18. /**
  19. * RoleRepository constructor.
  20. * @param Role $role
  21. */
  22. public function __construct(Role $role)
  23. {
  24. parent::__construct($role);
  25. $this->model = $role;
  26. }
  27. /**
  28. * List all Roles
  29. *
  30. * @param string $order
  31. * @param string $sort
  32. * @return Collection
  33. */
  34. public function listRoles(string $order = 'id', string $sort = 'desc') : Collection
  35. {
  36. return $this->all(['*'], $order, $sort);
  37. }
  38. /**
  39. * @param array $data
  40. * @return Role
  41. * @throws CreateRoleErrorException
  42. */
  43. public function createRole(array $data) : Role
  44. {
  45. try {
  46. $role = new Role($data);
  47. $role->save();
  48. return $role;
  49. } catch (QueryException $e) {
  50. throw new CreateRoleErrorException($e);
  51. }
  52. }
  53. /**
  54. * @param int $id
  55. *
  56. * @return Role
  57. * @throws RoleNotFoundErrorException
  58. */
  59. public function findRoleById(int $id) : Role
  60. {
  61. try {
  62. return $this->findOneOrFail($id);
  63. } catch (QueryException $e) {
  64. throw new RoleNotFoundErrorException($e);
  65. }
  66. }
  67. /**
  68. * @param array $data
  69. *
  70. * @return bool
  71. * @throws UpdateRoleErrorException
  72. */
  73. public function updateRole(array $data) : bool
  74. {
  75. try {
  76. return $this->update($data);
  77. } catch (QueryException $e) {
  78. throw new UpdateRoleErrorException($e);
  79. }
  80. }
  81. /**
  82. * @return bool
  83. * @throws DeleteRoleErrorException
  84. */
  85. public function deleteRoleById() : bool
  86. {
  87. try {
  88. return $this->delete();
  89. } catch (QueryException $e) {
  90. throw new DeleteRoleErrorException($e);
  91. }
  92. }
  93. /**
  94. * @param Permission $permission
  95. */
  96. public function attachToPermission(Permission $permission)
  97. {
  98. $this->model->attachPermission($permission);
  99. }
  100. /**
  101. * @param Permission ...$permissions
  102. */
  103. public function attachToPermissions(... $permissions)
  104. {
  105. $this->model->attachPermissions($permissions);
  106. }
  107. /**
  108. * @param array $ids
  109. */
  110. public function syncPermissions(array $ids)
  111. {
  112. $this->model->syncPermissions($ids);
  113. }
  114. /**
  115. * @return Collection
  116. */
  117. public function listPermissions() : Collection
  118. {
  119. return $this->model->permissions()->get();
  120. }
  121. }