66namespace Magento \CatalogRule \Model ;
77
88use Magento \Catalog \Model \Product ;
9+ use Magento \Catalog \Model \ProductFactory ;
10+ use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory ;
11+ use Magento \CatalogRule \Api \Data \RuleExtensionInterface ;
912use Magento \CatalogRule \Api \Data \RuleInterface ;
13+ use Magento \CatalogRule \Helper \Data ;
14+ use Magento \CatalogRule \Model \Data \Condition \Converter ;
15+ use Magento \CatalogRule \Model \Indexer \Rule \RuleProductProcessor ;
16+ use Magento \CatalogRule \Model \ResourceModel \Rule as RuleResourceModel ;
17+ use Magento \CatalogRule \Model \Rule \Action \CollectionFactory as RuleCollectionFactory ;
18+ use Magento \CatalogRule \Model \Rule \Condition \CombineFactory ;
19+ use Magento \Customer \Model \Session ;
1020use Magento \Framework \Api \AttributeValueFactory ;
1121use Magento \Framework \Api \ExtensionAttributesFactory ;
1222use Magento \Framework \App \ObjectManager ;
1323use Magento \Framework \DataObject \IdentityInterface ;
1424use Magento \CatalogRule \Model \ResourceModel \Product \ConditionsToCollectionApplier ;
25+ use Magento \Framework \App \Cache \TypeListInterface ;
26+ use Magento \Framework \DataObject ;
27+ use Magento \Framework \Data \Collection \AbstractDb ;
28+ use Magento \Framework \Data \FormFactory ;
29+ use Magento \Framework \Model \Context ;
30+ use Magento \Framework \Model \ResourceModel \AbstractResource ;
31+ use Magento \Framework \Model \ResourceModel \Iterator ;
32+ use Magento \Framework \Registry ;
33+ use Magento \Framework \Serialize \Serializer \Json ;
34+ use Magento \Framework \Stdlib \DateTime ;
35+ use Magento \Framework \Stdlib \DateTime \TimezoneInterface ;
36+ use Magento \Store \Model \StoreManagerInterface ;
1537
1638/**
1739 * Catalog Rule data model
@@ -148,6 +170,11 @@ class Rule extends \Magento\Rule\Model\AbstractModel implements RuleInterface, I
148170 */
149171 private $ websitesMap ;
150172
173+ /**
174+ * @var RuleResourceModel
175+ */
176+ private $ ruleResourceModel ;
177+
151178 /**
152179 * Rule constructor
153180 *
@@ -174,33 +201,34 @@ class Rule extends \Magento\Rule\Model\AbstractModel implements RuleInterface, I
174201 * @param AttributeValueFactory|null $customAttributeFactory
175202 * @param \Magento\Framework\Serialize\Serializer\Json $serializer
176203 * @param ConditionsToCollectionApplier $conditionsToCollectionApplier
177- *
204+ * @param RuleResourceModel|null $ruleResourceModel
178205 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
179206 */
180207 public function __construct (
181- \ Magento \ Framework \ Model \ Context $ context ,
182- \ Magento \ Framework \ Registry $ registry ,
183- \ Magento \ Framework \ Data \ FormFactory $ formFactory ,
184- \ Magento \ Framework \ Stdlib \ DateTime \ TimezoneInterface $ localeDate ,
185- \ Magento \ Catalog \ Model \ ResourceModel \ Product \ CollectionFactory $ productCollectionFactory ,
186- \ Magento \ Store \ Model \ StoreManagerInterface $ storeManager ,
187- \ Magento \ CatalogRule \ Model \ Rule \ Condition \ CombineFactory $ combineFactory ,
188- \ Magento \ CatalogRule \ Model \ Rule \ Action \ CollectionFactory $ actionCollectionFactory ,
189- \ Magento \ Catalog \ Model \ ProductFactory $ productFactory ,
190- \ Magento \ Framework \ Model \ ResourceModel \ Iterator $ resourceIterator ,
191- \ Magento \ Customer \ Model \ Session $ customerSession ,
192- \ Magento \ CatalogRule \ Helper \ Data $ catalogRuleData ,
193- \ Magento \ Framework \ App \ Cache \ TypeListInterface $ cacheTypesList ,
194- \ Magento \ Framework \ Stdlib \ DateTime $ dateTime ,
195- \ Magento \ CatalogRule \ Model \ Indexer \ Rule \ RuleProductProcessor $ ruleProductProcessor ,
196- \ Magento \ Framework \ Model \ ResourceModel \ AbstractResource $ resource = null ,
197- \ Magento \ Framework \ Data \ Collection \ AbstractDb $ resourceCollection = null ,
208+ Context $ context ,
209+ Registry $ registry ,
210+ FormFactory $ formFactory ,
211+ TimezoneInterface $ localeDate ,
212+ CollectionFactory $ productCollectionFactory ,
213+ StoreManagerInterface $ storeManager ,
214+ CombineFactory $ combineFactory ,
215+ RuleCollectionFactory $ actionCollectionFactory ,
216+ ProductFactory $ productFactory ,
217+ Iterator $ resourceIterator ,
218+ Session $ customerSession ,
219+ Data $ catalogRuleData ,
220+ TypeListInterface $ cacheTypesList ,
221+ DateTime $ dateTime ,
222+ RuleProductProcessor $ ruleProductProcessor ,
223+ AbstractResource $ resource = null ,
224+ AbstractDb $ resourceCollection = null ,
198225 array $ relatedCacheTypes = [],
199226 array $ data = [],
200227 ExtensionAttributesFactory $ extensionFactory = null ,
201228 AttributeValueFactory $ customAttributeFactory = null ,
202- \Magento \Framework \Serialize \Serializer \Json $ serializer = null ,
203- ConditionsToCollectionApplier $ conditionsToCollectionApplier = null
229+ Json $ serializer = null ,
230+ ConditionsToCollectionApplier $ conditionsToCollectionApplier = null ,
231+ RuleResourceModel $ ruleResourceModel = null
204232 ) {
205233 $ this ->_productCollectionFactory = $ productCollectionFactory ;
206234 $ this ->_storeManager = $ storeManager ;
@@ -214,7 +242,7 @@ public function __construct(
214242 $ this ->_relatedCacheTypes = $ relatedCacheTypes ;
215243 $ this ->dateTime = $ dateTime ;
216244 $ this ->_ruleProductProcessor = $ ruleProductProcessor ;
217-
245+ $ this -> ruleResourceModel = $ ruleResourceModel ?: ObjectManager:: getInstance ()-> get (RuleResourceModel::class);
218246 $ this ->conditionsToCollectionApplier = $ conditionsToCollectionApplier
219247 ?? ObjectManager::getInstance ()->get (ConditionsToCollectionApplier::class);
220248
@@ -240,7 +268,7 @@ public function __construct(
240268 protected function _construct ()
241269 {
242270 parent ::_construct ();
243- $ this ->_init (\ Magento \ CatalogRule \ Model \ ResourceModel \Rule ::class);
271+ $ this ->_init (RuleResourceModel ::class);
244272 $ this ->setIdFieldName ('rule_id ' );
245273 }
246274
@@ -272,7 +300,7 @@ public function getActionsInstance()
272300 public function getCustomerGroupIds ()
273301 {
274302 if (!$ this ->hasCustomerGroupIds ()) {
275- $ customerGroupIds = $ this ->_getResource () ->getCustomerGroupIds ($ this ->getId ());
303+ $ customerGroupIds = $ this ->ruleResourceModel ->getCustomerGroupIds ($ this ->getId ());
276304 $ this ->setData ('customer_group_ids ' , (array )$ customerGroupIds );
277305 }
278306 return $ this ->_getData ('customer_group_ids ' );
@@ -286,7 +314,7 @@ public function getCustomerGroupIds()
286314 public function getNow ()
287315 {
288316 if (!$ this ->_now ) {
289- return (new \DateTime ())->format (\ Magento \ Framework \ Stdlib \ DateTime::DATETIME_PHP_FORMAT );
317+ return (new \DateTime ())->format (DateTime::DATETIME_PHP_FORMAT );
290318 }
291319 return $ this ->_now ;
292320 }
@@ -342,6 +370,9 @@ public function getMatchingProductIds()
342370 return $ this ->_productIds ;
343371 }
344372
373+ /**
374+ * @return bool
375+ */
345376 private function canPreMapProducts ()
346377 {
347378 $ conditions = $ this ->getConditions ();
@@ -400,7 +431,7 @@ protected function _getWebsitesMap()
400431 /**
401432 * {@inheritdoc}
402433 */
403- public function validateData (\ Magento \ Framework \ DataObject $ dataObject )
434+ public function validateData (DataObject $ dataObject )
404435 {
405436 $ result = parent ::validateData ($ dataObject );
406437 if ($ result === true ) {
@@ -507,7 +538,7 @@ public function calcProductPriceRule(Product $product, $price)
507538 */
508539 protected function _getRulesFromProduct ($ dateTs , $ websiteId , $ customerGroupId , $ productId )
509540 {
510- return $ this ->_getResource () ->getRulesFromProduct ($ dateTs , $ websiteId , $ customerGroupId , $ productId );
541+ return $ this ->ruleResourceModel ->getRulesFromProduct ($ dateTs , $ websiteId , $ customerGroupId , $ productId );
511542 }
512543
513544 /**
@@ -553,10 +584,10 @@ protected function _invalidateCache()
553584 */
554585 public function afterSave ()
555586 {
556- if ($ this ->isObjectNew ()) {
557- $ this ->getMatchingProductIds ();
558- if (!empty ($ this -> _productIds ) && is_array ($ this -> _productIds )) {
559- $ this ->_getResource () ->addCommitCallback ([$ this , 'reindex ' ]);
587+ if ($ this ->isObjectNew () && ! $ this -> _ruleProductProcessor -> isIndexerScheduled () ) {
588+ $ productIds = $ this ->getMatchingProductIds ();
589+ if (!empty ($ productIds ) && is_array ($ productIds )) {
590+ $ this ->ruleResourceModel ->addCommitCallback ([$ this , 'reindex ' ]);
560591 }
561592 } else {
562593 $ this ->_ruleProductProcessor ->getIndexer ()->invalidate ();
@@ -805,7 +836,7 @@ public function getToDate()
805836 /**
806837 * {@inheritdoc}
807838 *
808- * @return \Magento\CatalogRule\Api\Data\ RuleExtensionInterface|null
839+ * @return RuleExtensionInterface|null
809840 */
810841 public function getExtensionAttributes ()
811842 {
@@ -815,10 +846,10 @@ public function getExtensionAttributes()
815846 /**
816847 * {@inheritdoc}
817848 *
818- * @param \Magento\CatalogRule\Api\Data\ RuleExtensionInterface $extensionAttributes
849+ * @param RuleExtensionInterface $extensionAttributes
819850 * @return $this
820851 */
821- public function setExtensionAttributes (\ Magento \ CatalogRule \ Api \ Data \ RuleExtensionInterface $ extensionAttributes )
852+ public function setExtensionAttributes (RuleExtensionInterface $ extensionAttributes )
822853 {
823854 return $ this ->_setExtensionAttributes ($ extensionAttributes );
824855 }
@@ -830,8 +861,8 @@ public function setExtensionAttributes(\Magento\CatalogRule\Api\Data\RuleExtensi
830861 private function getRuleConditionConverter ()
831862 {
832863 if (null === $ this ->ruleConditionConverter ) {
833- $ this ->ruleConditionConverter = \ Magento \ Framework \ App \ ObjectManager::getInstance ()
834- ->get (\ Magento \ CatalogRule \ Model \ Data \ Condition \ Converter::class);
864+ $ this ->ruleConditionConverter = ObjectManager::getInstance ()
865+ ->get (Converter::class);
835866 }
836867 return $ this ->ruleConditionConverter ;
837868 }
0 commit comments