CountryRepository.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace App\Shop\Countries\Repositories;
  3. use Jsdecena\Baserepo\BaseRepository;
  4. use App\Shop\Countries\Exceptions\CountryInvalidArgumentException;
  5. use App\Shop\Countries\Exceptions\CountryNotFoundException;
  6. use App\Shop\Countries\Repositories\Interfaces\CountryRepositoryInterface;
  7. use Illuminate\Database\Eloquent\ModelNotFoundException;
  8. use Illuminate\Database\QueryException;
  9. use App\Shop\Countries\Country;
  10. use Illuminate\Support\Collection;
  11. class CountryRepository extends BaseRepository implements CountryRepositoryInterface
  12. {
  13. /**
  14. * CountryRepository constructor.
  15. * @param Country $country
  16. */
  17. public function __construct(Country $country)
  18. {
  19. parent::__construct($country);
  20. $this->model = $country;
  21. }
  22. /**
  23. * List all the countries
  24. *
  25. * @param string $order
  26. * @param string $sort
  27. * @return Collection
  28. */
  29. public function listCountries(string $order = 'id', string $sort = 'desc') : Collection
  30. {
  31. return $this->model->where('status', 1)->get();
  32. }
  33. /**
  34. * @param array $params
  35. * @return Country
  36. */
  37. public function createCountry(array $params) : Country
  38. {
  39. return $this->create($params);
  40. }
  41. /**
  42. * Find the country
  43. *
  44. * @param $id
  45. * @return Country
  46. * @throws CountryNotFoundException
  47. */
  48. public function findCountryById(int $id) : Country
  49. {
  50. try {
  51. return $this->findOneOrFail($id);
  52. } catch (ModelNotFoundException $e) {
  53. throw new CountryNotFoundException('Country not found.');
  54. }
  55. }
  56. /**
  57. * Show all the provinces
  58. *
  59. * @return mixed
  60. */
  61. public function findProvinces()
  62. {
  63. return $this->model->provinces;
  64. }
  65. /**
  66. * Update the country
  67. *
  68. * @param array $params
  69. *
  70. * @return Country
  71. * @throws CountryNotFoundException
  72. */
  73. public function updateCountry(array $params) : Country
  74. {
  75. try {
  76. $this->model->update($params);
  77. return $this->findCountryById($this->model->id);
  78. } catch (QueryException $e) {
  79. throw new CountryInvalidArgumentException($e->getMessage());
  80. }
  81. }
  82. /**
  83. *
  84. * @return Collection
  85. */
  86. public function listStates() : Collection
  87. {
  88. return $this->model->states()->get();
  89. }
  90. }