CityRepository.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace App\Shop\Cities\Repositories;
  3. use Jsdecena\Baserepo\BaseRepository;
  4. use App\Shop\Cities\Exceptions\CityNotFoundException;
  5. use App\Shop\Cities\Repositories\Interfaces\CityRepositoryInterface;
  6. use Illuminate\Database\Eloquent\ModelNotFoundException;
  7. use App\Shop\Cities\City;
  8. use Illuminate\Support\Collection;
  9. class CityRepository extends BaseRepository implements CityRepositoryInterface
  10. {
  11. /**
  12. * CityRepository constructor.
  13. *
  14. * @param City $city
  15. */
  16. public function __construct(City $city)
  17. {
  18. parent::__construct($city);
  19. $this->model = $city;
  20. }
  21. /**
  22. * @param array $columns
  23. * @param string $orderBy
  24. * @param string $sortBy
  25. *
  26. * @return mixed
  27. */
  28. public function listCities($columns = ['*'], string $orderBy = 'name', string $sortBy = 'asc')
  29. {
  30. return $this->all($columns, $orderBy, $sortBy);
  31. }
  32. /**
  33. * @param int $id
  34. * @return City
  35. * @throws CityNotFoundException
  36. *
  37. * @deprecated @findCityByName
  38. */
  39. public function findCityById(int $id) : City
  40. {
  41. try {
  42. return $this->findOneOrFail($id);
  43. } catch (ModelNotFoundException $e) {
  44. throw new CityNotFoundException('City not found.');
  45. }
  46. }
  47. /**
  48. * @param array $params
  49. *
  50. * @return boolean
  51. */
  52. public function updateCity(array $params) : bool
  53. {
  54. $this->model->update($params);
  55. return $this->model->save();
  56. }
  57. /**
  58. * @param string $state_code
  59. *
  60. * @return Collection
  61. */
  62. public function listCitiesByStateCode(string $state_code) : Collection
  63. {
  64. return $this->model->where(compact('state_code'))->get();
  65. }
  66. /**
  67. * @param string $name
  68. *
  69. * @return mixed
  70. * @throws CityNotFoundException
  71. */
  72. public function findCityByName(string $name) : City
  73. {
  74. try {
  75. return $this->model->where(compact('name'))->firstOrFail();
  76. } catch (ModelNotFoundException $e) {
  77. throw new CityNotFoundException('City not found.');
  78. }
  79. }
  80. }