Skip to content

Commit 31e4834

Browse files
author
Yu Tang
committed
MAGETWO-33663: Refactor ConfigurableProduct module to use mutable data object interfaces
1 parent cb99a50 commit 31e4834

File tree

5 files changed

+187
-26
lines changed

5 files changed

+187
-26
lines changed

app/code/Magento/ConfigurableProduct/Api/Data/OptionInterface.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,75 @@ interface OptionInterface extends \Magento\Framework\Api\ExtensibleDataInterface
1313
*/
1414
public function getId();
1515

16+
/**
17+
* @param int $id
18+
* @return $this
19+
*/
20+
public function setId($id);
21+
1622
/**
1723
* @return string|null
1824
*/
1925
public function getAttributeId();
2026

27+
/**
28+
* @param string $attributeId
29+
* @return $this
30+
*/
31+
public function setAttributeId($attributeId);
32+
2133
/**
2234
* @return string|null
2335
*/
2436
public function getLabel();
2537

38+
/**
39+
* @param string $label
40+
* @return $this
41+
*/
42+
public function setLabel($label);
43+
2644
/**
2745
* @return string|null
2846
*/
2947
public function getType();
3048

49+
/**
50+
* @param string $type
51+
* @return $this
52+
*/
53+
public function setType($type);
54+
3155
/**
3256
* @return int|null
3357
*/
3458
public function getPosition();
3559

60+
/**
61+
* @param int $position
62+
* @return $this
63+
*/
64+
public function setPosition($position);
65+
3666
/**
3767
* @return bool|null
3868
*/
3969
public function getIsUseDefault();
4070

71+
/**
72+
* @param bool $isUseDefault
73+
* @return $this
74+
*/
75+
public function setIsUseDefault($isUseDefault);
76+
4177
/**
4278
* @return \Magento\ConfigurableProduct\Api\Data\OptionValueInterface[]|null
4379
*/
4480
public function getValues();
81+
82+
/**
83+
* @param \Magento\ConfigurableProduct\Api\Data\OptionValueInterface[] $values
84+
* @return $this
85+
*/
86+
public function setValues(array $values = null);
4587
}

app/code/Magento/ConfigurableProduct/Api/Data/OptionValueInterface.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,31 @@ interface OptionValueInterface extends \Magento\Framework\Api\ExtensibleDataInte
1313
*/
1414
public function getPricingValue();
1515

16+
/**
17+
* @param float $pricingValue
18+
* @return $this
19+
*/
20+
public function setPricingValue($pricingValue);
21+
1622
/**
1723
* @return int|null
1824
*/
1925
public function getIsPercent();
2026

27+
/**
28+
* @param int $isPercent
29+
* @return $this
30+
*/
31+
public function setIsPercent($isPercent);
32+
2133
/**
2234
* @return int
2335
*/
2436
public function getValueIndex();
37+
38+
/**
39+
* @param int $valueIndex
40+
* @return $this
41+
*/
42+
public function setValueIndex($valueIndex);
2543
}

app/code/Magento/ConfigurableProduct/Model/OptionRepository.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ class OptionRepository implements \Magento\ConfigurableProduct\Api\OptionReposit
2626
protected $productRepository;
2727

2828
/**
29-
* @var \Magento\ConfigurableProduct\Api\Data\OptionValueDataBuilder
29+
* @var \Magento\ConfigurableProduct\Api\Data\OptionValueInterfaceFactory
3030
*/
31-
protected $optionValueBuilder;
31+
protected $optionValueFactory;
3232

