Skip to content

Commit 320e1dc

Browse files
author
Tang, Yu(ytang1)
committed
Merge pull request #348 from magento-fearless-kiwis/develop
[FearlessKiwis] Sprint 60: basics for the SalesRuleStaging module
2 parents 6afabf5 + a2d59a6 commit 320e1dc

40 files changed

+988
-153
lines changed

app/code/Magento/SalesRule/Model/Plugin/ResourceModel/Rule.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
*/
66
namespace Magento\SalesRule\Model\Plugin\ResourceModel;
77

8+
/**
9+
* Class Rule
10+
* @package Magento\SalesRule\Model\Plugin\ResourceModel
11+
* @deprecated
12+
*/
813
class Rule
914
{
1015
/**
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\SalesRule\Model\ResourceModel;
7+
8+
use Magento\SalesRule\Model\ResourceModel\Rule;
9+
use Magento\Framework\Model\Entity\MetadataPool;
10+
11+
class ReadHandler
12+
{
13+
/**
14+
* @var Rule
15+
*/
16+
protected $ruleResource;
17+
18+
/**
19+
* @var MetadataPool
20+
*/
21+
protected $metadataPool;
22+
23+
/**
24+
* @param Rule $ruleResource
25+
* @param MetadataPool $metadataPool
26+
*/
27+
public function __construct(
28+
Rule $ruleResource,
29+
MetadataPool $metadataPool
30+
) {
31+
$this->ruleResource = $ruleResource;
32+
$this->metadataPool = $metadataPool;
33+
}
34+
35+
/**
36+
* @param string $entityType
37+
* @param array $entityData
38+
* @return array
39+
* @throws \Exception
40+
*/
41+
public function execute($entityType, $entityData)
42+
{
43+
$linkField = $this->metadataPool->getMetadata($entityType)->getLinkField();
44+
$entityId = $entityData[$linkField];
45+
46+
$entityData['customer_group_ids'] = $this->ruleResource->getCustomerGroupIds($entityId);
47+
$entityData['website_ids'] = $this->ruleResource->getWebsiteIds($entityId);
48+
49+
return $entityData;
50+
}
51+
}

app/code/Magento/SalesRule/Model/ResourceModel/Rule.php

Lines changed: 78 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,24 @@
55
*/
66
namespace Magento\SalesRule\Model\ResourceModel;
77

8+
use \Magento\SalesRule\Model\Rule as SalesRule;
89
use Magento\Framework\Model\AbstractModel;
10+
use Magento\Framework\DB\Select;
11+
use Magento\Rule\Model\ResourceModel\AbstractResource;
12+
use Magento\Framework\Model\EntityManager;
13+
use Magento\SalesRule\Api\Data\RuleInterface;
914

1015
/**
1116
* Sales Rule resource model
1217
*/
13-
class Rule extends \Magento\Rule\Model\ResourceModel\AbstractResource
18+
class Rule extends AbstractResource
1419
{
1520
/**
1621
* Store associated with rule entities information map
1722
*
1823
* @var array
1924
*/
20-
protected $_associatedEntitiesMap = [
21-
'website' => [
22-
'associations_table' => 'salesrule_website',
23-
'rule_id_field' => 'rule_id',
24-
'entity_id_field' => 'website_id',
25-
],
26-
'customer_group' => [
27-
'associations_table' => 'salesrule_customer_group',
28-
'rule_id_field' => 'rule_id',
29-
'entity_id_field' => 'customer_group_id',
30-
],
31-
];
25+
protected $_associatedEntitiesMap = [];
3226

3327
/**
3428
* @var array
@@ -52,20 +46,31 @@ class Rule extends \Magento\Rule\Model\ResourceModel\AbstractResource
5246
*/
5347
protected $_resourceCoupon;
5448

49+
/**
50+
* @var EntityManager
51+
*/
52+
protected $entityManager;
53+
5554
/**
5655
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
5756
* @param \Magento\Framework\Stdlib\StringUtils $string
5857
* @param \Magento\SalesRule\Model\ResourceModel\Coupon $resourceCoupon
58+
* @param EntityManager $entityManager
59+
* @param array $associatedEntitiesMap
5960
* @param string $connectionName
6061
*/
6162
public function __construct(
6263
\Magento\Framework\Model\ResourceModel\Db\Context $context,
6364
\Magento\Framework\Stdlib\StringUtils $string,
6465
\Magento\SalesRule\Model\ResourceModel\Coupon $resourceCoupon,
66+
EntityManager $entityManager,
67+
array $associatedEntitiesMap = [],
6568
$connectionName = null
6669
) {
6770
$this->string = $string;
6871
$this->_resourceCoupon = $resourceCoupon;
72+
$this->entityManager = $entityManager;
73+
$this->_associatedEntitiesMap = $associatedEntitiesMap;
6974
parent::__construct($context, $connectionName);
7075
}
7176

@@ -79,24 +84,10 @@ protected function _construct()
7984
$this->_init('salesrule', 'rule_id');
8085
}
8186

