Skip to content

Commit

Permalink
Remove value factory. Return value directly
Browse files Browse the repository at this point in the history
  • Loading branch information
rogyar committed Sep 18, 2018
1 parent b062bd9 commit 1700740
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 82 deletions.
57 changes: 22 additions & 35 deletions app/code/Magento/UrlRewriteGraphQl/Model/Resolver/EntityUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@

namespace Magento\UrlRewriteGraphQl\Model\Resolver;

use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\UrlRewrite\Model\UrlFinderInterface;
use Magento\UrlRewriteGraphQl\Model\Resolver\UrlRewrite\CustomUrlLocatorInterface;

/**
* The resolver returns the canonical URL for a specified product, category or CMS page
* UrlRewrite field resolver, used for GraphQL request processing.
*/
class EntityUrl implements ResolverInterface
{
Expand All @@ -31,11 +30,6 @@ class EntityUrl implements ResolverInterface
*/
private $storeManager;

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

/**
* @var CustomUrlLocatorInterface
*/
Expand All @@ -44,55 +38,48 @@ class EntityUrl implements ResolverInterface
/**
* @param UrlFinderInterface $urlFinder
* @param StoreManagerInterface $storeManager
* @param ValueFactory $valueFactory
* @param CustomUrlLocatorInterface $customUrlLocator
*/
public function __construct(
UrlFinderInterface $urlFinder,
StoreManagerInterface $storeManager,
ValueFactory $valueFactory,
CustomUrlLocatorInterface $customUrlLocator
) {
$this->urlFinder = $urlFinder;
$this->storeManager = $storeManager;
$this->valueFactory = $valueFactory;
$this->customUrlLocator = $customUrlLocator;
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) : Value {
$result = function () {
return null;
};
) {
if (!isset($args['url']) || empty(trim($args['url']))) {
throw new GraphQlInputException(__('"url" argument should be specified and not empty'));
}

if (isset($args['url'])) {
$url = $args['url'];
if (substr($url, 0, 1) === '/' && $url !== '/') {
$url = ltrim($url, '/');
}
$customUrl = $this->customUrlLocator->locateUrl($url);
$url = $customUrl ?: $url;
$urlRewrite = $this->findCanonicalUrl($url);
if ($urlRewrite) {
$urlRewriteReturnArray = [
'id' => $urlRewrite->getEntityId(),
'canonical_url' => $urlRewrite->getTargetPath(),
'type' => $this->sanitizeType($urlRewrite->getEntityType())
];
$result = function () use ($urlRewriteReturnArray) {
return $urlRewriteReturnArray;
};
}
$result = null;
$url = $args['url'];
if (substr($url, 0, 1) === '/' && $url !== '/') {
$url = ltrim($url, '/');
}
$customUrl = $this->customUrlLocator->locateUrl($url);
$url = $customUrl ?: $url;
$urlRewrite = $this->findCanonicalUrl($url);
if ($urlRewrite) {
$result = [
'id' => $urlRewrite->getEntityId(),
'canonical_url' => $urlRewrite->getTargetPath(),
'type' => $this->sanitizeType($urlRewrite->getEntityType())
];
}
return $this->valueFactory->create($result);
return $result;
}

/**
Expand Down
30 changes: 7 additions & 23 deletions app/code/Magento/UrlRewriteGraphQl/Model/Resolver/UrlRewrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

namespace Magento\UrlRewriteGraphQl\Model\Resolver;

use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\Model\AbstractModel;
use Magento\UrlRewrite\Model\UrlFinderInterface;
Expand All @@ -27,48 +26,37 @@ class UrlRewrite implements ResolverInterface
private $urlFinder;

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

/**
* @param ValueFactory $valueFactory
* @param UrlFinderInterface $urlFinder
*/
public function __construct(
ValueFactory $valueFactory,
UrlFinderInterface $urlFinder
) {
$this->valueFactory = $valueFactory;
$this->urlFinder = $urlFinder;
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
): Value {
): array {
if (!isset($value['model'])) {
$result = function () {
return null;
};
return $this->valueFactory->create($result);
throw new GraphQlInputException(__('"model" value should be specified'));
}

/** @var AbstractModel $entity */
$entity = $value['model'];
$entityId = $entity->getEntityId();

$urlRewritesCollection = $this->urlFinder->findAllByData([UrlRewriteDTO::ENTITY_ID => $entityId]);
$urlRewriteCollection = $this->urlFinder->findAllByData([UrlRewriteDTO::ENTITY_ID => $entityId]);
$urlRewrites = [];

/** @var UrlRewriteDTO $urlRewrite */
foreach ($urlRewritesCollection as $urlRewrite) {
foreach ($urlRewriteCollection as $urlRewrite) {
if ($urlRewrite->getRedirectType() !== 0) {
continue;
}
Expand All @@ -79,11 +67,7 @@ public function resolve(
];
}

$result = function () use ($urlRewrites) {
return $urlRewrites;
};

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,28 +146,4 @@ private function getUrlParameters(string $targetPath): array

return $urlParameters;
}

/**
* @param array $actualResponse
* @param array $assertionMap
*/
private function assertResponseFields($actualResponse, $assertionMap)
{
foreach ($assertionMap as $key => $assertionData) {
$expectedValue = isset($assertionData['expected_value'])
? $assertionData['expected_value']
: $assertionData;
$responseField = isset($assertionData['response_field']) ? $assertionData['response_field'] : $key;
self::assertNotNull(
$expectedValue,
"Value of '{$responseField}' field must not be NULL"
);
self::assertEquals(
$expectedValue,
$actualResponse[$responseField],
"Value of '{$responseField}' field in response does not match expected value: "
. var_export($expectedValue, true)
);
}
}
}

0 comments on commit 1700740

Please sign in to comment.