CourierRepository.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace App\Shop\Couriers\Repositories;
  3. use Jsdecena\Baserepo\BaseRepository;
  4. use App\Shop\Couriers\Courier;
  5. use App\Shop\Couriers\Exceptions\CourierInvalidArgumentException;
  6. use App\Shop\Couriers\Exceptions\CourierNotFoundException;
  7. use App\Shop\Couriers\Repositories\Interfaces\CourierRepositoryInterface;
  8. use Illuminate\Database\Eloquent\ModelNotFoundException;
  9. use Illuminate\Database\QueryException;
  10. use Illuminate\Support\Collection;
  11. class CourierRepository extends BaseRepository implements CourierRepositoryInterface
  12. {
  13. /**
  14. * CourierRepository constructor.
  15. * @param Courier $courier
  16. */
  17. public function __construct(Courier $courier)
  18. {
  19. parent::__construct($courier);
  20. $this->model = $courier;
  21. }
  22. /**
  23. * Create the courier
  24. *
  25. * @param array $params
  26. * @return Courier
  27. * @throws CourierInvalidArgumentException
  28. */
  29. public function createCourier(array $params) : Courier
  30. {
  31. try {
  32. return $this->create($params);
  33. } catch (QueryException $e) {
  34. throw new CourierInvalidArgumentException($e->getMessage());
  35. }
  36. }
  37. /**
  38. * Update the courier
  39. *
  40. * @param array $params
  41. *
  42. * @return bool
  43. * @throws CourierInvalidArgumentException
  44. */
  45. public function updateCourier(array $params) : bool
  46. {
  47. try {
  48. return $this->update($params);
  49. } catch (QueryException $e) {
  50. throw new CourierInvalidArgumentException($e->getMessage());
  51. }
  52. }
  53. /**
  54. * Return the courier
  55. *
  56. * @param int $id
  57. *
  58. * @return Courier
  59. * @throws CourierNotFoundException
  60. */
  61. public function findCourierById(int $id) : Courier
  62. {
  63. try {
  64. return $this->findOneOrFail($id);
  65. } catch (ModelNotFoundException $e) {
  66. throw new CourierNotFoundException('Courier not found.');
  67. }
  68. }
  69. /**
  70. * Return all the couriers
  71. *
  72. * @param string $order
  73. * @param string $sort
  74. * @return Collection|mixed
  75. */
  76. public function listCouriers(string $order = 'id', string $sort = 'desc') : Collection
  77. {
  78. return $this->all(['*'], $order, $sort);
  79. }
  80. /**
  81. * @return bool
  82. * @throws \Exception
  83. */
  84. public function deleteCourier()
  85. {
  86. return $this->delete();
  87. }
  88. }