Skip to content

Commit e79feba

Browse files
committed
1 parent ebbe813 commit e79feba

File tree

7 files changed

+75
-303
lines changed

7 files changed

+75
-303
lines changed

app/code/Magento/SwatchesGraphQl/Model/Resolver/Product/Options/DataProvider/SwatchDataProvider.php

Lines changed: 12 additions & 196 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,12 @@
77

88
namespace Magento\SwatchesGraphQl\Model\Resolver\Product\Options\DataProvider;
99

10-
use Magento\Catalog\Api\Data\ProductInterface;
11-
use Magento\Catalog\Model\Product\Image\UrlBuilder;
12-
use Magento\Framework\Exception\LocalizedException;
13-
use Magento\Framework\Exception\NoSuchEntityException;
14-
use Magento\Framework\Exception\RuntimeException;
15-
use Magento\Framework\GraphQl\Query\EnumLookup;
1610
use Magento\Swatches\Helper\Data as SwatchData;
1711
use Magento\Swatches\Helper\Media as SwatchesMedia;
1812
use Magento\Swatches\Model\Swatch;
1913

2014
/**
21-
* Swatch data provider
15+
* Data provider for options swatches.
2216
*/
2317
class SwatchDataProvider
2418
{
@@ -32,219 +26,41 @@ class SwatchDataProvider
3226
*/
3327
private $swatchMediaHelper;
3428

35-
/**
36-
* @var UrlBuilder
37-
*/
38-
private $imageUrlBuilder;
39-
40-
/**
41-
* @var EnumLookup
42-
*/
43-
private $enumLookup;
44-
4529
/**
4630
* SwatchDataProvider constructor.
4731
*
4832
* @param SwatchData $swatchHelper
4933
* @param SwatchesMedia $swatchMediaHelper
50-
* @param UrlBuilder $imageUrlBuilder
51-
* @param EnumLookup $enumLookup
5234
*/
5335
public function __construct(
5436
SwatchData $swatchHelper,
55-
SwatchesMedia $swatchMediaHelper,
56-
UrlBuilder $imageUrlBuilder,
57-
EnumLookup $enumLookup
37+
SwatchesMedia $swatchMediaHelper
5838
) {
5939
$this->swatchHelper = $swatchHelper;
6040
$this->swatchMediaHelper = $swatchMediaHelper;
61-
$this->imageUrlBuilder = $imageUrlBuilder;
62-
$this->enumLookup = $enumLookup;
6341
}
6442

6543
/**
66-
* Get swatch data
44+
* Returns swatch data by option ID.
6745
*
6846
* @param string $optionId
69-
* @param ProductInterface $product
70-
*
71-
* @return array
72-
*
73-
* @throws LocalizedException
74-
* @throws NoSuchEntityException
75-
* @throws \LogicException
47+
* @return array|null
7648
*/
77-
public function getData(string $optionId, ProductInterface $product): array
49+
public function getData(string $optionId): ?array
7850
{
7951
$swatches = $this->swatchHelper->getSwatchesByOptionsId([$optionId]);
80-
if (!isset($swatches[$optionId], $swatches[$optionId]['type'], $swatches[$optionId]['value'])) {
52+
if (!isset($swatches[$optionId]['type'], $swatches[$optionId]['value'])) {
8153
return null;
8254
}
83-
8455
$type = (int)$swatches[$optionId]['type'];
8556
$value = $swatches[$optionId]['value'];
86-
$thumbnail = null;
87-
88-
// change value & thumbnail if type is 'visual'
57+
$data = ['value' => $value, 'type' => $type];
8958
if ($type === Swatch::SWATCH_TYPE_VISUAL_IMAGE) {
90-
$thumbnail = $this->swatchMediaHelper->getSwatchAttributeImage(Swatch::SWATCH_THUMBNAIL_NAME, $value);
91-
$value = $this->swatchMediaHelper->getSwatchAttributeImage(Swatch::SWATCH_IMAGE_NAME, $value);
59+
$data['thumbnail'] = $this->swatchMediaHelper->getSwatchAttributeImage(
60+
Swatch::SWATCH_THUMBNAIL_NAME,
61+
$value
62+
);
9263
}
93-
94-
$attributeData = $this->getSwatchAttributeDataByOptionId($product, $optionId);
95-
// check if swatch value should be getting from related product image
96-
if (!$this->isUseProductImageForSwatch($attributeData)) {
97-
return $this->getResultArray($value, $type, $thumbnail);
98-
}
99-
100-
// get product with existing image
101-
$variationProduct = $this->getVariationProduct($attributeData, $optionId, $product);
102-
if (null === $variationProduct) {
103-
return $this->getResultArray($value, $type, $thumbnail);
104-
}
105-
106-
// set 'visual' type, because the product image is using as swatch value
107-
$type = Swatch::SWATCH_TYPE_VISUAL_IMAGE;
108-
109-
// get image from child product
110-
$productImage = $this->getSwatchProductImage($variationProduct, Swatch::SWATCH_IMAGE_NAME);
111-
if (null !== $productImage) {
112-
$value = $productImage;
113-
}
114-
115-
// get thumbnail from child product
116-
$productThumbnail = $this->getSwatchProductImage($variationProduct, Swatch::SWATCH_THUMBNAIL_NAME);
117-
if (null !== $productThumbnail) {
118-
$thumbnail = $productThumbnail;
119-
}
120-
121-
return $this->getResultArray($value, $type, $thumbnail);
122-
}
123-
124-
/**
125-
* Get result array
126-
*
127-
* @param string $value
128-
* @param int $type
129-
* @param null|string $thumbnail
130-
*
131-
* @return array
132-
*
133-
* @throws RuntimeException
134-
*/
135-
private function getResultArray(string $value, int $type, ?string $thumbnail)
136-
{
137-
return [
138-
'value' => $value,
139-
'type' => $this->enumLookup->getEnumValueFromField('SwatchTypeEnum', (string)$type),
140-
'thumbnail' => $thumbnail
141-
];
142-
}
143-
144-
/**
145-
* Is swatch images should be getting from related simple products
146-
*
147-
* @param array $attributeData
148-
*
149-
* @return bool
150-
*/
151-
private function isUseProductImageForSwatch(array $attributeData) : bool
152-
{
153-
return isset($attributeData['use_product_image_for_swatch']) && $attributeData['use_product_image_for_swatch'];
154-
}
155-
156-
/**
157-
* Get simple product with first variation swatch image or image
158-
*
159-
* @param array $attributeData
160-
* @param string $optionId
161-
* @param ProductInterface $product
162-
*
163-
* @return ProductInterface|null
164-
*/
165-
private function getVariationProduct(array $attributeData, string $optionId, ProductInterface $product) : ?ProductInterface
166-
{
167-
$attributeCode = $attributeData['attribute_code'];
168-
$requiredAttributes = [
169-
$attributeCode => $optionId
170-
];
171-
172-
$variationProduct = $this->swatchHelper->loadFirstVariationWithSwatchImage($product, $requiredAttributes);
173-
if ($variationProduct instanceof ProductInterface) {
174-
return $variationProduct;
175-
}
176-
177-
$variationProduct = $this->swatchHelper->loadFirstVariationWithImage($product, $requiredAttributes);
178-
if ($variationProduct instanceof ProductInterface) {
179-
return $variationProduct;
180-
}
181-
182-
return null;
183-
}
184-
185-
/**
186-
* Get swatch product image
187-
*
188-
* @param ProductInterface $product
189-
* @param string $imageType
190-
*
191-
* @return string|null
192-
*/
193-
private function getSwatchProductImage(ProductInterface $product, $imageType) : ?string
194-
{
195-
if ($this->isProductHasImage($product, Swatch::SWATCH_IMAGE_NAME)) {
196-
$swatchImageId = $imageType;
197-
$imageAttributes = ['type' => Swatch::SWATCH_IMAGE_NAME];
198-
} elseif ($this->isProductHasImage($product, 'image')) {
199-
$swatchImageId = $imageType == Swatch::SWATCH_IMAGE_NAME ? 'swatch_image_base' : 'swatch_thumb_base';
200-
$imageAttributes = ['type' => 'image'];
201-
}
202-
203-
if (empty($swatchImageId) || empty($imageAttributes['type'])) {
204-
return null;
205-
}
206-
207-
return $this->imageUrlBuilder->getUrl($product->getData($imageAttributes['type']), $swatchImageId);
208-
}
209-
210-
/**
211-
* Is product has image
212-
*
213-
* @param ProductInterface $product
214-
* @param string $imageType
215-
*
216-
* @return bool
217-
*/
218-
private function isProductHasImage(ProductInterface $product, string $imageType) : bool
219-
{
220-
return $product->getData($imageType) !== null && $product->getData($imageType) != SwatchData::EMPTY_IMAGE_VALUE;
221-
}
222-
223-
/**
224-
* Get swatch attribute data by option id
225-
*
226-
* @param ProductInterface $product
227-
* @param string $optionId
228-
*
229-
* @return array
230-
*
231-
* @throws LocalizedException
232-
* @throws \LogicException
233-
* @throws NoSuchEntityException
234-
*/
235-
private function getSwatchAttributeDataByOptionId(ProductInterface $product, string $optionId) : array
236-
{
237-
$attributesData = $this->swatchHelper->getSwatchAttributesAsArray($product);
238-
foreach ($attributesData as $attributeData) {
239-
if (!isset($attributeData['options']) || !is_array($attributeData['options'])) {
240-
continue;
241-
}
242-
243-
if (array_key_exists($optionId, $attributeData['options'])) {
244-
return $attributeData;
245-
}
246-
}
247-
248-
throw new LocalizedException(__(sprintf('Cannot find the attribute with option id "%1".', $optionId)));
64+
return $data;
24965
}
25066
}

app/code/Magento/SwatchesGraphQl/Model/Resolver/Product/Options/SwatchData.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
namespace Magento\SwatchesGraphQl\Model\Resolver\Product\Options;
99

10-
use Magento\Catalog\Api\Data\ProductInterface;
11-
use Magento\Framework\Exception\LocalizedException;
1210
use Magento\Framework\GraphQl\Config\Element\Field;
1311
use Magento\Framework\GraphQl\Query\ResolverInterface;
1412
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
@@ -47,10 +45,6 @@ public function resolve(
4745
array $value = null,
4846
array $args = null
4947
) {
50-
if (!array_key_exists('model', $value) || !$value['model'] instanceof ProductInterface) {
51-
throw new LocalizedException(__('"model" value should be specified'));
52-
}
53-
54-
return $this->swatchDataProvider->getData($value['value_index'], $value['model']);
48+
return $this->swatchDataProvider->getData($value['value_index']);
5549
}
5650
}

