CouponRepository.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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\SalesRule\Model\ResourceModel\Coupon\Collection;
  10. /**
  11. * Coupon CRUD class
  12. *
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. */
  15. class CouponRepository implements \Magento\SalesRule\Api\CouponRepositoryInterface
  16. {
  17. /**
  18. * @var \Magento\SalesRule\Model\CouponFactory
  19. */
  20. protected $couponFactory;
  21. /**
  22. * @var \Magento\SalesRule\Model\RuleFactory
  23. */
  24. protected $ruleFactory;
  25. /**
  26. * @var \Magento\SalesRule\Api\Data\CouponSearchResultInterfaceFactory
  27. */
  28. protected $searchResultFactory;
  29. /**
  30. * @var \Magento\SalesRule\Model\ResourceModel\Coupon\CollectionFactory
  31. */
  32. protected $collectionFactory;
  33. /**
  34. * @var \Magento\SalesRule\Model\Spi\CouponResourceInterface
  35. */
  36. protected $resourceModel;
  37. /**
  38. * @var \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface
  39. */
  40. protected $extensionAttributesJoinProcessor;
  41. /**
  42. * @var \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface
  43. */
  44. private $collectionProcessor;
  45. /**
  46. * CouponRepository constructor.
  47. * @param CouponFactory $couponFactory
  48. * @param RuleFactory $ruleFactory
  49. * @param \Magento\SalesRule\Api\Data\CouponSearchResultInterfaceFactory $searchResultFactory
  50. * @param ResourceModel\Coupon\CollectionFactory $collectionFactory
  51. * @param Spi\CouponResourceInterface $resourceModel
  52. * @param \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor
  53. * @param CollectionProcessorInterface|null $collectionProcessor
  54. */
  55. public function __construct(
  56. \Magento\SalesRule\Model\CouponFactory $couponFactory,
  57. \Magento\SalesRule\Model\RuleFactory $ruleFactory,
  58. \Magento\SalesRule\Api\Data\CouponSearchResultInterfaceFactory $searchResultFactory,
  59. \Magento\SalesRule\Model\ResourceModel\Coupon\CollectionFactory $collectionFactory,
  60. \Magento\SalesRule\Model\Spi\CouponResourceInterface $resourceModel,
  61. \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor,
  62. CollectionProcessorInterface $collectionProcessor = null
  63. ) {
  64. $this->couponFactory = $couponFactory;
  65. $this->ruleFactory = $ruleFactory;
  66. $this->searchResultFactory = $searchResultFactory;
  67. $this->collectionFactory = $collectionFactory;
  68. $this->resourceModel = $resourceModel;
  69. $this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
  70. $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
  71. }
  72. /**
  73. * Save coupon.
  74. *
  75. * @param \Magento\SalesRule\Api\Data\CouponInterface $coupon
  76. * @return \Magento\SalesRule\Api\Data\CouponInterface
  77. * @throws \Magento\Framework\Exception\InputException If there is a problem with the input
  78. * @throws \Magento\Framework\Exception\NoSuchEntityException If a coupon ID is sent but the coupon does not exist
  79. * @throws \Magento\Framework\Exception\LocalizedException
  80. */
  81. public function save(\Magento\SalesRule\Api\Data\CouponInterface $coupon)
  82. {
  83. //if coupon id is provided, use the existing coupon and blend in the new data supplied
  84. $couponId = $coupon->getCouponId();
  85. if ($couponId) {
  86. $existingCoupon = $this->getById($couponId);
  87. $mergedData = array_merge($existingCoupon->getData(), $coupon->getData());
  88. $coupon->setData($mergedData);
  89. }
  90. //blend in specific fields from the rule
  91. try {
  92. $rule = $this->ruleFactory->create()->load($coupon->getRuleId());
  93. if (!$rule->getRuleId()) {
  94. throw \Magento\Framework\Exception\NoSuchEntityException::singleField('rule_id', $coupon->getRuleId());
  95. }
  96. if ($rule->getCouponType() == $rule::COUPON_TYPE_NO_COUPON) {
  97. throw new \Magento\Framework\Exception\LocalizedException(
  98. __('Specified rule does not allow coupons')
  99. );
  100. } elseif ($rule->getUseAutoGeneration() && $coupon->getType() == $coupon::TYPE_MANUAL) {
  101. throw new \Magento\Framework\Exception\LocalizedException(
  102. __('Specified rule only allows auto generated coupons')
  103. );
  104. } elseif (!$rule->getUseAutoGeneration() && $coupon->getType() == $coupon::TYPE_GENERATED) {
  105. throw new \Magento\Framework\Exception\LocalizedException(
  106. __('Specified rule does not allow auto generated coupons')
  107. );
  108. }
  109. $coupon->setExpirationDate($rule->getToDate());
  110. $coupon->setUsageLimit($rule->getUsesPerCoupon());
  111. $coupon->setUsagePerCustomer($rule->getUsesPerCustomer());
  112. } catch (\Exception $e) {
  113. throw new \Magento\Framework\Exception\LocalizedException(
  114. __('Error occurred when saving coupon: %1', $e->getMessage())
  115. );
  116. }
  117. $this->resourceModel->save($coupon);
  118. return $coupon;
  119. }
  120. /**
  121. * Get coupon by coupon id.
  122. *
  123. * @param int $couponId
  124. * @return \Magento\SalesRule\Api\Data\CouponInterface
  125. * @throws \Magento\Framework\Exception\NoSuchEntityException If $couponId is not found
  126. * @throws \Magento\Framework\Exception\LocalizedException
  127. */
  128. public function getById($couponId)
  129. {
  130. $coupon = $this->couponFactory->create()->load($couponId);
  131. if (!$coupon->getCouponId()) {
  132. throw new \Magento\Framework\Exception\NoSuchEntityException();
  133. }
  134. return $coupon;
  135. }
  136. /**
  137. * Retrieve coupon.
  138. *
  139. * @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
  140. * @return \Magento\SalesRule\Api\Data\CouponSearchResultInterface
  141. * @throws \Magento\Framework\Exception\LocalizedException
  142. */
  143. public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
  144. {
  145. /** @var \Magento\SalesRule\Model\ResourceModel\Coupon\Collection $collection */
  146. $collection = $this->collectionFactory->create();
  147. $couponInterfaceName = \Magento\SalesRule\Api\Data\CouponInterface::class;
  148. $this->extensionAttributesJoinProcessor->process($collection, $couponInterfaceName);
  149. $this->collectionProcessor->process($searchCriteria, $collection);
  150. $searchResults = $this->searchResultFactory->create();
  151. $searchResults->setSearchCriteria($searchCriteria);
  152. $searchResults->setItems($collection->getItems());
  153. $searchResults->setTotalCount($collection->getSize());
  154. return $searchResults;
  155. }
  156. /**
  157. * Delete coupon by coupon id.
  158. *
  159. * @param int $couponId
  160. * @return bool true on success
  161. * @throws \Magento\Framework\Exception\NoSuchEntityException
  162. * @throws \Magento\Framework\Exception\LocalizedException
  163. */
  164. public function deleteById($couponId)
  165. {
  166. /** @var \Magento\SalesRule\Model\Coupon $coupon */
  167. $coupon = $this->couponFactory->create()
  168. ->load($couponId);
  169. if (!$coupon->getCouponId()) {
  170. throw new \Magento\Framework\Exception\NoSuchEntityException();
  171. }
  172. $this->resourceModel->delete($coupon);
  173. return true;
  174. }
  175. /**
  176. * Helper function that adds a FilterGroup to the collection.
  177. *
  178. * @param FilterGroup $filterGroup
  179. * @param Collection $collection
  180. * @deprecated 101.0.0
  181. * @return void
  182. */
  183. protected function addFilterGroupToCollection(
  184. FilterGroup $filterGroup,
  185. Collection $collection
  186. ) {
  187. $fields = [];
  188. $conditions = [];
  189. foreach ($filterGroup->getFilters() as $filter) {
  190. $condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
  191. $fields[] = $filter->getField();
  192. $conditions[] = [$condition => $filter->getValue()];
  193. }
  194. if ($fields) {
  195. $collection->addFieldToFilter($fields, $conditions);
  196. }
  197. }
  198. /**
  199. * Retrieve collection processor
  200. *
  201. * @deprecated 101.0.0
  202. * @return CollectionProcessorInterface
  203. */
  204. private function getCollectionProcessor()
  205. {
  206. if (!$this->collectionProcessor) {
  207. $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
  208. \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface::class
  209. );
  210. }
  211. return $this->collectionProcessor;
  212. }
  213. }