RuleRepository.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SalesRule\Model;
  7. use Magento\Framework\Api\Search\FilterGroup;
  8. use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
  9. use Magento\Framework\Api\SearchCriteriaInterface;
  10. use Magento\SalesRule\Model\ResourceModel\Rule\Collection;
  11. /**
  12. * Sales rule CRUD class
  13. *
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. */
  16. class RuleRepository implements \Magento\SalesRule\Api\RuleRepositoryInterface
  17. {
  18. /**
  19. * @var \Magento\SalesRule\Model\RuleFactory
  20. */
  21. protected $ruleFactory;
  22. /**
  23. * @var \Magento\SalesRule\Api\Data\RuleInterfaceFactory
  24. */
  25. protected $ruleDataFactory;
  26. /**
  27. * @var \Magento\SalesRule\Api\Data\ConditionInterfaceFactory
  28. */
  29. protected $conditionDataFactory;
  30. /**
  31. * @var \Magento\SalesRule\Model\Converter\ToDataModel
  32. */
  33. protected $toDataModelConverter;
  34. /**
  35. * @var \Magento\SalesRule\Model\Converter\ToModel
  36. */
  37. protected $toModelConverter;
  38. /**
  39. * @var \Magento\Framework\Reflection\DataObjectProcessor
  40. */
  41. protected $dataObjectProcessor;
  42. /**
  43. * @var \Magento\SalesRule\Api\Data\RuleSearchResultInterfaceFactory
  44. */
  45. protected $searchResultFactory;
  46. /**
  47. * @var \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface
  48. */
  49. protected $extensionAttributesJoinProcessor;
  50. /**
  51. * @var \Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory
  52. */
  53. protected $ruleCollectionFactory;
  54. /**
  55. * @var \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface
  56. */
  57. private $collectionProcessor;
  58. /**
  59. * RuleRepository constructor.
  60. * @param RuleFactory $ruleFactory
  61. * @param \Magento\SalesRule\Api\Data\RuleInterfaceFactory $ruleDataFactory
  62. * @param \Magento\SalesRule\Api\Data\ConditionInterfaceFactory $conditionDataFactory
  63. * @param Converter\ToDataModel $toDataModelConverter
  64. * @param Converter\ToModel $toModelConverter
  65. * @param \Magento\SalesRule\Api\Data\RuleSearchResultInterfaceFactory $searchResultFactory
  66. * @param \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor
  67. * @param ResourceModel\Rule\CollectionFactory $ruleCollectionFactory
  68. * @param \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor
  69. * @param CollectionProcessorInterface|null $collectionProcessor
  70. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  71. */
  72. public function __construct(
  73. \Magento\SalesRule\Model\RuleFactory $ruleFactory,
  74. \Magento\SalesRule\Api\Data\RuleInterfaceFactory $ruleDataFactory,
  75. \Magento\SalesRule\Api\Data\ConditionInterfaceFactory $conditionDataFactory,
  76. \Magento\SalesRule\Model\Converter\ToDataModel $toDataModelConverter,
  77. \Magento\SalesRule\Model\Converter\ToModel $toModelConverter,
  78. \Magento\SalesRule\Api\Data\RuleSearchResultInterfaceFactory $searchResultFactory,
  79. \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor,
  80. \Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory $ruleCollectionFactory,
  81. \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor,
  82. CollectionProcessorInterface $collectionProcessor = null
  83. ) {
  84. $this->ruleFactory = $ruleFactory;
  85. $this->ruleDataFactory = $ruleDataFactory;
  86. $this->conditionDataFactory = $conditionDataFactory;
  87. $this->toDataModelConverter = $toDataModelConverter;
  88. $this->toModelConverter = $toModelConverter;
  89. $this->searchResultFactory = $searchResultFactory;
  90. $this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
  91. $this->ruleCollectionFactory = $ruleCollectionFactory;
  92. $this->dataObjectProcessor = $dataObjectProcessor;
  93. $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
  94. }
  95. /**
  96. * @inheritdoc
  97. */
  98. public function save(\Magento\SalesRule\Api\Data\RuleInterface $rule)
  99. {
  100. $model = $this->toModelConverter->toModel($rule);
  101. $model->save();
  102. $model->load($model->getId());
  103. $model->getStoreLabels();
  104. return $this->toDataModelConverter->toDataModel($model);
  105. }
  106. /**
  107. * @inheritdoc
  108. */
  109. public function getById($id)
  110. {
  111. $model = $this->ruleFactory->create()
  112. ->load($id);
  113. if (!$model->getId()) {
  114. throw new \Magento\Framework\Exception\NoSuchEntityException();
  115. }
  116. $model->getStoreLabels();
  117. $dataModel = $this->toDataModelConverter->toDataModel($model);
  118. return $dataModel;
  119. }
  120. /**
  121. * @inheritdoc
  122. */
  123. public function getList(SearchCriteriaInterface $searchCriteria)
  124. {
  125. /** @var \Magento\SalesRule\Model\ResourceModel\Rule\Collection $collection */
  126. $collection = $this->ruleCollectionFactory->create();
  127. $ruleInterfaceName = \Magento\SalesRule\Api\Data\RuleInterface::class;
  128. $this->extensionAttributesJoinProcessor->process($collection, $ruleInterfaceName);
  129. $this->collectionProcessor->process($searchCriteria, $collection);
  130. $rules = [];
  131. /** @var \Magento\SalesRule\Model\Rule $ruleModel */
  132. foreach ($collection->getItems() as $ruleModel) {
  133. $ruleModel->load($ruleModel->getId());
  134. $ruleModel->getStoreLabels();
  135. $rules[] = $this->toDataModelConverter->toDataModel($ruleModel);
  136. }
  137. $searchResults = $this->searchResultFactory->create();
  138. $searchResults->setSearchCriteria($searchCriteria);
  139. $searchResults->setItems($rules);
  140. $searchResults->setTotalCount($collection->getSize());
  141. return $searchResults;
  142. }
  143. /**
  144. * Delete sales rule by ID.
  145. *
  146. * @param int $id
  147. * @return bool true on success
  148. * @throws \Magento\Framework\Exception\NoSuchEntityException
  149. * @throws \Magento\Framework\Exception\LocalizedException
  150. */
  151. public function deleteById($id)
  152. {
  153. $model = $this->ruleFactory->create()->load($id);
  154. if (!$model->getId()) {
  155. throw new \Magento\Framework\Exception\NoSuchEntityException();
  156. }
  157. $model->delete();
  158. return true;
  159. }
  160. /**
  161. * Helper function that adds a FilterGroup to the collection.
  162. *
  163. * @param FilterGroup $filterGroup
  164. * @param Collection $collection
  165. * @deprecated 101.0.0
  166. * @return void
  167. */
  168. protected function addFilterGroupToCollection(
  169. FilterGroup $filterGroup,
  170. Collection $collection
  171. ) {
  172. $fields = [];
  173. $conditions = [];
  174. foreach ($filterGroup->getFilters() as $filter) {
  175. $condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
  176. $fields[] = $filter->getField();
  177. $conditions[] = [$condition => $filter->getValue()];
  178. }
  179. if ($fields) {
  180. $collection->addFieldToFilter($fields, $conditions);
  181. }
  182. }
  183. /**
  184. * Retrieve collection processor
  185. *
  186. * @deprecated 101.0.0
  187. * @return CollectionProcessorInterface
  188. */
  189. private function getCollectionProcessor()
  190. {
  191. if (!$this->collectionProcessor) {
  192. $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
  193. \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface::class
  194. );
  195. }
  196. return $this->collectionProcessor;
  197. }
  198. }