app/code/Magento/SwatchesGraphQl/Model/Resolver/Product/Options/SwatchDataTypeResolver.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\SwatchesGraphQl\Model\Resolver\Product\Options;
99

10+
use Magento\Framework\Exception\LocalizedException;
1011
use Magento\Framework\GraphQl\Query\Resolver\TypeResolverInterface;
1112
use Magento\Swatches\Model\Swatch;
1213

@@ -16,19 +17,19 @@
1617
class SwatchDataTypeResolver implements TypeResolverInterface
1718
{
1819
/**
19-
* {@inheritdoc}
20+
* @inheritdoc
2021
*/
2122
public function resolveType(array $data): string
2223
{
2324
switch ($data['type']) {
2425
case Swatch::SWATCH_TYPE_TEXTUAL:
2526
return 'TextSwatchData';
26-
case Swatch::SWATCH_TYPE_VISUAL_COLOR;
27+
case Swatch::SWATCH_TYPE_VISUAL_COLOR:
2728
return 'ColorSwatchData';
28-
case Swatch::SWATCH_TYPE_VISUAL_IMAGE;
29+
case Swatch::SWATCH_TYPE_VISUAL_IMAGE:
2930
return 'ImageSwatchData';
3031
default:
31-
return '';
32+
throw new LocalizedException(__('Unsupported swatch type'));
3233
}
3334
}
3435
}

app/code/Magento/SwatchesGraphQl/etc/schema.graphqls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ type TextSwatchData implements SwatchDataInterface {
4646

4747
type ColorSwatchData implements SwatchDataInterface {
4848

49-
}
49+
}

0 commit comments

Comments
 (0)