Skip to content

Commit 6ca1cf3

Browse files
author
Idolov, Stanislav(sidolov)
committed
Merge pull request #270 from magento-folks/catalog_promotions
[Folks] CatalogRule module refactoring
2 parents 59d576e + 5ccdcbd commit 6ca1cf3

File tree

30 files changed

+1361
-256
lines changed

30 files changed

+1361
-256
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\CatalogRule\Api;
7+
8+
interface CatalogRuleRepositoryInterface
9+
{
10+
/**
11+
* @api
12+
* @param \Magento\CatalogRule\Api\Data\RuleInterface $rule
13+
* @return \Magento\CatalogRule\Api\Data\RuleInterface
14+
* @throws \Magento\Framework\Exception\CouldNotSaveException
15+
*/
16+
public function save(\Magento\CatalogRule\Api\Data\RuleInterface $rule);
17+
18+
/**
19+
* @api
20+
* @param int $ruleId
21+
* @return \Magento\CatalogRule\Api\Data\RuleInterface
22+
* @throws \Magento\Framework\Exception\NoSuchEntityException
23+
*/
24+
public function get($ruleId);
25+
26+
/**
27+
* @api
28+
* @param \Magento\CatalogRule\Api\Data\RuleInterface $rule
29+
* @return bool
30+
* @throws \Magento\Framework\Exception\CouldNotDeleteException
31+
*/
32+
public function delete(\Magento\CatalogRule\Api\Data\RuleInterface $rule);
33+
34+
/**
35+
* @api
36+
* @param int $ruleId
37+
* @return bool
38+
* @throws \Magento\Framework\Exception\CouldNotDeleteException
39+
*/
40+
public function deleteById($ruleId);
41+
}
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\CatalogRule\Api\Data;
7+
8+
/**
9+
* @api
10+
*/
11+
interface RuleInterface extends \Magento\Framework\Api\CustomAttributesDataInterface
12+
{
13+
/**#@+
14+
* Constants defined for keys of data array
15+
*/
16+
const RULE_ID = 'rule_id';
17+
18+
const NAME = 'name';
19+
20+
const DESCRIPTION = 'description';
21+
22+
const IS_ACTIVE = 'is_active';
23+
24+
const CONDITIONS_SERIALIZED = 'conditions_serialized';
25+
26+
const ACTIONS_SERIALIZED = 'actions_serialized';
27+
28+
const STOP_RULES_PROCESSING = 'stop_rules_processing';
29+
30+
const SORT_ORDER = 'sort_order';
31+
32+
const SIMPLE_ACTION = 'simple_action';
33+
34+
const DISCOUNT_AMOUNT = 'discount_amount';
35+
36+
/**#@-*/
37+
38+
/**
39+
* Returns rule id field
40+
*
41+
* @return int
42+
*/
43+
public function getRuleId();
44+
45+
/**
46+
* @param int $ruleId
47+
* @return $this
48+
*/
49+
public function setRuleId($ruleId);
50+
51+
/**
52+
* Returns rule name
53+
*
54+
* @return string
55+
*/
56+
public function getName();
57+
58+
/**
59+
* @param string $name
60+
* @return $this
61+
*/
62+
public function setName($name);
63+
64+
/**
65+
* Returns rule description
66+
*
67+
* @return string
68+
*/
69+
public function getDescription();
70+
71+
/**
72+
* @param string $description
73+
* @return $this
74+
*/
75+
public function setDescription($description);
76+
77+
/**
78+
* Returns rule activity flag
79+
*
80+
* @return int
81+
*/
82+
public function getIsActive();
83+
84+
/**
85+
* @param int $isActive
86+
* @return $this
87+
*/
88+
public function setIsActive($isActive);
89+
90+
/**
91+
* Returns serialized rule condition
92+
*
93+
* @return string
94+
*/
95+
public function getConditionsSerialized();
96+
97+
/**
98+
* @param string $conditions
99+
* @return $this
100+
*/
101+
public function setConditionsSerialized($conditions);
102+
103+
/**
104+
* Returns serialized rule actions
105+
*
106+
* @return string
107+
*/
108+
public function getActionsSerialized();
109+
110+
/**
111+
* @param string $actions
112+
* @return $this
113+
*/
114+
public function setActionsSerialized($actions);
115+
116+
/**
117+
* Returns stop rule processing flag
118+
*
119+
* @return int
120+
*/
121+
public function getStopRulesProcessing();
122+
123+
/**
124+
* @param int $isStopProcessing
125+
* @return $this
126+
*/
127+
public function setStopRulesProcessing($isStopProcessing);
128+
129+
/**
130+
* Returns rule sort order
131+
*
132+
* @return int
133+
*/
134+
public function getSortOrder();
135+
136+
/**
137+
* @param int $sortOrder
138+
* @return $this
139+
*/
140+
public function setSortOrder($sortOrder);
141+
142+
/**
143+
* Returns rule simple action
144+
*
145+
* @return string
146+
*/
147+
public function getSimpleAction();
148+
149+
/**
150+
* @param string $action
151+
* @return $this
152+
*/
153+
public function setSimpleAction($action);
154+
155+
/**
156+
* Returns discount amount
157+
*
158+
* @return float
159+
*/
160+
public function getDiscountAmount();
161+
162+
/**
163+
* @param float $amount
164+
* @return $this
165+
*/
166+
public function setDiscountAmount($amount);
167+
168+
/**
169+
* Retrieve existing extension attributes object or create a new one.
170+
*
171+
* @return \Magento\CatalogRule\Api\Data\RuleExtensionInterface|null
172+
*/
173+
public function getExtensionAttributes();
174+
175+
/**
176+
* Set an extension attributes object.
177+
*
178+
* @param \Magento\CatalogRule\Api\Data\RuleExtensionInterface $extensionAttributes
179+
* @return $this
180+
*/
181+
public function setExtensionAttributes(\Magento\CatalogRule\Api\Data\RuleExtensionInterface $extensionAttributes);
182+
}

