PermissionRepository.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace App\Shop\Permissions\Repositories;
  3. use Jsdecena\Baserepo\BaseRepository;
  4. use App\Shop\Permissions\Exceptions\CreatePermissionErrorException;
  5. use App\Shop\Permissions\Exceptions\DeletePermissionErrorException;
  6. use App\Shop\Permissions\Exceptions\PermissionNotFoundErrorException;
  7. use App\Shop\Permissions\Exceptions\UpdatePermissionErrorException;
  8. use App\Shop\Permissions\Permission;
  9. use App\Shop\Permissions\Repositories\Interfaces\PermissionRepositoryInterface;
  10. use Illuminate\Database\Eloquent\ModelNotFoundException;
  11. use Illuminate\Database\QueryException;
  12. use Illuminate\Support\Collection;
  13. class PermissionRepository extends BaseRepository implements PermissionRepositoryInterface
  14. {
  15. /**
  16. * PermissionRepository constructor.
  17. *
  18. * @param Permission $permission
  19. */
  20. public function __construct(Permission $permission)
  21. {
  22. parent::__construct($permission);
  23. $this->model = $permission;
  24. }
  25. /**
  26. * @param array $data
  27. *
  28. * @return Permission
  29. * @throws CreatePermissionErrorException
  30. */
  31. public function createPermission(array $data) : Permission
  32. {
  33. try {
  34. return $this->create($data);
  35. } catch (QueryException $e) {
  36. throw new CreatePermissionErrorException($e);
  37. }
  38. }
  39. /**
  40. * @param int $id
  41. *
  42. * @return Permission
  43. * @throws PermissionNotFoundErrorException
  44. */
  45. public function findPermissionById(int $id) : Permission
  46. {
  47. try {
  48. return $this->findOneOrFail($id);
  49. } catch (ModelNotFoundException $e) {
  50. throw new PermissionNotFoundErrorException($e);
  51. }
  52. }
  53. /**
  54. * @param array $data
  55. *
  56. * @return bool
  57. * @throws UpdatePermissionErrorException
  58. */
  59. public function updatePermission(array $data) : bool
  60. {
  61. try {
  62. return $this->update($data);
  63. } catch (QueryException $e) {
  64. throw new UpdatePermissionErrorException($e);
  65. }
  66. }
  67. /**
  68. * @return bool
  69. * @throws DeletePermissionErrorException
  70. */
  71. public function deletePermissionById() : bool
  72. {
  73. try {
  74. return $this->delete();
  75. } catch (QueryException $e) {
  76. throw new DeletePermissionErrorException($e);
  77. }
  78. }
  79. /**
  80. * @param array $columns
  81. * @param string $orderBy
  82. * @param string $sortBy
  83. *
  84. * @return Collection
  85. */
  86. public function listPermissions($columns = array('*'), string $orderBy = 'id', string $sortBy = 'asc') : Collection
  87. {
  88. return $this->all($columns, $orderBy, $sortBy);
  89. }
  90. }