Skip to content

Commit de97b0a

Browse files
committed
Replaced hardcoded custom option types and groups with injected array arguments (#24726)
1 parent 0dcb19e commit de97b0a

File tree

2 files changed

+47
-23
lines changed

2 files changed

+47
-23
lines changed

app/code/Magento/Catalog/Model/Product/Option.php

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Catalog\Api\Data\ProductCustomOptionValuesInterfaceFactory;
1212
use Magento\Catalog\Api\Data\ProductInterface;
1313
use Magento\Catalog\Model\Product;
14+
use Magento\Catalog\Model\Product\Option\Type\DefaultType;
1415
use Magento\Catalog\Model\ResourceModel\Product\Option\Value\Collection;
1516
use Magento\Catalog\Pricing\Price\BasePrice;
1617
use Magento\Framework\EntityManager\MetadataPool;
@@ -98,6 +99,16 @@ class Option extends AbstractExtensibleModel implements ProductCustomOptionInter
9899
*/
99100
protected $validatorPool;
100101

102+
/**
103+
* @var DefaultType[]
104+
*/
105+
private $groupsPool;
106+
107+
/**
108+
* @var string[]
109+
*/
110+
private $typesPool;
111+
101112
/**
102113
* @var MetadataPool
103114
*/
@@ -120,6 +131,8 @@ class Option extends AbstractExtensibleModel implements ProductCustomOptionInter
120131
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
121132
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
122133
* @param array $data
134+
* @param array $groupsPool
135+
* @param array $typesPool
123136
* @param ProductCustomOptionValuesInterfaceFactory|null $customOptionValuesFactory
124137
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
125138
*/
@@ -135,12 +148,16 @@ public function __construct(
135148
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
136149
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
137150
array $data = [],
151+
array $groupsPool = [],
152+
array $typesPool = [],
138153
ProductCustomOptionValuesInterfaceFactory $customOptionValuesFactory = null
139154
) {
140155
$this->productOptionValue = $productOptionValue;
141156
$this->optionTypeFactory = $optionFactory;
142-
$this->validatorPool = $validatorPool;
143157
$this->string = $string;
158+
$this->validatorPool = $validatorPool;
159+
$this->groupsPool = $groupsPool;
160+
$this->typesPool = $typesPool;
144161
$this->customOptionValuesFactory = $customOptionValuesFactory ?:
145162
\Magento\Framework\App\ObjectManager::getInstance()->get(ProductCustomOptionValuesInterfaceFactory::class);
146163

@@ -306,44 +323,30 @@ public function setProduct(Product $product = null)
306323
/**
307324
* Get group name of option by given option type
308325
*
309-
* @param string $type
326+
* @param string|null $type
310327
* @return string
311328
*/
312-
public function getGroupByType($type = null)
329+
public function getGroupByType($type = null): string
313330
{
314331
if ($type === null) {
315332
$type = $this->getType();
316333
}
317-
$optionGroupsToTypes = [
318-
self::OPTION_TYPE_FIELD => self::OPTION_GROUP_TEXT,
319-
self::OPTION_TYPE_AREA => self::OPTION_GROUP_TEXT,
320-
self::OPTION_TYPE_FILE => self::OPTION_GROUP_FILE,
321-
self::OPTION_TYPE_DROP_DOWN => self::OPTION_GROUP_SELECT,
322-
self::OPTION_TYPE_RADIO => self::OPTION_GROUP_SELECT,
323-
self::OPTION_TYPE_CHECKBOX => self::OPTION_GROUP_SELECT,
324-
self::OPTION_TYPE_MULTIPLE => self::OPTION_GROUP_SELECT,
325-
self::OPTION_TYPE_DATE => self::OPTION_GROUP_DATE,
326-
self::OPTION_TYPE_DATE_TIME => self::OPTION_GROUP_DATE,
327-
self::OPTION_TYPE_TIME => self::OPTION_GROUP_DATE,
328-
];
329334

330-
return $optionGroupsToTypes[$type] ?? '';
335+
return $this->typesPool[$type] ?? '';
331336
}
332337

333338
/**
334339
* Group model factory
335340
*
336341
* @param string $type Option type
337-
* @return \Magento\Catalog\Model\Product\Option\Type\DefaultType
342+
* @return DefaultType
338343
* @throws LocalizedException
339344
*/
340-
public function groupFactory($type)
345+
public function groupFactory($type): DefaultType
341346
{
342347
$group = $this->getGroupByType($type);
343-
if (!empty($group)) {
344-
return $this->optionTypeFactory->create(
345-
'Magento\Catalog\Model\Product\Option\Type\\' . $this->string->upperCaseWords($group)
346-
);
348+
if (!empty($group) && isset($this->groupsPool[$group])) {
349+
return $this->optionTypeFactory->create($this->groupsPool[$group]);
347350
}
348351
throw new LocalizedException(__('The option type to get group instance is incorrect.'));
349352
}

app/code/Magento/Catalog/etc/di.xml

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@
7272
<preference for="Magento\Catalog\Model\Indexer\Product\Price\UpdateIndexInterface" type="Magento\Catalog\Model\Indexer\Product\Price\InvalidateIndex" />
7373
<preference for="Magento\Catalog\Model\Product\Gallery\ImagesConfigFactoryInterface" type="Magento\Catalog\Model\Product\Gallery\ImagesConfigFactory" />
7474
<preference for="Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface" type="Magento\Catalog\Model\Product\Configuration\Item\ItemResolverComposite" />
75-
<preference for="Magento\Catalog\Api\Data\MassActionInterface" type="\Magento\Catalog\Model\MassAction" />
7675
<type name="Magento\Customer\Model\ResourceModel\Visitor">
7776
<plugin name="catalogLog" type="Magento\Catalog\Model\Plugin\Log" />
7877
</type>
@@ -411,6 +410,28 @@
411410
</argument>
412411
</arguments>
413412
</type>
413+
<type name="Magento\Catalog\Model\Product\Option">
414+
<arguments>
415+
<argument name="groupsPool" xsi:type="array">
416+
<item name="date" xsi:type="string">Magento\Catalog\Model\Product\Option\Type\Date</item>
417+
<item name="file" xsi:type="string">Magento\Catalog\Model\Product\Option\Type\File</item>
418+
<item name="select" xsi:type="string">Magento\Catalog\Model\Product\Option\Type\Select</item>
419+
<item name="text" xsi:type="string">Magento\Catalog\Model\Product\Option\Type\Text</item>
420+
</argument>
421+
<argument name="typesPool" xsi:type="array">
422+
<item name="field" xsi:type="string">text</item>
423+
<item name="area" xsi:type="string">text</item>
424+
<item name="file" xsi:type="string">file</item>
425+
<item name="drop_down" xsi:type="string">select</item>
426+
<item name="radio" xsi:type="string">select</item>
427+
<item name="checkbox" xsi:type="string">select</item>
428+
<item name="multiple" xsi:type="string">select</item>
429+
<item name="date" xsi:type="string">date</item>
430+
<item name="date_time" xsi:type="string">date</item>
431+
<item name="time" xsi:type="string">date</item>
432+
</argument>
433+
</arguments>
434+
</type>
414435
<type name="Magento\Catalog\Model\Product\Option\Validator\Pool">
415436
<arguments>
416437
<argument name="validators" xsi:type="array">

0 commit comments

Comments
 (0)