app/code/Magento/CatalogRule/Controller/Adminhtml/Promo/Catalog/Delete.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ public function execute()
1818
$id = $this->getRequest()->getParam('id');
1919
if ($id) {
2020
try {
21-
/** @var \Magento\CatalogRule\Model\Rule $model */
22-
$model = $this->_objectManager->create('Magento\CatalogRule\Model\Rule');
23-
$model->load($id);
24-
$model->delete();
21+
/** @var \Magento\CatalogRule\Api\CatalogRuleRepositoryInterface $ruleRepository */
22+
$ruleRepository = $this->_objectManager->create(
23+
'Magento\CatalogRule\Api\CatalogRuleRepositoryInterface'
24+
);
25+
$ruleRepository->deleteById($id);
26+
2527
$this->_objectManager->create('Magento\CatalogRule\Model\Flag')->loadSelf()->setState(1)->save();
2628
$this->messageManager->addSuccess(__('You deleted the rule.'));
2729
$this->_redirect('catalog_rule/*/');

app/code/Magento/CatalogRule/Controller/Adminhtml/Promo/Catalog/Edit.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,19 @@ class Edit extends \Magento\CatalogRule\Controller\Adminhtml\Promo\Catalog
1414
public function execute()
1515
{
1616
$id = $this->getRequest()->getParam('id');
17+
18+
/** @var \Magento\CatalogRule\Model\Rule $model */
1719
$model = $this->_objectManager->create('Magento\CatalogRule\Model\Rule');
1820

21+
/** @var \Magento\CatalogRule\Api\CatalogRuleRepositoryInterface $ruleRepository */
22+
$ruleRepository = $this->_objectManager->create(
23+
'Magento\CatalogRule\Api\CatalogRuleRepositoryInterface'
24+
);
25+
1926
if ($id) {
20-
$model->load($id);
21-
if (!$model->getRuleId()) {
27+
try {
28+
$model = $ruleRepository->get($id);
29+
} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) {
2230
$this->messageManager->addError(__('This rule no longer exists.'));
2331
$this->_redirect('catalog_rule/*');
2432
return;
@@ -39,12 +47,9 @@ public function execute()
3947
$this->_view->getPage()->getConfig()->getTitle()->prepend(
4048
$model->getRuleId() ? $model->getName() : __('New Catalog Price Rule')
4149
);
42-
$this->_view->getLayout()->getBlock(
43-
'promo_catalog_edit'
44-
)->setData(
45-
'action',
46-
$this->getUrl('catalog_rule/promo_catalog/save')
47-
);
50+
$this->_view->getLayout()
51+
->getBlock('promo_catalog_edit')
52+
->setData('action', $this->getUrl('catalog_rule/promo_catalog/save'));
4853

4954
$breadcrumb = $id ? __('Edit Rule') : __('New Rule');
5055
$this->_addBreadcrumb($breadcrumb, $breadcrumb);

app/code/Magento/CatalogRule/Controller/Adminhtml/Promo/Catalog/NewActionHtml.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,12 @@ public function execute()
1919
$typeArr = explode('|', str_replace('-', '/', $this->getRequest()->getParam('type')));
2020
$type = $typeArr[0];
2121

22-
$model = $this->_objectManager->create(
23-
$type
24-
)->setId(
25-
$id
26-
)->setType(
27-
$type
28-
)->setRule(
29-
$this->_objectManager->create('Magento\CatalogRule\Model\Rule')
30-
)->setPrefix(
31-
'actions'
32-
);
22+
$model = $this->_objectManager->create($type)
23+
->setId($id)
24+
->setType($type)
25+
->setRule($this->_objectManager->create('Magento\CatalogRule\Model\Rule'))
26+
->setPrefix('actions');
27+
3328
if (!empty($typeArr[1])) {
3429
$model->setAttribute($typeArr[1]);
3530
}

app/code/Magento/CatalogRule/Controller/Adminhtml/Promo/Catalog/NewConditionHtml.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,12 @@ public function execute()
1919
$typeArr = explode('|', str_replace('-', '/', $this->getRequest()->getParam('type')));
2020
$type = $typeArr[0];
2121

22-
$model = $this->_objectManager->create(
23-
$type
24-
)->setId(
25-
$id
26-
)->setType(
27-
$type
28-
)->setRule(
29-
$this->_objectManager->create('Magento\CatalogRule\Model\Rule')
30-
)->setPrefix(
31-
'conditions'
32-
);
22+
$model = $this->_objectManager->create($type)
23+
->setId($id)
24+
->setType($type)
25+
->setRule($this->_objectManager->create('Magento\CatalogRule\Model\Rule'))
26+
->setPrefix('conditions');
27+
3328
if (!empty($typeArr[1])) {
3429
$model->setAttribute($typeArr[1]);
3530
}

app/code/Magento/CatalogRule/Controller/Adminhtml/Promo/Catalog/Save.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,16 @@ class Save extends \Magento\CatalogRule\Controller\Adminhtml\Promo\Catalog
1717
public function execute()
1818
{
1919
if ($this->getRequest()->getPostValue()) {
20+
21+
/** @var \Magento\CatalogRule\Api\CatalogRuleRepositoryInterface $ruleRepository */
22+
$ruleRepository = $this->_objectManager->create(
23+
'Magento\CatalogRule\Api\CatalogRuleRepositoryInterface'
24+
);
25+
26+
/** @var \Magento\CatalogRule\Model\Rule $model */
27+
$model = $this->_objectManager->create('Magento\CatalogRule\Model\Rule');
28+
2029
try {
21-
/** @var \Magento\CatalogRule\Model\Rule $model */
22-
$model = $this->_objectManager->create('Magento\CatalogRule\Model\Rule');
2330
$this->_eventManager->dispatch(
2431
'adminhtml_controller_catalogrule_prepare_save',
2532
['request' => $this->getRequest()]
@@ -33,10 +40,7 @@ public function execute()
3340
$data = $inputFilter->getUnescaped();
3441
$id = $this->getRequest()->getParam('rule_id');
3542
if ($id) {
36-
$model->load($id);
37-
if ($id != $model->getId()) {
38-
throw new LocalizedException(__('Wrong rule specified.'));
39-
}
43+
$model = $ruleRepository->get($id);
4044
}
4145

4246
$validateResult = $model->validateData(new \Magento\Framework\DataObject($data));
@@ -55,8 +59,7 @@ public function execute()
5559
$model->loadPost($data);
5660

5761
$this->_objectManager->get('Magento\Backend\Model\Session')->setPageData($model->getData());
58-
59-
$model->save();
62+
$ruleRepository->save($model);
6063

6164
$this->messageManager->addSuccess(__('You saved the rule.'));
6265
$this->_objectManager->get('Magento\Backend\Model\Session')->setPageData(false);
@@ -85,7 +88,7 @@ public function execute()
8588
__('Something went wrong while saving the rule data. Please review the error log.')
8689
);
8790
$this->_objectManager->get('Psr\Log\LoggerInterface')->critical($e);
88-
$this->_objectManager->get('Magento\Backend\Model\Session')->setPageData($data);
91+
$this->_objectManager->get('Magento\Backend\Model\Session')->setPageData($model->getData());
8992
$this->_redirect('catalog_rule/*/edit', ['id' => $this->getRequest()->getParam('rule_id')]);
9093
return;
9194
}

0 commit comments

Comments
 (0)