DummyRepository.stub 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace App\Shop\Dummies\Repositories;
  3. use App\Shop\Dummies\Dummy;
  4. use Illuminate\Support\Collection;
  5. use Jsdecena\Baserepo\BaseRepository;
  6. use Illuminate\Database\QueryException;
  7. use Illuminate\Database\Eloquent\ModelNotFoundException;
  8. use App\Shop\Dummies\Repositories\Interfaces\DummyRepositoryInterface;
  9. class DummyRepository extends BaseRepository implements DummyRepositoryInterface
  10. {
  11. /**
  12. * DummyRepository constructor.
  13. *
  14. * @param Dummy $dummy
  15. */
  16. public function __construct(Dummy $dummy)
  17. {
  18. parent::__construct($dummy);
  19. $this->model = $dummy;
  20. }
  21. /**
  22. * List all the Dummies
  23. *
  24. * @param string $order
  25. * @param string $sort
  26. * @param array $except
  27. * @return \Illuminate\Support\Collection
  28. */
  29. public function listDummies(string $order = 'id', string $sort = 'desc', $except = []) : Collection
  30. {
  31. return $this->model->orderBy($order, $sort)->get()->except($except);
  32. }
  33. /**
  34. * Create Dummy
  35. *
  36. * @param array $params
  37. *
  38. * @return Dummy
  39. * @throws InvalidArgumentException
  40. */
  41. public function createDummy(array $params) : Dummy
  42. {
  43. try {
  44. return Dummy::create($params);
  45. } catch (QueryException $e) {
  46. throw new InvalidArgumentException($e->getMessage());
  47. }
  48. }
  49. /**
  50. * Update the dummy
  51. *
  52. * @param array $params
  53. * @return Dummy
  54. */
  55. public function updateDummy(array $params) : Dummy
  56. {
  57. $dummy = $this->findDummyById($this->model->id);
  58. $dummy->update($params);
  59. return $dummy;
  60. }
  61. /**
  62. * @param int $id
  63. *
  64. * @return Dummy
  65. * @throws ModelNotFoundException
  66. */
  67. public function findDummyById(int $id) : Dummy
  68. {
  69. try {
  70. return $this->findOneOrFail($id);
  71. } catch (ModelNotFoundException $e) {
  72. throw new ModelNotFoundException($e->getMessage());
  73. }
  74. }
  75. /**
  76. * Delete a dummy
  77. *
  78. * @return bool
  79. */
  80. public function deleteDummy() : bool
  81. {
  82. return $this->model->delete();
  83. }
  84. }