AddressRepository.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace App\Shop\Addresses\Repositories;
  3. use App\Shop\Addresses\Address;
  4. use App\Shop\Addresses\Exceptions\CreateAddressErrorException;
  5. use App\Shop\Addresses\Exceptions\AddressNotFoundException;
  6. use App\Shop\Addresses\Repositories\Interfaces\AddressRepositoryInterface;
  7. use App\Shop\Addresses\Transformations\AddressTransformable;
  8. use App\Shop\Cities\City;
  9. use App\Shop\Countries\Country;
  10. use App\Shop\Customers\Customer;
  11. use App\Shop\Provinces\Province;
  12. use Illuminate\Database\Eloquent\ModelNotFoundException;
  13. use Illuminate\Database\QueryException;
  14. use Illuminate\Support\Collection;
  15. use Jsdecena\Baserepo\BaseRepository;
  16. class AddressRepository extends BaseRepository implements AddressRepositoryInterface
  17. {
  18. use AddressTransformable;
  19. /**
  20. * AddressRepository constructor.
  21. * @param Address $address
  22. */
  23. public function __construct(Address $address)
  24. {
  25. parent::__construct($address);
  26. $this->model = $address;
  27. }
  28. /**
  29. * Create the address
  30. *
  31. * @param array $data
  32. *
  33. * @return Address
  34. * @throws CreateAddressErrorException
  35. */
  36. public function createAddress(array $data) : Address
  37. {
  38. try {
  39. return $this->create($data);
  40. } catch (QueryException $e) {
  41. throw new CreateAddressErrorException('Address creation error');
  42. }
  43. }
  44. /**
  45. * Attach the customer to the address
  46. *
  47. * @param Address $address
  48. * @param Customer $customer
  49. */
  50. public function attachToCustomer(Address $address, Customer $customer)
  51. {
  52. $customer->addresses()->save($address);
  53. }
  54. /**
  55. * @param array $data
  56. * @return bool
  57. */
  58. public function updateAddress(array $data): bool
  59. {
  60. return $this->update($data);
  61. }
  62. /**
  63. * Soft delete the address
  64. *
  65. */
  66. public function deleteAddress()
  67. {
  68. $this->model->customer()->dissociate();
  69. return $this->model->delete();
  70. }
  71. /**
  72. * List all the address
  73. *
  74. * @param string $order
  75. * @param string $sort
  76. * @param array $columns
  77. * @return array|Collection
  78. */
  79. public function listAddress(string $order = 'id', string $sort = 'desc', array $columns = ['*']) : Collection
  80. {
  81. return $this->all($columns, $order, $sort);
  82. }
  83. /**
  84. * Return the address
  85. *
  86. * @param int $id
  87. *
  88. * @return Address
  89. * @throws AddressNotFoundException
  90. */
  91. public function findAddressById(int $id) : Address
  92. {
  93. try {
  94. return $this->findOneOrFail($id);
  95. } catch (ModelNotFoundException $e) {
  96. throw new AddressNotFoundException('Address not found.');
  97. }
  98. }
  99. /**
  100. * Return the address
  101. *
  102. * @param int $id
  103. *
  104. * @return Address
  105. * @throws AddressNotFoundException
  106. */
  107. public function findCustomerAddressById(int $id, Customer $customer) : Address
  108. {
  109. try
  110. {
  111. return $customer
  112. ->addresses()
  113. ->whereId($id)
  114. ->firstOrFail();
  115. }
  116. catch (ModelNotFoundException $e)
  117. {
  118. throw new AddressNotFoundException('Address not found.');
  119. }
  120. }
  121. /**
  122. * Return the customer owner of the address
  123. *
  124. * @return Customer
  125. */
  126. public function findCustomer() : Customer
  127. {
  128. return $this->model->customer;
  129. }
  130. /**
  131. * @param string $text
  132. * @return mixed
  133. */
  134. public function searchAddress(string $text = null) : Collection
  135. {
  136. if (is_null($text)) {
  137. return $this->all(['*'], 'address_1', 'asc');
  138. }
  139. return $this->model->searchAddress($text)->get();
  140. }
  141. /**
  142. * @return Country
  143. */
  144. public function findCountry() : Country
  145. {
  146. return $this->model->country;
  147. }
  148. /**
  149. * @return Province
  150. */
  151. public function findProvince() : Province
  152. {
  153. return $this->model->province;
  154. }
  155. public function findCity() : City
  156. {
  157. return $this->model->city;
  158. }
  159. /**
  160. * @return Collection
  161. */
  162. public function findOrders() : Collection
  163. {
  164. return $this->model->orders()->get();
  165. }
  166. }