3333
/**
3434
* @var Product\Type\Configurable
@@ -57,7 +57,7 @@ class OptionRepository implements \Magento\ConfigurableProduct\Api\OptionReposit
5757

5858
/**
5959
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
60-
* @param \Magento\ConfigurableProduct\Api\Data\OptionValueDataBuilder $optionValueBuilder
60+
* @param \Magento\ConfigurableProduct\Api\Data\OptionValueInterfaceFactory $optionValueFactory
6161
* @param ConfigurableType $configurableType
6262
* @param Resource\Product\Type\Configurable\Attribute $optionResource
6363
* @param \Magento\Framework\Store\StoreManagerInterface $storeManager
@@ -66,15 +66,15 @@ class OptionRepository implements \Magento\ConfigurableProduct\Api\OptionReposit
6666
*/
6767
public function __construct(
6868
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
69-
\Magento\ConfigurableProduct\Api\Data\OptionValueDataBuilder $optionValueBuilder,
69+
\Magento\ConfigurableProduct\Api\Data\OptionValueInterfaceFactory $optionValueFactory,
7070
\Magento\ConfigurableProduct\Model\Product\Type\Configurable $configurableType,
7171
\Magento\ConfigurableProduct\Model\Resource\Product\Type\Configurable\Attribute $optionResource,
7272
\Magento\Framework\Store\StoreManagerInterface $storeManager,
7373
\Magento\Catalog\Api\ProductAttributeRepositoryInterface $productAttributeRepository,
7474
\Magento\ConfigurableProduct\Model\Product\Type\Configurable\AttributeFactory $configurableAttributeFactory
7575
) {
7676
$this->productRepository = $productRepository;
77-
$this->optionValueBuilder = $optionValueBuilder;
77+
$this->optionValueFactory = $optionValueFactory;
7878
$this->configurableType = $configurableType;
7979
$this->optionResource = $optionResource;
8080
$this->storeManager = $storeManager;
@@ -98,11 +98,12 @@ public function get($productSku, $optionId)
9898
$prices = $configurableAttribute->getPrices();
9999
if (is_array($prices)) {
100100
foreach ($prices as $price) {
101-
$values[] = $this->optionValueBuilder
102-
->setValueIndex($price['value_index'])
101+
/** @var \Magento\ConfigurableProduct\Api\Data\OptionValueInterface $value */
102+
$value = $this->optionValueFactory->create();
103+
$value->setValueIndex($price['value_index'])
103104
->setPricingValue($price['pricing_value'])
104-
->setIsPercent($price['is_percent'])
105-
->create();
105+
->setIsPercent($price['is_percent']);
106+
$values[] = $value;
106107
}
107108
}
108109
$configurableAttribute->setValues($values);
@@ -121,7 +122,7 @@ public function getList($productSku)
121122
$prices = $option->getPrices();
122123
if (is_array($prices)) {
123124
foreach ($prices as $price) {
124-
$values[] = $this->optionValueBuilder
125+
$values[] = $this->optionValueFactory
125126
->setValueIndex($price['value_index'])
126127
->setPricingValue($price['pricing_value'])
127128
->setIsPercent($price['is_percent'])

app/code/Magento/ConfigurableProduct/Model/Product/Type/Configurable/Attribute.php

Lines changed: 75 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,23 @@
1313
* @method Attribute getResource()
1414
* @method int getProductId()
1515
* @method Attribute setProductId(int $value)
16-
* @method Attribute setAttributeId(int $value)
17-
* @method Attribute setPosition(int $value)
1816
* @method Attribute setProductAttribute(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $value)
1917
* @method \Magento\Eav\Model\Entity\Attribute\AbstractAttribute getProductAttribute()
2018
*/
2119
class Attribute extends \Magento\Framework\Model\AbstractExtensibleModel implements
2220
\Magento\ConfigurableProduct\Api\Data\OptionInterface
2321
{
22+
/**#@+
23+
* Constants for field names
24+
*/
25+
const KEY_ATTRIBUTE_ID = 'attribute_id';
26+
const KEY_LABEL = 'label';
27+
const KEY_TYPE = 'type';
28+
const KEY_POSITION = 'position';
29+
const KEY_IS_USE_DEFAULT = 'is_use_default';
30+
const KEY_VALUES = 'values';
31+
/**#@-*/
32+
2433
/**
2534
* Initialize resource model
2635
*
@@ -55,11 +64,11 @@ public function getLabel()
5564
{
5665
if ($this->getData('use_default') && $this->getProductAttribute()) {
5766
return $this->getProductAttribute()->getStoreLabel();
58-
} elseif (is_null($this->getData('label')) && $this->getProductAttribute()) {
59-
$this->setData('label', $this->getProductAttribute()->getStoreLabel());
67+
} elseif (is_null($this->getData(self::KEY_LABEL)) && $this->getProductAttribute()) {
68+
$this->setData(self::KEY_LABEL, $this->getProductAttribute()->getStoreLabel());
6069
}
6170

62-
return $this->getData('label');
71+
return $this->getData(self::KEY_LABEL);
6372
}
6473

6574
/**
@@ -107,7 +116,7 @@ public function deleteByProduct($product)
107116
*/
108117
public function getAttributeId()
109118
{
110-
return $this->getData('attribute_id');
119+
return $this->getData(self::KEY_ATTRIBUTE_ID);
111120
}
112121

113122
/**
@@ -116,7 +125,7 @@ public function getAttributeId()
116125
*/
117126
public function getType()
118127
{
119-
return $this->getData('type');
128+
return $this->getData(self::KEY_TYPE);
120129
}
121130

122131
/**
@@ -125,7 +134,7 @@ public function getType()
125134
*/
126135
public function getPosition()
127136
{
128-
return $this->getData('position');
137+
return $this->getData(self::KEY_POSITION);
129138
}
130139

131140
/**
@@ -134,7 +143,7 @@ public function getPosition()
134143
*/
135144
public function getIsUseDefault()
136145
{
137-
return $this->getData('is_use_default');
146+
return $this->getData(self::KEY_IS_USE_DEFAULT);
138147
}
139148

140149
/**
@@ -143,6 +152,62 @@ public function getIsUseDefault()
143152
*/
144153
public function getValues()
145154
{
146-
return $this->getData('values');
155+
return $this->getData(self::KEY_VALUES);
156+
}
157+
158+
//@codeCoverageIgnoreStart
159+
/**
160+
* @param string $attributeId
161+
* @return $this
162+
*/
163+
public function setAttributeId($attributeId)
164+
{
165+
return $this->setAttributeId(self::KEY_ATTRIBUTE_ID, $attributeId);
166+
}
167+
168+
/**
169+
* @param string $label
170+
* @return $this
171+
*/
172+
public function setLabel($label)
173+
{
174+
return $this->setAttributeId(self::KEY_LABEL, $label);
175+
}
176+
177+
/**
178+
* @param string $type
179+
* @return $this
180+
*/
181+
public function setType($type)
182+
{
183+
return $this->setAttributeId(self::KEY_TYPE, $type);
184+
}
185+
186+
/**
187+
* @param int $position
188+
* @return $this
189+
*/
190+
public function setPosition($position)
191+
{
192+
return $this->setAttributeId(self::KEY_POSITION, $position);
193+
}
194+
195+
/**
196+
* @param bool $isUseDefault
197+
* @return $this
198+
*/
199+
public function setIsUseDefault($isUseDefault)
200+
{
201+
return $this->setAttributeId(self::KEY_IS_USE_DEFAULT, $isUseDefault);
202+
}
203+
204+
/**
205+
* @param \Magento\ConfigurableProduct\Api\Data\OptionValueInterface[] $values
206+
* @return $this
207+
*/
208+
public function setValues(array $values = null)
209+
{
210+
return $this->setAttributeId(self::KEY_VALUES, $values);
147211
}
212+
//@codeCoverageIgnoreEnd
148213
}

app/code/Magento/ConfigurableProduct/Model/Product/Type/Configurable/OptionValue.php

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,68 @@
77

88
namespace Magento\ConfigurableProduct\Model\Product\Type\Configurable;
99

10+
use Magento\ConfigurableProduct\Api\Data\OptionValueInterface;
11+
1012
class OptionValue extends \Magento\Framework\Model\AbstractExtensibleModel implements
1113
\Magento\ConfigurableProduct\Api\Data\OptionValueInterface
1214
{
15+
/**#@+
16+
* Constants for field names
17+
*/
18+
const KEY_PRICING_VALUE = 'pricing_value';
19+
const KEY_IS_PERCENT = 'is_percent';
20+
const KEY_VALUE_INDEX = 'value_index';
21+
/**#@-*/
22+
23+
//@codeCoverageIgnoreStart
1324
/**
1425
* {@inheritdoc}
15-
* @codeCoverageIgnore
1626
*/
1727
public function getPricingValue()
1828
{
19-
return $this->getData('pricing_value');
29+
return $this->getData(self::KEY_PRICING_VALUE);
2030
}
2131

2232
/**
2333
* {@inheritdoc}
24-
* @codeCoverageIgnore
2534
*/
2635
public function getIsPercent()
2736
{
28-
return $this->getData('is_percent');
37+
return $this->getData(self::KEY_IS_PERCENT);
2938
}
3039

3140
/**
3241
* {@inheritdoc}
33-
* @codeCoverageIgnore
3442
*/
3543
public function getValueIndex()
3644
{
37-
return $this->getData('value_index');
45+
return $this->getData(self::KEY_VALUE_INDEX);
46+
}
47+
/**
48+
* @param float $pricingValue
49+
* @return $this
50+
*/
51+
public function setPricingValue($pricingValue)
52+
{
53+
return $this->setData(self::KEY_PRICING_VALUE, $pricingValue);
54+
}
55+
56+
/**
57+
* @param int $isPercent
58+
* @return $this
59+
*/
60+
public function setIsPercent($isPercent)
61+
{
62+
return $this->setData(self::KEY_IS_PERCENT, $isPercent);
63+
}
64+
65+
/**
66+
* @param int $valueIndex
67+
* @return $this
68+
*/
69+
public function setValueIndex($valueIndex)
70+
{
71+
return $this->setData(self::KEY_VALUE_INDEX, $valueIndex);
3872
}
73+
//@codeCoverageIgnoreEnd
3974
}

0 commit comments

Comments
 (0)