82-
/**
83-
* Add customer group ids and website ids to rule data after load
84-
*
85-
* @param AbstractModel $object
86-
* @return $this
87-
*/
88-
protected function _afterLoad(AbstractModel $object)
89-
{
90-
$this->loadCustomerGroupIds($object);
91-
$this->loadWebsiteIds($object);
92-
93-
parent::_afterLoad($object);
94-
return $this;
95-
}
96-
9787
/**
9888
* @param AbstractModel $object
9989
* @return void
90+
* @deprecated
10091
*/
10192
public function loadCustomerGroupIds(AbstractModel $object)
10293
{
@@ -109,6 +100,7 @@ public function loadCustomerGroupIds(AbstractModel $object)
109100
/**
110101
* @param AbstractModel $object
111102
* @return void
103+
* @deprecated
112104
*/
113105
public function loadWebsiteIds(AbstractModel $object)
114106
{
@@ -135,6 +127,23 @@ public function _beforeSave(AbstractModel $object)
135127
return $this;
136128
}
137129

130+
/**
131+
* Load an object
132+
*
133+
* @param SalesRule|AbstractModel $object
134+
* @param mixed $value
135+
* @param string $field field to load by (defaults to model id)
136+
* @return $this
137+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
138+
*/
139+
public function load(AbstractModel $object, $value, $field = null)
140+
{
141+
$this->entityManager->load(RuleInterface::class, $object, $value);
142+
$this->unserializeFields($object);
143+
$this->_afterLoad($object);
144+
return $this;
145+
}
146+
138147
/**
139148
* Bind sales rule to customer group(s) and website(s).
140149
* Save rule's associated store labels.
@@ -149,22 +158,6 @@ protected function _afterSave(AbstractModel $object)
149158
$this->saveStoreLabels($object->getId(), $object->getStoreLabels());
150159
}
151160

152-
if ($object->hasWebsiteIds()) {
153-
$websiteIds = $object->getWebsiteIds();
154-
if (!is_array($websiteIds)) {
155-
$websiteIds = explode(',', (string)$websiteIds);
156-
}
157-
$this->bindRuleToEntity($object->getId(), $websiteIds, 'website');
158-
}
159-
160-
if ($object->hasCustomerGroupIds()) {
161-
$customerGroupIds = $object->getCustomerGroupIds();
162-
if (!is_array($customerGroupIds)) {
163-
$customerGroupIds = explode(',', (string)$customerGroupIds);
164-
}
165-
$this->bindRuleToEntity($object->getId(), $customerGroupIds, 'customer_group');
166-
}
167-
168161
// Save product attributes used in rule
169162
$ruleProductAttributes = array_merge(
170163
$this->getProductAttributes(serialize($object->getConditions()->asArray())),
@@ -369,4 +362,45 @@ public function getProductAttributes($serializedString)
369362

370363
return $result;
371364
}
365+
366+
/**
367+
* @param \Magento\Framework\Model\AbstractModel $object
368+
* @return $this
369+
* @throws \Exception
370+
*/
371+
public function save(\Magento\Framework\Model\AbstractModel $object)
372+
{
373+
if ($object->isDeleted()) {
374+
return $this->delete($object);
375+
}
376+
377+
$this->beginTransaction();
378+
379+
try {
380+
if (!$this->isModified($object)) {
381+
$this->processNotModifiedSave($object);
382+
$this->commit();
383+
$object->setHasDataChanges(false);
384+
return $this;
385+
}
386+
$object->validateBeforeSave();
387+
$object->beforeSave();
388+
if ($object->isSaveAllowed()) {
389+
$this->_serializeFields($object);
390+
$this->_beforeSave($object);
391+
$this->_checkUnique($object);
392+
$this->objectRelationProcessor->validateDataIntegrity($this->getMainTable(), $object->getData());
393+
$this->entityManager->save(RuleInterface::class, $object);
394+
$this->unserializeFields($object);
395+
$this->processAfterSaves($object);
396+
}
397+
$this->addCommitCallback([$object, 'afterCommitCallback'])->commit();
398+
$object->setHasDataChanges(false);
399+
} catch (\Exception $e) {
400+
$this->rollBack();
401+
$object->setHasDataChanges(true);
402+
throw $e;
403+
}
404+
return $this;
405+
}
372406
}

