Skip to content

Commit 79a968b

Browse files
author
vitaliyboyko
committed
graphQl-44: refactoring of format strategy
Signed-off-by: vitaliyboyko <v.boyko@atwix.com>
1 parent c95cac9 commit 79a968b

File tree

6 files changed

+46
-38
lines changed

6 files changed

+46
-38
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/ProductTextAttribute.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace Magento\CatalogGraphQl\Model\Resolver\Product;
99

1010
use Magento\Catalog\Model\Product;
11-
use Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextareaAttribute\FormatFactory;
11+
use Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextAttribute\FormatList;
1212
use Magento\Framework\GraphQl\Config\Element\Field;
1313
use Magento\Framework\GraphQl\Query\Resolver\Value;
1414
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
@@ -18,30 +18,33 @@
1818
/**
1919
* Resolve rendered content for attributes where HTML content is allowed
2020
*/
21-
class ProductTextareaAttribute implements ResolverInterface
21+
class ProductTextAttribute implements ResolverInterface
2222
{
23-
const DEFAULT_CONTENT_FORMAT_IDENTIFIER = 'html';
23+
/**
24+
* @var FormatList
25+
*/
26+
private $formatList;
2427

2528
/**
2629
* @var ValueFactory
2730
*/
2831
private $valueFactory;
2932

3033
/**
31-
* @var FormatFactory
34+
* @var string
3235
*/
33-
private $formatFactory;
36+
private $defaultFormat = 'html';
3437

3538
/**
3639
* @param ValueFactory $valueFactory
37-
* @param FormatFactory $formatFactory
40+
* @param FormatList $formatFactory
3841
*/
3942
public function __construct(
4043
ValueFactory $valueFactory,
41-
FormatFactory $formatFactory
44+
FormatList $formatFactory
4245
) {
4346
$this->valueFactory = $valueFactory;
44-
$this->formatFactory = $formatFactory;
47+
$this->formatList = $formatFactory;
4548
}
4649

4750
/**
@@ -55,22 +58,16 @@ public function resolve(
5558
array $args = null
5659
): Value {
5760
if (!isset($value['model'])) {
58-
$result = function () {
59-
return null;
60-
};
61+
$result = [];
6162
return $this->valueFactory->create($result);
6263
}
6364

6465
/* @var $product Product */
6566
$product = $value['model'];
6667
$fieldName = $field->getName();
67-
$formatIdentifier = $args['format'] ?? self::DEFAULT_CONTENT_FORMAT_IDENTIFIER;
68-
$format = $this->formatFactory->create($formatIdentifier);
69-
$attribute = ['content' => $format->getContent($product, $fieldName)];
70-
71-
$result = function () use ($attribute) {
72-
return $attribute;
73-
};
68+
$formatIdentifier = $args['format'] ?? $this->defaultFormat;
69+
$format = $this->formatList->create($formatIdentifier);
70+
$result = ['content' => $format->getContent($product, $fieldName)];
7471

7572
return $this->valueFactory->create($result);
7673
}

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/ProductTextAttribute/FormatInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextareaAttribute;
8+
namespace Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextAttribute;
99

1010
use Magento\Catalog\Model\Product as ModelProduct;
1111

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/ProductTextAttribute/FormatList.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,39 @@
99

1010
use Magento\Framework\ObjectManagerInterface;
1111

12-
class FormatFactory
12+
class FormatList
1313
{
1414
/**
1515
* @var ObjectManagerInterface
1616
*/
1717
private $objectManager;
1818

19+
/**
20+
* @var string
21+
*/
22+
private $formats;
23+
1924
/**
2025
* @param ObjectManagerInterface $objectManager
26+
* @param array $formats
2127
*/
22-
public function __construct(ObjectManagerInterface $objectManager)
23-
{
28+
public function __construct(
29+
ObjectManagerInterface $objectManager,
30+
array $formats
31+
) {
2432
$this->objectManager = $objectManager;
33+
$this->formats = $formats;
2534
}
2635

2736
/**
2837
* @param string $formatIdentifier
29-
* @param array $data
3038
* @return FormatInterface
3139
*/
32-
public function create(string $formatIdentifier, $data = []) : FormatInterface
40+
public function create(string $formatIdentifier) : FormatInterface
3341
{
34-
$formatClassName = 'Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextareaAttribute\\' . ucfirst($formatIdentifier);
35-
$formatInstance = $this->objectManager->create($formatClassName, $data);
36-
if (false == $formatInstance instanceof FormatInterface) {
37-
throw new \InvalidArgumentException(
38-
$formatInstance . ' is not instance of \Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextareaAttribute\FormatInterface'
39-
);
40-
}
42+
$formatClassName = 'Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextAttribute\\' . ucfirst($formatIdentifier);
43+
$formatInstance = $this->objectManager->get($formatClassName);
44+
4145
return $formatInstance;
4246
}
4347
}

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/ProductTextAttribute/Html.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextareaAttribute;
8+
namespace Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextAttribute;
99

1010
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
1111
use Magento\Catalog\Helper\Output as OutputHelper;

app/code/Magento/CatalogGraphQl/etc/graphql/di.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@
6363
<argument name="collectionProcessor" xsi:type="object">Magento\Catalog\Model\Api\SearchCriteria\ProductCollectionProcessor</argument>
6464
</arguments>
6565
</type>
66+
<type name="Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextAttribute\FormatList">
67+
<arguments>
68+
<argument name="formats" xsi:type="array">
69+
<item name="html" xsi:type="string">Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextAttribute\Html</item>
70+
</argument>
71+
</arguments>
72+
</type>
6673
<virtualType name="Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\ProductFilterProcessor" type="Magento\Eav\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor">
6774
<arguments>
6875
<argument name="customFilters" xsi:type="array">

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ interface ProductInterface @typeResolver(class: "Magento\\CatalogGraphQl\\Model\
248248
id: Int @doc(description: "The ID number assigned to the product") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\EntityIdToId")
249249
name: String @doc(description: "The product name. Customers use this name to identify the product.")
250250
sku: String @doc(description: "A number or code assigned to a product to identify the product, options, price, and manufacturer")
251-
description: ProductTextareaAttribute @doc(description: "Detailed information about the product. The value can include simple HTML tags.")
252-
short_description: ProductTextareaAttribute @doc(description: "A short description of the product. Its use depends on the theme.")
251+
description: ProductTextAttribute @doc(description: "Detailed information about the product. The value can include simple HTML tags.")
252+
short_description: ProductTextAttribute @doc(description: "A short description of the product. Its use depends on the theme.")
253253
special_price: Float @doc(description: "The discounted price of the product")
254254
special_from_date: String @doc(description: "The beginning date that a product has a special price")
255255
special_to_date: String @doc(description: "The end date that a product has a special price")
@@ -552,9 +552,9 @@ type SortFields @doc(description: "SortFields contains a default value for sort
552552
options: [SortField] @doc(description: "Available sort fields")
553553
}
554554

555-
type ProductTextareaAttribute {
555+
type ProductTextAttribute {
556556
content (
557-
format: String! @doc(description: "The format of content")
558-
): String
559-
@resolver(class: "\\Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\ProductTextareaAttribute")
557+
format: String @doc(description: "The format of content")
558+
): String @doc(description: "The format of content")
559+
@resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\ProductTextAttribute")
560560
}

0 commit comments

Comments
 (0)