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 ;
22+ use Magento \Framework \App \Cache \TypeListInterface ;
23+ use Magento \Framework \App \ObjectManager ;
24+ use Magento \Framework \Data \Collection \AbstractDb ;
25+ use Magento \Framework \Data \FormFactory ;
26+ use Magento \Framework \DataObject ;
1227use Magento \Framework \DataObject \IdentityInterface ;
28+ use Magento \Framework \Model \Context ;
29+ use Magento \Framework \Model \ResourceModel \AbstractResource ;
30+ use Magento \Framework \Model \ResourceModel \Iterator ;
31+ use Magento \Framework \Registry ;
32+ use Magento \Framework \Serialize \Serializer \Json ;
33+ use Magento \Framework \Stdlib \DateTime ;
34+ use Magento \Framework \Stdlib \DateTime \TimezoneInterface ;
35+ use Magento \Store \Model \StoreManagerInterface ;
1336
1437/**
1538 * Catalog Rule data model
@@ -136,6 +159,11 @@ class Rule extends \Magento\Rule\Model\AbstractModel implements RuleInterface, I
136159 */
137160 protected $ ruleConditionConverter ;
138161
162+ /**
163+ * @var RuleResourceModel
164+ */
165+ private $ ruleResourceModel ;
166+
139167 /**
140168 * Rule constructor
141169 *
@@ -161,32 +189,33 @@ class Rule extends \Magento\Rule\Model\AbstractModel implements RuleInterface, I
161189 * @param ExtensionAttributesFactory|null $extensionFactory
162190 * @param AttributeValueFactory|null $customAttributeFactory
163191 * @param \Magento\Framework\Serialize\Serializer\Json $serializer
164- *
192+ * @param \Magento\CatalogRule\Model\ResourceModel\RuleResourceModel|null $ruleResourceModel
165193 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
166194 */
167195 public function __construct (
168- \ Magento \ Framework \ Model \ Context $ context ,
169- \ Magento \ Framework \ Registry $ registry ,
170- \ Magento \ Framework \ Data \ FormFactory $ formFactory ,
171- \ Magento \ Framework \ Stdlib \ DateTime \ TimezoneInterface $ localeDate ,
172- \ Magento \ Catalog \ Model \ ResourceModel \ Product \ CollectionFactory $ productCollectionFactory ,
173- \ Magento \ Store \ Model \ StoreManagerInterface $ storeManager ,
174- \ Magento \ CatalogRule \ Model \ Rule \ Condition \ CombineFactory $ combineFactory ,
175- \ Magento \ CatalogRule \ Model \ Rule \ Action \ CollectionFactory $ actionCollectionFactory ,
176- \ Magento \ Catalog \ Model \ ProductFactory $ productFactory ,
177- \ Magento \ Framework \ Model \ ResourceModel \ Iterator $ resourceIterator ,
178- \ Magento \ Customer \ Model \ Session $ customerSession ,
179- \ Magento \ CatalogRule \ Helper \ Data $ catalogRuleData ,
180- \ Magento \ Framework \ App \ Cache \ TypeListInterface $ cacheTypesList ,
181- \ Magento \ Framework \ Stdlib \ DateTime $ dateTime ,
182- \ Magento \ CatalogRule \ Model \ Indexer \ Rule \ RuleProductProcessor $ ruleProductProcessor ,
183- \ Magento \ Framework \ Model \ ResourceModel \ AbstractResource $ resource = null ,
184- \ Magento \ Framework \ Data \ Collection \ AbstractDb $ resourceCollection = null ,
196+ Context $ context ,
197+ Registry $ registry ,
198+ FormFactory $ formFactory ,
199+ TimezoneInterface $ localeDate ,
200+ CollectionFactory $ productCollectionFactory ,
201+ StoreManagerInterface $ storeManager ,
202+ CombineFactory $ combineFactory ,
203+ RuleCollectionFactory $ actionCollectionFactory ,
204+ ProductFactory $ productFactory ,
205+ Iterator $ resourceIterator ,
206+ Session $ customerSession ,
207+ Data $ catalogRuleData ,
208+ TypeListInterface $ cacheTypesList ,
209+ DateTime $ dateTime ,
210+ RuleProductProcessor $ ruleProductProcessor ,
211+ AbstractResource $ resource = null ,
212+ AbstractDb $ resourceCollection = null ,
185213 array $ relatedCacheTypes = [],
186214 array $ data = [],
187215 ExtensionAttributesFactory $ extensionFactory = null ,
188216 AttributeValueFactory $ customAttributeFactory = null ,
189- \Magento \Framework \Serialize \Serializer \Json $ serializer = null
217+ Json $ serializer = null ,
218+ RuleResourceModel $ ruleResourceModel = null
190219 ) {
191220 $ this ->_productCollectionFactory = $ productCollectionFactory ;
192221 $ this ->_storeManager = $ storeManager ;
@@ -200,6 +229,7 @@ public function __construct(
200229 $ this ->_relatedCacheTypes = $ relatedCacheTypes ;
201230 $ this ->dateTime = $ dateTime ;
202231 $ this ->_ruleProductProcessor = $ ruleProductProcessor ;
232+ $ this ->ruleResourceModel = $ ruleResourceModel ?: ObjectManager::getInstance ()->get (RuleResourceModel::class);
203233
204234 parent ::__construct (
205235 $ context ,
@@ -223,7 +253,7 @@ public function __construct(
223253 protected function _construct ()
224254 {
225255 parent ::_construct ();
226- $ this ->_init (\ Magento \ CatalogRule \ Model \ ResourceModel \Rule ::class);
256+ $ this ->_init (RuleResourceModel ::class);
227257 $ this ->setIdFieldName ('rule_id ' );
228258 }
229259
@@ -255,7 +285,7 @@ public function getActionsInstance()
255285 public function getCustomerGroupIds ()
256286 {
257287 if (!$ this ->hasCustomerGroupIds ()) {
258- $ customerGroupIds = $ this ->_getResource () ->getCustomerGroupIds ($ this ->getId ());
288+ $ customerGroupIds = $ this ->ruleResourceModel ->getCustomerGroupIds ($ this ->getId ());
259289 $ this ->setData ('customer_group_ids ' , (array )$ customerGroupIds );
260290 }
261291 return $ this ->_getData ('customer_group_ids ' );
@@ -269,7 +299,7 @@ public function getCustomerGroupIds()
269299 public function getNow ()
270300 {
271301 if (!$ this ->_now ) {
272- return (new \DateTime ())->format (\ Magento \ Framework \ Stdlib \ DateTime::DATETIME_PHP_FORMAT );
302+ return (new \DateTime ())->format (DateTime::DATETIME_PHP_FORMAT );
273303 }
274304 return $ this ->_now ;
275305 }
@@ -363,7 +393,7 @@ protected function _getWebsitesMap()
363393 /**
364394 * {@inheritdoc}
365395 */
366- public function validateData (\ Magento \ Framework \ DataObject $ dataObject )
396+ public function validateData (DataObject $ dataObject )
367397 {
368398 $ result = parent ::validateData ($ dataObject );
369399 if ($ result === true ) {
@@ -470,7 +500,7 @@ public function calcProductPriceRule(Product $product, $price)
470500 */
471501 protected function _getRulesFromProduct ($ dateTs , $ websiteId , $ customerGroupId , $ productId )
472502 {
473- return $ this ->_getResource () ->getRulesFromProduct ($ dateTs , $ websiteId , $ customerGroupId , $ productId );
503+ return $ this ->ruleResourceModel ->getRulesFromProduct ($ dateTs , $ websiteId , $ customerGroupId , $ productId );
474504 }
475505
476506 /**
@@ -516,10 +546,10 @@ protected function _invalidateCache()
516546 */
517547 public function afterSave ()
518548 {
519- if ($ this ->isObjectNew ()) {
520- $ this ->getMatchingProductIds ();
521- if (!empty ($ this -> _productIds ) && is_array ($ this -> _productIds )) {
522- $ this ->_getResource () ->addCommitCallback ([$ this , 'reindex ' ]);
549+ if ($ this ->isObjectNew () && ! $ this -> _ruleProductProcessor -> isIndexerScheduled () ) {
550+ $ productIds = $ this ->getMatchingProductIds ();
551+ if (!empty ($ productIds ) && is_array ($ productIds )) {
552+ $ this ->ruleResourceModel ->addCommitCallback ([$ this , 'reindex ' ]);
523553 }
524554 } else {
525555 $ this ->_ruleProductProcessor ->getIndexer ()->invalidate ();
@@ -778,7 +808,7 @@ public function getExtensionAttributes()
778808 * @param \Magento\CatalogRule\Api\Data\RuleExtensionInterface $extensionAttributes
779809 * @return $this
780810 */
781- public function setExtensionAttributes (\ Magento \ CatalogRule \ Api \ Data \ RuleExtensionInterface $ extensionAttributes )
811+ public function setExtensionAttributes (RuleExtensionInterface $ extensionAttributes )
782812 {
783813 return $ this ->_setExtensionAttributes ($ extensionAttributes );
784814 }
@@ -790,8 +820,8 @@ public function setExtensionAttributes(\Magento\CatalogRule\Api\Data\RuleExtensi
790820 private function getRuleConditionConverter ()
791821 {
792822 if (null === $ this ->ruleConditionConverter ) {
793- $ this ->ruleConditionConverter = \ Magento \ Framework \ App \ ObjectManager::getInstance ()
794- ->get (\ Magento \ CatalogRule \ Model \ Data \ Condition \ Converter::class);
823+ $ this ->ruleConditionConverter = ObjectManager::getInstance ()
824+ ->get (Converter::class);
795825 }
796826 return $ this ->ruleConditionConverter ;
797827 }
0 commit comments