AttributeRepository.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace App\Shop\Attributes\Repositories;
  3. use App\Shop\Attributes\Attribute;
  4. use App\Shop\Attributes\Exceptions\AttributeNotFoundException;
  5. use App\Shop\Attributes\Exceptions\CreateAttributeErrorException;
  6. use App\Shop\Attributes\Exceptions\UpdateAttributeErrorException;
  7. use App\Shop\AttributeValues\AttributeValue;
  8. use Jsdecena\Baserepo\BaseRepository;
  9. use Illuminate\Database\Eloquent\ModelNotFoundException;
  10. use Illuminate\Support\Collection;
  11. use Illuminate\Database\QueryException;
  12. class AttributeRepository extends BaseRepository implements AttributeRepositoryInterface
  13. {
  14. /**
  15. * @var Attribute
  16. */
  17. protected $model;
  18. /**
  19. * AttributeRepository constructor.
  20. * @param Attribute $attribute
  21. */
  22. public function __construct(Attribute $attribute)
  23. {
  24. parent::__construct($attribute);
  25. $this->model = $attribute;
  26. }
  27. /**
  28. * @param array $data
  29. * @return Attribute
  30. * @throws CreateAttributeErrorException
  31. */
  32. public function createAttribute(array $data) : Attribute
  33. {
  34. try {
  35. $attribute = new Attribute($data);
  36. $attribute->save();
  37. return $attribute;
  38. } catch (QueryException $e) {
  39. throw new CreateAttributeErrorException($e);
  40. }
  41. }
  42. /**
  43. * @param int $id
  44. * @return Attribute
  45. * @throws AttributeNotFoundException
  46. */
  47. public function findAttributeById(int $id) : Attribute
  48. {
  49. try {
  50. return $this->findOneOrFail($id);
  51. } catch (ModelNotFoundException $e) {
  52. throw new AttributeNotFoundException($e);
  53. }
  54. }
  55. /**
  56. * @param array $data
  57. * @return bool
  58. * @throws UpdateAttributeErrorException
  59. */
  60. public function updateAttribute(array $data) : bool
  61. {
  62. try {
  63. return $this->model->update($data);
  64. } catch (QueryException $e) {
  65. throw new UpdateAttributeErrorException($e);
  66. }
  67. }
  68. /**
  69. * @return bool|null
  70. */
  71. public function deleteAttribute() : ?bool
  72. {
  73. return $this->model->delete();
  74. }
  75. /**
  76. * @param array $columns
  77. * @param string $orderBy
  78. * @param string $sortBy
  79. * @return Collection
  80. */
  81. public function listAttributes($columns = array('*'), string $orderBy = 'id', string $sortBy = 'asc') : Collection
  82. {
  83. return $this->all($columns, $orderBy, $sortBy);
  84. }
  85. /**
  86. * @return Collection
  87. */
  88. public function listAttributeValues() : Collection
  89. {
  90. return $this->model->values()->get();
  91. }
  92. /**
  93. * @param AttributeValue $attributeValue
  94. * @return AttributeValue
  95. */
  96. public function associateAttributeValue(AttributeValue $attributeValue) : AttributeValue
  97. {
  98. return $this->model->values()->save($attributeValue);
  99. }
  100. }