OrderStatusRepository.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace App\Shop\OrderStatuses\Repositories;
  3. use Jsdecena\Baserepo\BaseRepository;
  4. use App\Shop\OrderStatuses\Exceptions\OrderStatusInvalidArgumentException;
  5. use App\Shop\OrderStatuses\Exceptions\OrderStatusNotFoundException;
  6. use App\Shop\OrderStatuses\OrderStatus;
  7. use App\Shop\OrderStatuses\Repositories\Interfaces\OrderStatusRepositoryInterface;
  8. use Illuminate\Database\Eloquent\ModelNotFoundException;
  9. use Illuminate\Database\QueryException;
  10. use Illuminate\Support\Collection;
  11. class OrderStatusRepository extends BaseRepository implements OrderStatusRepositoryInterface
  12. {
  13. /**
  14. * OrderStatusRepository constructor.
  15. * @param OrderStatus $orderStatus
  16. */
  17. public function __construct(OrderStatus $orderStatus)
  18. {
  19. parent::__construct($orderStatus);
  20. $this->model = $orderStatus;
  21. }
  22. /**
  23. * Create the order status
  24. *
  25. * @param array $params
  26. * @return OrderStatus
  27. * @throws OrderStatusInvalidArgumentException
  28. */
  29. public function createOrderStatus(array $params) : OrderStatus
  30. {
  31. try {
  32. return $this->create($params);
  33. } catch (QueryException $e) {
  34. throw new OrderStatusInvalidArgumentException($e->getMessage());
  35. }
  36. }
  37. /**
  38. * Update the order status
  39. *
  40. * @param array $data
  41. *
  42. * @return bool
  43. * @throws OrderStatusInvalidArgumentException
  44. */
  45. public function updateOrderStatus(array $data) : bool
  46. {
  47. try {
  48. return $this->update($data);
  49. } catch (QueryException $e) {
  50. throw new OrderStatusInvalidArgumentException($e->getMessage());
  51. }
  52. }
  53. /**
  54. * @param int $id
  55. * @return OrderStatus
  56. * @throws OrderStatusNotFoundException
  57. */
  58. public function findOrderStatusById(int $id) : OrderStatus
  59. {
  60. try {
  61. return $this->findOneOrFail($id);
  62. } catch (ModelNotFoundException $e) {
  63. throw new OrderStatusNotFoundException('Order status not found.');
  64. }
  65. }
  66. /**
  67. * @return mixed
  68. */
  69. public function listOrderStatuses()
  70. {
  71. return $this->all();
  72. }
  73. /**
  74. * @return bool
  75. * @throws \Exception
  76. */
  77. public function deleteOrderStatus() : bool
  78. {
  79. return $this->delete();
  80. }
  81. /**
  82. * @return Collection
  83. */
  84. public function findOrders() : Collection
  85. {
  86. return $this->model->orders()->get();
  87. }
  88. /**
  89. * @param string $name
  90. *
  91. * @return mixed
  92. */
  93. public function findByName(string $name)
  94. {
  95. return $this->model->where('name', $name)->first();
  96. }
  97. }