Skip to content

Commit 1700740

Browse files
committed
Remove value factory. Return value directly
1 parent b062bd9 commit 1700740

File tree

3 files changed

+29
-82
lines changed

3 files changed

+29
-82
lines changed

app/code/Magento/UrlRewriteGraphQl/Model/Resolver/EntityUrl.php

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,16 @@
77

88
namespace Magento\UrlRewriteGraphQl\Model\Resolver;
99

10+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1011
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1112
use Magento\Framework\GraphQl\Config\Element\Field;
12-
use Magento\Framework\GraphQl\Query\Resolver\Value;
13-
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
1413
use Magento\Framework\GraphQl\Query\ResolverInterface;
1514
use Magento\Store\Model\StoreManagerInterface;
1615
use Magento\UrlRewrite\Model\UrlFinderInterface;
1716
use Magento\UrlRewriteGraphQl\Model\Resolver\UrlRewrite\CustomUrlLocatorInterface;
1817

1918
/**
20-
* The resolver returns the canonical URL for a specified product, category or CMS page
19+
* UrlRewrite field resolver, used for GraphQL request processing.
2120
*/
2221
class EntityUrl implements ResolverInterface
2322
{
@@ -31,11 +30,6 @@ class EntityUrl implements ResolverInterface
3130
*/
3231
private $storeManager;
3332

34-
/**
35-
* @var ValueFactory
36-
*/
37-
private $valueFactory;
38-
3933
/**
4034
* @var CustomUrlLocatorInterface
4135
*/
@@ -44,55 +38,48 @@ class EntityUrl implements ResolverInterface
4438
/**
4539
* @param UrlFinderInterface $urlFinder
4640
* @param StoreManagerInterface $storeManager
47-
* @param ValueFactory $valueFactory
4841
* @param CustomUrlLocatorInterface $customUrlLocator
4942
*/
5043
public function __construct(
5144
UrlFinderInterface $urlFinder,
5245
StoreManagerInterface $storeManager,
53-
ValueFactory $valueFactory,
5446
CustomUrlLocatorInterface $customUrlLocator
5547
) {
5648
$this->urlFinder = $urlFinder;
5749
$this->storeManager = $storeManager;
58-
$this->valueFactory = $valueFactory;
5950
$this->customUrlLocator = $customUrlLocator;
6051
}
6152

6253
/**
63-
* {@inheritdoc}
54+
* @inheritdoc
6455
*/
6556
public function resolve(
6657
Field $field,
6758
$context,
6859
ResolveInfo $info,
6960
array $value = null,
7061
array $args = null
71-
) : Value {
72-
$result = function () {
73-
return null;
74-
};
62+
) {
63+
if (!isset($args['url']) || empty(trim($args['url']))) {
64+
throw new GraphQlInputException(__('"url" argument should be specified and not empty'));
65+
}
7566

76-
if (isset($args['url'])) {
77-
$url = $args['url'];
78-
if (substr($url, 0, 1) === '/' && $url !== '/') {
79-
$url = ltrim($url, '/');
80-
}
81-
$customUrl = $this->customUrlLocator->locateUrl($url);
82-
$url = $customUrl ?: $url;
83-
$urlRewrite = $this->findCanonicalUrl($url);
84-
if ($urlRewrite) {
85-
$urlRewriteReturnArray = [
86-
'id' => $urlRewrite->getEntityId(),
87-
'canonical_url' => $urlRewrite->getTargetPath(),
88-
'type' => $this->sanitizeType($urlRewrite->getEntityType())
89-
];
90-
$result = function () use ($urlRewriteReturnArray) {
91-
return $urlRewriteReturnArray;
92-
};
93-
}
67+
$result = null;
68+
$url = $args['url'];
69+
if (substr($url, 0, 1) === '/' && $url !== '/') {
70+
$url = ltrim($url, '/');
71+
}
72+
$customUrl = $this->customUrlLocator->locateUrl($url);
73+
$url = $customUrl ?: $url;
74+
$urlRewrite = $this->findCanonicalUrl($url);
75+
if ($urlRewrite) {
76+
$result = [
77+
'id' => $urlRewrite->getEntityId(),
78+
'canonical_url' => $urlRewrite->getTargetPath(),
79+
'type' => $this->sanitizeType($urlRewrite->getEntityType())
80+
];
9481
}
95-
return $this->valueFactory->create($result);
82+
return $result;
9683
}
9784

9885
/**

app/code/Magento/UrlRewriteGraphQl/Model/Resolver/UrlRewrite.php

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77

88
namespace Magento\UrlRewriteGraphQl\Model\Resolver;
99

10+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1011
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1112
use Magento\Framework\GraphQl\Config\Element\Field;
12-
use Magento\Framework\GraphQl\Query\Resolver\Value;
13-
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
1413
use Magento\Framework\GraphQl\Query\ResolverInterface;
1514
use Magento\Framework\Model\AbstractModel;
1615
use Magento\UrlRewrite\Model\UrlFinderInterface;
@@ -27,48 +26,37 @@ class UrlRewrite implements ResolverInterface
2726
private $urlFinder;
2827

2928
/**
30-
* @var ValueFactory
31-
*/
32-
private $valueFactory;
33-
34-
/**
35-
* @param ValueFactory $valueFactory
3629
* @param UrlFinderInterface $urlFinder
3730
*/
3831
public function __construct(
39-
ValueFactory $valueFactory,
4032
UrlFinderInterface $urlFinder
4133
) {
42-
$this->valueFactory = $valueFactory;
4334
$this->urlFinder = $urlFinder;
4435
}
4536

4637
/**
47-
* {@inheritdoc}
38+
* @inheritdoc
4839
*/
4940
public function resolve(
5041
Field $field,
5142
$context,
5243
ResolveInfo $info,
5344
array $value = null,
5445
array $args = null
55-
): Value {
46+
): array {
5647
if (!isset($value['model'])) {
57-
$result = function () {
58-
return null;
59-
};
60-
return $this->valueFactory->create($result);
48+
throw new GraphQlInputException(__('"model" value should be specified'));
6149
}
6250

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

67-
$urlRewritesCollection = $this->urlFinder->findAllByData([UrlRewriteDTO::ENTITY_ID => $entityId]);
55+
$urlRewriteCollection = $this->urlFinder->findAllByData([UrlRewriteDTO::ENTITY_ID => $entityId]);
6856
$urlRewrites = [];
6957

7058
/** @var UrlRewriteDTO $urlRewrite */
71-
foreach ($urlRewritesCollection as $urlRewrite) {
59+
foreach ($urlRewriteCollection as $urlRewrite) {
7260
if ($urlRewrite->getRedirectType() !== 0) {
7361
continue;
7462
}
@@ -79,11 +67,7 @@ public function resolve(
7967
];
8068
}
8169

82-
$result = function () use ($urlRewrites) {
83-
return $urlRewrites;
84-
};
85-
86-
return $this->valueFactory->create($result);
70+
return $urlRewrites;
8771
}
8872

8973
/**

dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/UrlRewritesTest.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -146,28 +146,4 @@ private function getUrlParameters(string $targetPath): array
146146

147147
return $urlParameters;
148148
}
149-
150-
/**
151-
* @param array $actualResponse
152-
* @param array $assertionMap
153-
*/
154-
private function assertResponseFields($actualResponse, $assertionMap)
155-
{
156-
foreach ($assertionMap as $key => $assertionData) {
157-
$expectedValue = isset($assertionData['expected_value'])
158-
? $assertionData['expected_value']
159-
: $assertionData;
160-
$responseField = isset($assertionData['response_field']) ? $assertionData['response_field'] : $key;
161-
self::assertNotNull(
162-
$expectedValue,
163-
"Value of '{$responseField}' field must not be NULL"
164-
);
165-
self::assertEquals(
166-
$expectedValue,
167-
$actualResponse[$responseField],
168-
"Value of '{$responseField}' field in response does not match expected value: "
169-
. var_export($expectedValue, true)
170-
);
171-
}
172-
}
173149
}

0 commit comments

Comments
 (0)