CatalogRuleRepository.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogRule\Model;
  7. use Magento\CatalogRule\Api\Data;
  8. use Magento\Framework\Exception\CouldNotDeleteException;
  9. use Magento\Framework\Exception\CouldNotSaveException;
  10. use Magento\Framework\Exception\NoSuchEntityException;
  11. use Magento\Framework\Exception\ValidatorException;
  12. class CatalogRuleRepository implements \Magento\CatalogRule\Api\CatalogRuleRepositoryInterface
  13. {
  14. /**
  15. * @var ResourceModel\Rule
  16. */
  17. protected $ruleResource;
  18. /**
  19. * @var RuleFactory
  20. */
  21. protected $ruleFactory;
  22. /**
  23. * @var array
  24. */
  25. private $rules = [];
  26. /**
  27. * @param ResourceModel\Rule $ruleResource
  28. * @param RuleFactory $ruleFactory
  29. */
  30. public function __construct(
  31. \Magento\CatalogRule\Model\ResourceModel\Rule $ruleResource,
  32. \Magento\CatalogRule\Model\RuleFactory $ruleFactory
  33. ) {
  34. $this->ruleResource = $ruleResource;
  35. $this->ruleFactory = $ruleFactory;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function save(Data\RuleInterface $rule)
  41. {
  42. if ($rule->getRuleId()) {
  43. $rule = $this->get($rule->getRuleId())->addData($rule->getData());
  44. }
  45. try {
  46. $this->ruleResource->save($rule);
  47. unset($this->rules[$rule->getId()]);
  48. } catch (ValidatorException $e) {
  49. throw new CouldNotSaveException(__($e->getMessage()));
  50. } catch (\Exception $e) {
  51. throw new CouldNotSaveException(
  52. __('The "%1" rule was unable to be saved. Please try again.', $rule->getRuleId())
  53. );
  54. }
  55. return $rule;
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function get($ruleId)
  61. {
  62. if (!isset($this->rules[$ruleId])) {
  63. /** @var \Magento\CatalogRule\Model\Rule $rule */
  64. $rule = $this->ruleFactory->create();
  65. /* TODO: change to resource model after entity manager will be fixed */
  66. $rule->load($ruleId);
  67. if (!$rule->getRuleId()) {
  68. throw new NoSuchEntityException(
  69. __('The rule with the "%1" ID wasn\'t found. Verify the ID and try again.', $ruleId)
  70. );
  71. }
  72. $this->rules[$ruleId] = $rule;
  73. }
  74. return $this->rules[$ruleId];
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function delete(Data\RuleInterface $rule)
  80. {
  81. try {
  82. $this->ruleResource->delete($rule);
  83. unset($this->rules[$rule->getId()]);
  84. } catch (ValidatorException $e) {
  85. throw new CouldNotSaveException(__($e->getMessage()));
  86. } catch (\Exception $e) {
  87. throw new CouldNotDeleteException(__('The "%1" rule couldn\'t be removed.', $rule->getRuleId()));
  88. }
  89. return true;
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function deleteById($ruleId)
  95. {
  96. $model = $this->get($ruleId);
  97. $this->delete($model);
  98. return true;
  99. }
  100. }