Skip to content

Commit

Permalink
graphQl-44: refactoring of format strategy
Browse files Browse the repository at this point in the history
Signed-off-by: vitaliyboyko <v.boyko@atwix.com>
  • Loading branch information
vitaliyboyko committed Sep 22, 2018
1 parent c95cac9 commit 79a968b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Magento\CatalogGraphQl\Model\Resolver\Product;

use Magento\Catalog\Model\Product;
use Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextareaAttribute\FormatFactory;
use Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextAttribute\FormatList;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
Expand All @@ -18,30 +18,33 @@
/**
* Resolve rendered content for attributes where HTML content is allowed
*/
class ProductTextareaAttribute implements ResolverInterface
class ProductTextAttribute implements ResolverInterface
{
const DEFAULT_CONTENT_FORMAT_IDENTIFIER = 'html';
/**
* @var FormatList
*/
private $formatList;

/**
* @var ValueFactory
*/
private $valueFactory;

/**
* @var FormatFactory
* @var string
*/
private $formatFactory;
private $defaultFormat = 'html';

/**
* @param ValueFactory $valueFactory
* @param FormatFactory $formatFactory
* @param FormatList $formatFactory
*/
public function __construct(
ValueFactory $valueFactory,
FormatFactory $formatFactory
FormatList $formatFactory
) {
$this->valueFactory = $valueFactory;
$this->formatFactory = $formatFactory;
$this->formatList = $formatFactory;
}

/**
Expand All @@ -55,22 +58,16 @@ public function resolve(
array $args = null
): Value {
if (!isset($value['model'])) {
$result = function () {
return null;
};
$result = [];
return $this->valueFactory->create($result);
}

/* @var $product Product */
$product = $value['model'];
$fieldName = $field->getName();
$formatIdentifier = $args['format'] ?? self::DEFAULT_CONTENT_FORMAT_IDENTIFIER;
$format = $this->formatFactory->create($formatIdentifier);
$attribute = ['content' => $format->getContent($product, $fieldName)];

$result = function () use ($attribute) {
return $attribute;
};
$formatIdentifier = $args['format'] ?? $this->defaultFormat;
$format = $this->formatList->create($formatIdentifier);
$result = ['content' => $format->getContent($product, $fieldName)];

return $this->valueFactory->create($result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
declare(strict_types=1);

namespace Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextareaAttribute;
namespace Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextAttribute;

use Magento\Catalog\Model\Product as ModelProduct;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,39 @@

use Magento\Framework\ObjectManagerInterface;

class FormatFactory
class FormatList
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;

/**
* @var string
*/
private $formats;

/**
* @param ObjectManagerInterface $objectManager
* @param array $formats
*/
public function __construct(ObjectManagerInterface $objectManager)
{
public function __construct(
ObjectManagerInterface $objectManager,
array $formats
) {
$this->objectManager = $objectManager;
$this->formats = $formats;
}

/**
* @param string $formatIdentifier
* @param array $data
* @return FormatInterface
*/
public function create(string $formatIdentifier, $data = []) : FormatInterface
public function create(string $formatIdentifier) : FormatInterface
{
$formatClassName = 'Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextareaAttribute\\' . ucfirst($formatIdentifier);
$formatInstance = $this->objectManager->create($formatClassName, $data);
if (false == $formatInstance instanceof FormatInterface) {
throw new \InvalidArgumentException(
$formatInstance . ' is not instance of \Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextareaAttribute\FormatInterface'
);
}
$formatClassName = 'Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextAttribute\\' . ucfirst($formatIdentifier);
$formatInstance = $this->objectManager->get($formatClassName);

return $formatInstance;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
declare(strict_types=1);

namespace Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextareaAttribute;
namespace Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextAttribute;

use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
use Magento\Catalog\Helper\Output as OutputHelper;
Expand Down
7 changes: 7 additions & 0 deletions app/code/Magento/CatalogGraphQl/etc/graphql/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@
<argument name="collectionProcessor" xsi:type="object">Magento\Catalog\Model\Api\SearchCriteria\ProductCollectionProcessor</argument>
</arguments>
</type>
<type name="Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextAttribute\FormatList">
<arguments>
<argument name="formats" xsi:type="array">
<item name="html" xsi:type="string">Magento\CatalogGraphQl\Model\Resolver\Product\ProductTextAttribute\Html</item>
</argument>
</arguments>
</type>
<virtualType name="Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\ProductFilterProcessor" type="Magento\Eav\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor">
<arguments>
<argument name="customFilters" xsi:type="array">
Expand Down
12 changes: 6 additions & 6 deletions app/code/Magento/CatalogGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ interface ProductInterface @typeResolver(class: "Magento\\CatalogGraphQl\\Model\
id: Int @doc(description: "The ID number assigned to the product") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\EntityIdToId")
name: String @doc(description: "The product name. Customers use this name to identify the product.")
sku: String @doc(description: "A number or code assigned to a product to identify the product, options, price, and manufacturer")
description: ProductTextareaAttribute @doc(description: "Detailed information about the product. The value can include simple HTML tags.")
short_description: ProductTextareaAttribute @doc(description: "A short description of the product. Its use depends on the theme.")
description: ProductTextAttribute @doc(description: "Detailed information about the product. The value can include simple HTML tags.")
short_description: ProductTextAttribute @doc(description: "A short description of the product. Its use depends on the theme.")
special_price: Float @doc(description: "The discounted price of the product")
special_from_date: String @doc(description: "The beginning date that a product has a special price")
special_to_date: String @doc(description: "The end date that a product has a special price")
Expand Down Expand Up @@ -552,9 +552,9 @@ type SortFields @doc(description: "SortFields contains a default value for sort
options: [SortField] @doc(description: "Available sort fields")
}

type ProductTextareaAttribute {
type ProductTextAttribute {
content (
format: String! @doc(description: "The format of content")
): String
@resolver(class: "\\Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\ProductTextareaAttribute")
format: String @doc(description: "The format of content")
): String @doc(description: "The format of content")
@resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\ProductTextAttribute")
}

0 comments on commit 79a968b

Please sign in to comment.