6
6
namespace Magento \CatalogRule \Model ;
7
7
8
8
use 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 ;
9
12
use 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 ;
10
20
use Magento \Framework \Api \AttributeValueFactory ;
11
21
use 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 ;
12
27
use 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 ;
13
36
14
37
/**
15
38
* Catalog Rule data model
@@ -136,6 +159,11 @@ class Rule extends \Magento\Rule\Model\AbstractModel implements RuleInterface, I
136
159
*/
137
160
protected $ ruleConditionConverter ;
138
161
162
+ /**
163
+ * @var RuleResourceModel
164
+ */
165
+ private $ ruleResourceModel ;
166
+
139
167
/**
140
168
* Rule constructor
141
169
*
@@ -161,32 +189,33 @@ class Rule extends \Magento\Rule\Model\AbstractModel implements RuleInterface, I
161
189
* @param ExtensionAttributesFactory|null $extensionFactory
162
190
* @param AttributeValueFactory|null $customAttributeFactory
163
191
* @param \Magento\Framework\Serialize\Serializer\Json $serializer
164
- *
192
+ * @param \Magento\CatalogRule\Model\ResourceModel\RuleResourceModel|null $ruleResourceModel
165
193
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
166
194
*/
167
195
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 ,
185
213
array $ relatedCacheTypes = [],
186
214
array $ data = [],
187
215
ExtensionAttributesFactory $ extensionFactory = null ,
188
216
AttributeValueFactory $ customAttributeFactory = null ,
189
- \Magento \Framework \Serialize \Serializer \Json $ serializer = null
217
+ Json $ serializer = null ,
218
+ RuleResourceModel $ ruleResourceModel = null
190
219
) {
191
220
$ this ->_productCollectionFactory = $ productCollectionFactory ;
192
221
$ this ->_storeManager = $ storeManager ;
@@ -200,6 +229,7 @@ public function __construct(
200
229
$ this ->_relatedCacheTypes = $ relatedCacheTypes ;
201
230
$ this ->dateTime = $ dateTime ;
202
231
$ this ->_ruleProductProcessor = $ ruleProductProcessor ;
232
+ $ this ->ruleResourceModel = $ ruleResourceModel ?: ObjectManager::getInstance ()->get (RuleResourceModel::class);
203
233
204
234
parent ::__construct (
205
235
$ context ,
@@ -223,7 +253,7 @@ public function __construct(
223
253
protected function _construct ()
224
254
{
225
255
parent ::_construct ();
226
- $ this ->_init (\ Magento \ CatalogRule \ Model \ ResourceModel \Rule ::class);
256
+ $ this ->_init (RuleResourceModel ::class);
227
257
$ this ->setIdFieldName ('rule_id ' );
228
258
}
229
259
@@ -255,7 +285,7 @@ public function getActionsInstance()
255
285
public function getCustomerGroupIds ()
256
286
{
257
287
if (!$ this ->hasCustomerGroupIds ()) {
258
- $ customerGroupIds = $ this ->_getResource () ->getCustomerGroupIds ($ this ->getId ());
288
+ $ customerGroupIds = $ this ->ruleResourceModel ->getCustomerGroupIds ($ this ->getId ());
259
289
$ this ->setData ('customer_group_ids ' , (array )$ customerGroupIds );
260
290
}
261
291
return $ this ->_getData ('customer_group_ids ' );
@@ -269,7 +299,7 @@ public function getCustomerGroupIds()
269
299
public function getNow ()
270
300
{
271
301
if (!$ this ->_now ) {
272
- return (new \DateTime ())->format (\ Magento \ Framework \ Stdlib \ DateTime::DATETIME_PHP_FORMAT );
302
+ return (new \DateTime ())->format (DateTime::DATETIME_PHP_FORMAT );
273
303
}
274
304
return $ this ->_now ;
275
305
}
@@ -363,7 +393,7 @@ protected function _getWebsitesMap()
363
393
/**
364
394
* {@inheritdoc}
365
395
*/
366
- public function validateData (\ Magento \ Framework \ DataObject $ dataObject )
396
+ public function validateData (DataObject $ dataObject )
367
397
{
368
398
$ result = parent ::validateData ($ dataObject );
369
399
if ($ result === true ) {
@@ -470,7 +500,7 @@ public function calcProductPriceRule(Product $product, $price)
470
500
*/
471
501
protected function _getRulesFromProduct ($ dateTs , $ websiteId , $ customerGroupId , $ productId )
472
502
{
473
- return $ this ->_getResource () ->getRulesFromProduct ($ dateTs , $ websiteId , $ customerGroupId , $ productId );
503
+ return $ this ->ruleResourceModel ->getRulesFromProduct ($ dateTs , $ websiteId , $ customerGroupId , $ productId );
474
504
}
475
505
476
506
/**
@@ -516,10 +546,10 @@ protected function _invalidateCache()
516
546
*/
517
547
public function afterSave ()
518
548
{
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 ' ]);
523
553
}
524
554
} else {
525
555
$ this ->_ruleProductProcessor ->getIndexer ()->invalidate ();
@@ -778,7 +808,7 @@ public function getExtensionAttributes()
778
808
* @param \Magento\CatalogRule\Api\Data\RuleExtensionInterface $extensionAttributes
779
809
* @return $this
780
810
*/
781
- public function setExtensionAttributes (\ Magento \ CatalogRule \ Api \ Data \ RuleExtensionInterface $ extensionAttributes )
811
+ public function setExtensionAttributes (RuleExtensionInterface $ extensionAttributes )
782
812
{
783
813
return $ this ->_setExtensionAttributes ($ extensionAttributes );
784
814
}
@@ -790,8 +820,8 @@ public function setExtensionAttributes(\Magento\CatalogRule\Api\Data\RuleExtensi
790
820
private function getRuleConditionConverter ()
791
821
{
792
822
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);
795
825
}
796
826
return $ this ->ruleConditionConverter ;
797
827
}
0 commit comments