app/code/Magento/SalesRule/Model/ResourceModel/Rule/Collection.php

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,12 @@ class Collection extends \Magento\Rule\Model\ResourceModel\Rule\Collection\Abstr
2222
*
2323
* @var array
2424
*/
25-
protected $_associatedEntitiesMap = [
26-
'website' => [
27-
'associations_table' => 'salesrule_website',
28-
'rule_id_field' => 'rule_id',
29-
'entity_id_field' => 'website_id',
30-
],
31-
'customer_group' => [
32-
'associations_table' => 'salesrule_customer_group',
33-
'rule_id_field' => 'rule_id',
34-
'entity_id_field' => 'customer_group_id',
35-
],
36-
];
25+
protected $_associatedEntitiesMap;
26+
27+
/**
28+
* @var \Magento\SalesRule\Model\ResourceModel\Rule\DateApplier
29+
*/
30+
protected $dateApplier;
3731

3832
/**
3933
* @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
@@ -46,6 +40,8 @@ class Collection extends \Magento\Rule\Model\ResourceModel\Rule\Collection\Abstr
4640
* @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
4741
* @param \Magento\Framework\Event\ManagerInterface $eventManager
4842
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $date
43+
* @param \Magento\SalesRule\Model\ResourceModel\Rule\DateApplier $dateApplier
44+
* @param array $associatedEntitiesMap
4945
* @param mixed $connection
5046
* @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
5147
*/
@@ -55,11 +51,15 @@ public function __construct(
5551
\Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
5652
\Magento\Framework\Event\ManagerInterface $eventManager,
5753
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $date,
54+
\Magento\SalesRule\Model\ResourceModel\Rule\DateApplier $dateApplier,
55+
array $associatedEntitiesMap = [],
5856
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
5957
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
6058
) {
6159
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
6260
$this->_date = $date;
61+
$this->_associatedEntitiesMap = $associatedEntitiesMap;
62+
$this->dateApplier = $dateApplier;
6363
}
6464

6565
/**
@@ -195,14 +195,10 @@ public function addWebsiteGroupDateFilter($websiteId, $customerGroupId, $now = n
195195
(int)$customerGroupId
196196
),
197197
[]
198-
)->where(
199-
'from_date is null or from_date <= ?',
200-
$now
201-
)->where(
202-
'to_date is null or to_date >= ?',
203-
$now
204198
);
205199

200+
$this->dateApplier->applyDate($this->getSelect(), $now);
201+
206202
$this->addIsActiveFilter();
207203

208204
$this->setFlag('website_group_date_filter', true);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\SalesRule\Model\ResourceModel\Rule;
7+
8+
/**
9+
* Class DateApplier
10+
* adds the dates just for SalesRule
11+
*/
12+
class DateApplier
13+
{
14+
/**
15+
* @param \Magento\Framework\DB\Select $select
16+
* @param int|string $now
17+
* @return void
18+
*/
19+
public function applyDate($select, $now)
20+
{
21+
$select->where(
22+
'from_date is null or from_date <= ?',
23+
$now
24+
)->where(
25+
'to_date is null or to_date >= ?',
26+
$now
27+
);
28+
}
29+
}

0 commit comments

Comments
 (0)