Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.

Commit 4942c6f

Browse files
author
Roman Glushko
committed
#141 Decoupling the CustomOption data provider
1 parent 436dee7 commit 4942c6f

9 files changed

+401
-124
lines changed

app/code/Magento/QuoteGraphQl/Model/Resolver/CartItemTypeResolverComposite.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function __construct(array $cartItemTypeResolvers = [])
3030

3131
/**
3232
* {@inheritdoc}
33-
*
33+
*
3434
* @throws GraphQlInputException
3535
*/
3636
public function resolveType(array $data) : string
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Resolver\DataProvider\CartItem;
9+
10+
use Magento\Catalog\Model\Config\Source\ProductPriceOptionsInterface;
11+
use Magento\Framework\Exception\NoSuchEntityException;
12+
use Magento\Store\Api\Data\StoreInterface;
13+
use Magento\Store\Model\Store;
14+
use Magento\Store\Model\StoreManagerInterface;
15+
16+
/**
17+
* Custom Option Data provider
18+
*/
19+
class CustomOptionPriceUnitLabel
20+
{
21+
/**
22+
* @var StoreManagerInterface
23+
*/
24+
private $storeManager;
25+
26+
/**
27+
* @param StoreManagerInterface $storeManager
28+
*/
29+
public function __construct(
30+
StoreManagerInterface $storeManager
31+
) {
32+
$this->storeManager = $storeManager;
33+
}
34+
35+
/**
36+
* Retrieve price value unit
37+
*
38+
* @param string $priceType
39+
* @return string
40+
* @throws NoSuchEntityException
41+
*/
42+
public function getData(string $priceType): string
43+
{
44+
if (ProductPriceOptionsInterface::VALUE_PERCENT == $priceType) {
45+
return '%';
46+
}
47+
48+
return $this->getCurrencySymbol();
49+
}
50+
51+
/**
52+
* Get currency symbol
53+
*
54+
* @return string
55+
* @throws NoSuchEntityException
56+
*/
57+
private function getCurrencySymbol(): string
58+
{
59+
/** @var Store|StoreInterface $store */
60+
$store = $this->storeManager->getStore();
61+
62+
return $store->getBaseCurrency()->getCurrencySymbol();
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Resolver\DataProvider\CartItem\CustomOptionValue;
9+
10+
use Magento\Catalog\Model\Product\Option;
11+
use Magento\Catalog\Model\Product\Option\Type\DefaultType;
12+
use Magento\Quote\Model\Quote\Item as QuoteItem;
13+
use Magento\Quote\Model\Quote\Item\Option as SelectedOption;
14+
15+
/**
16+
* Custom Option Value Data provider
17+
*/
18+
interface CustomOptionValueInterface
19+
{
20+
/**
21+
* Custom Option Type Data Provider
22+
*
23+
* @param QuoteItem $cartItem
24+
* @param Option $option
25+
* @param SelectedOption $selectedOption
26+
* @param DefaultType $optionTypeRenderer
27+
* @return array
28+
*/
29+
public function getData(
30+
QuoteItem $cartItem,
31+
Option $option,
32+
SelectedOption $selectedOption,
33+
DefaultType $optionTypeRenderer
34+
): array;
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Resolver\DataProvider\CartItem\CustomOptionValue;
9+
10+
use Magento\Catalog\Model\Product\Option;
11+
use Magento\Catalog\Model\Product\Option\Type\DefaultType as DefaultOptionType;
12+
use Magento\Framework\Exception\NoSuchEntityException;
13+
use Magento\Quote\Model\Quote\Item as QuoteItem;
14+
use Magento\Quote\Model\Quote\Item\Option as SelectedOption;
15+
use Magento\QuoteGraphQl\Model\Resolver\DataProvider\CartItem\CustomOptionPriceUnitLabel;
16+
17+
/**
18+
* Dropdown Custom Option Value Data provider
19+
*/
20+
class DropdownCustomOptionValue implements CustomOptionValueInterface
21+
{
22+
/**
23+
* @var CustomOptionPriceUnitLabel
24+
*/
25+
private $customOptionPriceUnitLabel;
26+
27+
/**
28+
* @param CustomOptionPriceUnitLabel $customOptionPriceUnitLabel
29+
*/
30+
public function __construct(
31+
CustomOptionPriceUnitLabel $customOptionPriceUnitLabel
32+
) {
33+
$this->customOptionPriceUnitLabel = $customOptionPriceUnitLabel;
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*
39+
* @throws NoSuchEntityException
40+
*/
41+
public function getData(
42+
QuoteItem $cartItem,
43+
Option $option,
44+
SelectedOption $selectedOption,
45+
DefaultOptionType $optionTypeRenderer
46+
): array {
47+
$selectedValue = $selectedOption->getValue();
48+
$optionValue = $option->getValueById($selectedValue);
49+
$optionPriceType = (string) $optionValue->getPriceType();
50+
$priceValueUnits = $this->customOptionPriceUnitLabel->getData($optionPriceType);
51+
52+
$selectedOptionValueData = [
53+
'id' => $selectedOption->getId(),
54+
'label' => $optionTypeRenderer->getFormattedOptionValue($selectedValue),
55+
'price' => [
56+
'type' => strtoupper($optionPriceType),
57+
'units' => $priceValueUnits,
58+
'value' => $optionValue->getPrice(),
59+
]
60+
];
61+
62+
return [$selectedOptionValueData];
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Resolver\DataProvider\CartItem\CustomOptionValue;
9+
10+
use Magento\Catalog\Model\Product\Option;
11+
use Magento\Catalog\Model\Product\Option\Type\DefaultType;
12+
use Magento\Framework\Exception\NoSuchEntityException;
13+
use Magento\Quote\Model\Quote\Item as QuoteItem;
14+
use Magento\Quote\Model\Quote\Item\Option as SelectedOption;
15+
use Magento\QuoteGraphQl\Model\Resolver\DataProvider\CartItem\CustomOptionPriceUnitLabel;
16+
17+
/**
18+
* Multiple Option Value Data provider
19+
*/
20+
class MultipleCustomOptionValue implements CustomOptionValueInterface
21+
{
22+
/**
23+
* @var CustomOptionPriceUnitLabel
24+
*/
25+
private $customOptionPriceUnitLabel;
26+
27+
/**
28+
* @param CustomOptionPriceUnitLabel $customOptionPriceUnitLabel
29+
*/
30+
public function __construct(
31+
CustomOptionPriceUnitLabel $customOptionPriceUnitLabel
32+
) {
33+
$this->customOptionPriceUnitLabel = $customOptionPriceUnitLabel;
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*
39+
* @throws NoSuchEntityException
40+
*/
41+
public function getData(
42+
QuoteItem $cartItem,
43+
Option $option,
44+
SelectedOption $selectedOption,
45+
DefaultType $optionTypeRenderer
46+
): array {
47+
$selectedOptionValueData = [];
48+
$optionIds = explode(',', $selectedOption->getValue());
49+
50+
if (0 === count($optionIds)) {
51+
return $selectedOptionValueData;
52+
}
53+
54+
foreach ($optionIds as $optionId) {
55+
$optionValue = $option->getValueById($optionId);
56+
$priceValueUnits = $this->customOptionPriceUnitLabel->getData($optionValue->getPriceType());
57+
58+
$selectedOptionValueData[] = [
59+
'id' => $selectedOption->getId(),
60+
'label' => $optionValue->getTitle(),
61+
'price' => [
62+
'type' => strtoupper($optionValue->getPriceType()),
63+
'units' => $priceValueUnits,
64+
'value' => $optionValue->getPrice(),
65+
],
66+
];
67+
}
68+
69+
return $selectedOptionValueData;
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Resolver\DataProvider\CartItem\CustomOptionValue;
9+
10+
use Magento\Catalog\Model\Product\Option;
11+
use Magento\Catalog\Model\Product\Option\Type\DefaultType;
12+
use Magento\Framework\Exception\NoSuchEntityException;
13+
use Magento\Quote\Model\Quote\Item as QuoteItem;
14+
use Magento\Quote\Model\Quote\Item\Option as SelectedOption;
15+
use Magento\QuoteGraphQl\Model\Resolver\DataProvider\CartItem\CustomOptionPriceUnitLabel;
16+
17+
/**
18+
* Text Custom Option Value Data provider
19+
*/
20+
class TextCustomOptionValue implements CustomOptionValueInterface
21+
{
22+
/**
23+
* @var CustomOptionPriceUnitLabel
24+
*/
25+
private $customOptionPriceUnitLabel;
26+
27+
/**
28+
* @param CustomOptionPriceUnitLabel $customOptionPriceUnitLabel
29+
*/
30+
public function __construct(
31+
CustomOptionPriceUnitLabel $customOptionPriceUnitLabel
32+
) {
33+
$this->customOptionPriceUnitLabel = $customOptionPriceUnitLabel;
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*
39+
* @throws NoSuchEntityException
40+
*/
41+
public function getData(
42+
QuoteItem $cartItem,
43+
Option $option,
44+
SelectedOption $selectedOption,
45+
DefaultType $optionTypeRenderer
46+
): array {
47+
$priceValueUnits = $this->customOptionPriceUnitLabel->getData($option->getPriceType());
48+
49+
$selectedOptionValueData = [
50+
'id' => $selectedOption->getId(),
51+
'label' => $optionTypeRenderer->getFormattedOptionValue($selectedOption->getValue()),
52+
'price' => [
53+
'type' => strtoupper($option->getPriceType()),
54+
'units' => $priceValueUnits,
55+
'value' => $option->getPrice(),
56+
]
57+
];
58+
59+
return [$selectedOptionValueData];
60+
}
61+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Resolver\DataProvider\CartItem;
9+
10+
use Magento\Catalog\Model\Product\Option;
11+
use Magento\Catalog\Model\Product\Option\Type\DefaultType as DefaultOptionType;
12+
use Magento\Catalog\Model\Product\Option\Type\Select as SelectOptionType;
13+
use Magento\Catalog\Model\Product\Option\Type\Text as TextOptionType;
14+
use Magento\Framework\Exception\LocalizedException;
15+
use Magento\Quote\Model\Quote\Item as QuoteItem;
16+
use Magento\Quote\Model\Quote\Item\Option as SelectedOption;
17+
use Magento\QuoteGraphQl\Model\Resolver\DataProvider\CartItem\CustomOptionValue\CustomOptionValueInterface;
18+
19+
/**
20+
* Custom Option Value Composite Data provider
21+
*/
22+
class CustomOptionValueComposite
23+
{
24+
/**
25+
* @var CustomOptionValueInterface[]
26+
*/
27+
private $customOptionValueTypeProviders;
28+
29+
/**
30+
* @param array $customOptionValueTypeProviders
31+
*/
32+
public function __construct(
33+
array $customOptionValueTypeProviders
34+
) {
35+
$this->customOptionValueTypeProviders = $customOptionValueTypeProviders;
36+
}
37+
38+
/**
39+
* Retrieve custom option values data
40+
*
41+
* @param string $optionType
42+
* @param $cartItem
43+
* @param $option
44+
* @param $selectedOption
45+
* @return array
46+
* @throws LocalizedException
47+
*/
48+
public function getData(
49+
string $optionType,
50+
QuoteItem $cartItem,
51+
Option $option,
52+
SelectedOption $selectedOption
53+
): array {
54+
if (!array_key_exists($optionType, $this->customOptionValueTypeProviders)) {
55+
throw new LocalizedException(__('Option type "%1" is not supported', $optionType));
56+
}
57+
58+
/** @var SelectOptionType|TextOptionType|DefaultOptionType $optionTypeRenderer */
59+
$optionTypeRenderer = $option->groupFactory($optionType)
60+
->setOption($option)
61+
->setConfigurationItem($cartItem)
62+
->setConfigurationItemOption($selectedOption);
63+
64+
$customOptionValueTypeProvider = $this->customOptionValueTypeProviders[$optionType];
65+
66+
return $customOptionValueTypeProvider->getData($cartItem, $option, $selectedOption, $optionTypeRenderer);
67+
}
68+
}

0 commit comments

Comments
 (0)