CustomerRepository.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace App\Shop\Customers\Repositories;
  3. use App\Shop\Addresses\Address;
  4. use Jsdecena\Baserepo\BaseRepository;
  5. use App\Shop\Customers\Customer;
  6. use App\Shop\Customers\Exceptions\CreateCustomerInvalidArgumentException;
  7. use App\Shop\Customers\Exceptions\CustomerNotFoundException;
  8. use App\Shop\Customers\Exceptions\CustomerPaymentChargingErrorException;
  9. use App\Shop\Customers\Exceptions\UpdateCustomerInvalidArgumentException;
  10. use App\Shop\Customers\Repositories\Interfaces\CustomerRepositoryInterface;
  11. use Illuminate\Database\Eloquent\Collection;
  12. use Illuminate\Database\Eloquent\ModelNotFoundException;
  13. use Illuminate\Database\QueryException;
  14. use Illuminate\Support\Collection as Support;
  15. class CustomerRepository extends BaseRepository implements CustomerRepositoryInterface
  16. {
  17. /**
  18. * CustomerRepository constructor.
  19. * @param Customer $customer
  20. */
  21. public function __construct(Customer $customer)
  22. {
  23. parent::__construct($customer);
  24. $this->model = $customer;
  25. }
  26. /**
  27. * List all the employees
  28. *
  29. * @param string $order
  30. * @param string $sort
  31. * @param array $columns
  32. * @return \Illuminate\Support\Collection
  33. */
  34. public function listCustomers(string $order = 'id', string $sort = 'desc', array $columns = ['*']) : Support
  35. {
  36. return $this->all($columns, $order, $sort);
  37. }
  38. /**
  39. * Create the customer
  40. *
  41. * @param array $params
  42. * @return Customer
  43. * @throws CreateCustomerInvalidArgumentException
  44. */
  45. public function createCustomer(array $params) : Customer
  46. {
  47. try {
  48. $data = collect($params)->except('password')->all();
  49. $customer = new Customer($data);
  50. if (isset($params['password'])) {
  51. $customer->password = bcrypt($params['password']);
  52. }
  53. $customer->save();
  54. return $customer;
  55. } catch (QueryException $e) {
  56. throw new CreateCustomerInvalidArgumentException($e->getMessage(), 500, $e);
  57. }
  58. }
  59. /**
  60. * Update the customer
  61. *
  62. * @param array $params
  63. *
  64. * @return bool
  65. * @throws UpdateCustomerInvalidArgumentException
  66. */
  67. public function updateCustomer(array $params) : bool
  68. {
  69. try {
  70. return $this->model->update($params);
  71. } catch (QueryException $e) {
  72. throw new UpdateCustomerInvalidArgumentException($e);
  73. }
  74. }
  75. /**
  76. * Find the customer or fail
  77. *
  78. * @param int $id
  79. *
  80. * @return Customer
  81. * @throws CustomerNotFoundException
  82. */
  83. public function findCustomerById(int $id) : Customer
  84. {
  85. try {
  86. return $this->findOneOrFail($id);
  87. } catch (ModelNotFoundException $e) {
  88. throw new CustomerNotFoundException($e);
  89. }
  90. }
  91. /**
  92. * Delete a customer
  93. *
  94. * @return bool
  95. * @throws \Exception
  96. */
  97. public function deleteCustomer() : bool
  98. {
  99. return $this->delete();
  100. }
  101. /**
  102. * @param Address $address
  103. * @return Address
  104. */
  105. public function attachAddress(Address $address) : Address
  106. {
  107. $this->model->addresses()->save($address);
  108. return $address;
  109. }
  110. /**
  111. * Find the address attached to the customer
  112. *
  113. * @return mixed
  114. */
  115. public function findAddresses() : Support
  116. {
  117. return $this->model->addresses;
  118. }
  119. /**
  120. * @param array $columns
  121. * @param string $orderBy
  122. *
  123. * @return Collection
  124. */
  125. public function findOrders($columns = ['*'], string $orderBy = 'id') : Collection
  126. {
  127. return $this->model->orders()->get($columns)->sortByDesc($orderBy);
  128. }
  129. /**
  130. * @param string $text
  131. * @return mixed
  132. */
  133. public function searchCustomer(string $text = null) : Collection
  134. {
  135. if (is_null($text)) {
  136. return $this->all();
  137. }
  138. return $this->model->searchCustomer($text)->get();
  139. }
  140. /**
  141. * @param int $amount
  142. * @param array $options
  143. * @return \Stripe\Charge
  144. * @throws CustomerPaymentChargingErrorException
  145. */
  146. public function charge(int $amount, array $options)
  147. {
  148. try {
  149. return $this->model->charge($amount * 100, $options);
  150. } catch (\Exception $e) {
  151. throw new CustomerPaymentChargingErrorException($e);
  152. }
  153. }
  154. }