ProvinceRepository.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace App\Shop\Provinces\Repositories;
  3. use Jsdecena\Baserepo\BaseRepository;
  4. use App\Shop\Countries\Country;
  5. use App\Shop\Provinces\Exceptions\ProvinceNotFoundException;
  6. use App\Shop\Provinces\Province;
  7. use App\Shop\Provinces\Repositories\Interfaces\ProvinceRepositoryInterface;
  8. use Illuminate\Database\Eloquent\ModelNotFoundException;
  9. use Illuminate\Database\QueryException;
  10. use Illuminate\Support\Collection;
  11. class ProvinceRepository extends BaseRepository implements ProvinceRepositoryInterface
  12. {
  13. /**
  14. * ProvinceRepository constructor.
  15. * @param Province $province
  16. */
  17. public function __construct(Province $province)
  18. {
  19. parent::__construct($province);
  20. }
  21. /**
  22. * List all the provinces
  23. *
  24. * @param string $order
  25. * @param string $sort
  26. * @param array $columns
  27. * @return Collection
  28. */
  29. public function listProvinces(string $order = 'id', string $sort = 'desc', array $columns = ['*']) : Collection
  30. {
  31. return $this->all($columns, $order, $sort);
  32. }
  33. /**
  34. * Find the province
  35. *
  36. * @param int $id
  37. * @return Province
  38. */
  39. public function findProvinceById(int $id) : Province
  40. {
  41. try {
  42. return $this->findOneOrFail($id);
  43. } catch (ModelNotFoundException $e) {
  44. throw new ProvinceNotFoundException($e->getMessage());
  45. }
  46. }
  47. /**
  48. * Update the province
  49. *
  50. * @param array $params
  51. * @return boolean
  52. */
  53. public function updateProvince(array $params) : bool
  54. {
  55. try {
  56. return $this->model->update($params);
  57. } catch (QueryException $e) {
  58. throw new ProvinceNotFoundException($e->getMessage());
  59. }
  60. }
  61. /**
  62. * @param int $provinceId
  63. * @return mixed
  64. */
  65. public function listCities(int $provinceId) : Collection
  66. {
  67. return $this->findProvinceById($provinceId)->cities()->get();
  68. }
  69. /**
  70. * @return Country
  71. */
  72. public function findCountry() : Country
  73. {
  74. return $this->model->country;
  75. }
  76. }