Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. <?php
  2.  
  3. namespace VendorModuleModel;
  4.  
  5. use MagentoQuoteModelQuoteAddress;
  6. use MagentoRuleModelAbstractModel;
  7.  
  8. class Rule extends AbstractModel
  9. {
  10. /**
  11. * Prefix of model events names
  12. *
  13. * @var string
  14. */
  15. protected $_eventPrefix = 'vendor_table';
  16.  
  17. protected $_eventObject = 'rule';
  18.  
  19. /** @var MagentoSalesRuleModelRuleConditionCombineFactory */
  20. protected $condCombineFactory;
  21.  
  22. /** @var MagentoSalesRuleModelRuleConditionProductCombineFactory */
  23. protected $condProdCombineF;
  24.  
  25. /**
  26. * Store already validated addresses and validation results
  27. *
  28. * @var array
  29. */
  30. protected $validatedAddresses = [];
  31.  
  32. /**
  33. * @param MagentoFrameworkModelContext $context
  34. * @param MagentoFrameworkRegistry $registry
  35. * @param MagentoFrameworkDataFormFactory $formFactory
  36. * @param MagentoFrameworkStdlibDateTimeTimezoneInterface $localeDate
  37. * @param MagentoCatalogWidgetModelRuleConditionCombineFactory $condCombineFactory
  38. * @param MagentoCatalogWidgetModelRuleConditionProductCombineFactory $condProdCombineF
  39. * @param MagentoFrameworkModelResourceModelAbstractResource $resource
  40. * @param MagentoFrameworkDataCollectionAbstractDb $resourceCollection
  41. * @param array $data
  42. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  43. */
  44. public function __construct(
  45. MagentoFrameworkModelContext $context,
  46. MagentoFrameworkRegistry $registry,
  47. MagentoFrameworkDataFormFactory $formFactory,
  48. MagentoFrameworkStdlibDateTimeTimezoneInterface $localeDate,
  49. MagentoCatalogWidgetModelRuleConditionCombineFactory $condCombineFactory,
  50. MagentoCatalogWidgetModelRuleConditionProductFactory $condProdCombineF,
  51. MagentoFrameworkModelResourceModelAbstractResource $resource = null,
  52. MagentoFrameworkDataCollectionAbstractDb $resourceCollection = null,
  53. array $data = []
  54. ) {
  55. $this->condCombineFactory = $condCombineFactory;
  56. $this->condProdCombineF = $condProdCombineF;
  57. parent::__construct($context, $registry, $formFactory, $localeDate, $resource, $resourceCollection, $data);
  58. }
  59.  
  60. /**
  61. * Set resource model and Id field name
  62. *
  63. * @return void
  64. */
  65. protected function _construct()
  66. {
  67. parent::_construct();
  68. $this->_init('VendorModuleModelResourceModelRule');
  69. $this->setIdFieldName('profile_id');
  70. }
  71.  
  72. /**
  73. * Get rule condition combine model instance
  74. *
  75. * @return MagentoSalesRuleModelRuleConditionCombine
  76. */
  77. public function getConditionsInstance()
  78. {
  79. return $this->condCombineFactory->create();
  80. }
  81.  
  82. /**
  83. * Get rule condition product combine model instance
  84. *
  85. * @return MagentoSalesRuleModelRuleConditionProductCombine
  86. */
  87. public function getActionsInstance()
  88. {
  89. return $this->condProdCombineF->create();
  90. }
  91.  
  92. /**
  93. * Check cached validation result for specific address
  94. *
  95. * @param Address $address
  96. * @return bool
  97. */
  98. public function hasIsValidForAddress($address)
  99. {
  100. $addressId = $this->_getAddressId($address);
  101. return isset($this->validatedAddresses[$addressId]) ? true : false;
  102. }
  103.  
  104. /**
  105. * Set validation result for specific address to results cache
  106. *
  107. * @param Address $address
  108. * @param bool $validationResult
  109. * @return $this
  110. */
  111. public function setIsValidForAddress($address, $validationResult)
  112. {
  113. $addressId = $this->_getAddressId($address);
  114. $this->validatedAddresses[$addressId] = $validationResult;
  115. return $this;
  116. }
  117.  
  118. /**
  119. * Get cached validation result for specific address
  120. *
  121. * @param Address $address
  122. * @return bool
  123. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  124. */
  125. public function getIsValidForAddress($address)
  126. {
  127. $addressId = $this->_getAddressId($address);
  128. return isset($this->validatedAddresses[$addressId]) ? $this->validatedAddresses[$addressId] : false;
  129. }
  130.  
  131. /**
  132. * Return id for address
  133. *
  134. * @param Address $address
  135. * @return string
  136. */
  137. private function _getAddressId($address)
  138. {
  139. if ($address instanceof Address) {
  140. return $address->getId();
  141. }
  142. return $address;
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement