Skip to content

Commit e40257a

Browse files
author
Krishna Kumar
committed
Fixed the static test fails
1 parent 85b2cb1 commit e40257a

File tree

4 files changed

+156
-249
lines changed

4 files changed

+156
-249
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\UrlRewriteGraphQl\Model\Resolver;
9+
10+
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
11+
use Magento\UrlRewrite\Model\UrlFinderInterface;
12+
13+
abstract class AbstractEntityUrl {
14+
15+
/**
16+
* @var UrlFinderInterface
17+
*/
18+
private $urlFinder;
19+
20+
protected $redirectType = 0;
21+
22+
/**
23+
* AbstractUrlHelper constructor.
24+
* @param UrlFinderInterface $urlFinder
25+
*/
26+
public function __construct (
27+
UrlFinderInterface $urlFinder
28+
) {
29+
$this->urlFinder = $urlFinder;
30+
}
31+
32+
/**
33+
* Handle custom urls with and without redirects
34+
*
35+
* @param UrlRewrite $finalUrlRewrite
36+
* @param int $storeId
37+
* @return array|null
38+
*/
39+
protected function rewriteCustomUrls(UrlRewrite $finalUrlRewrite, int $storeId): ?array
40+
{
41+
if ($finalUrlRewrite->getEntityType() === 'custom' || !($finalUrlRewrite->getEntityId() > 0)) {
42+
$finalCustomUrlRewrite = clone $finalUrlRewrite;
43+
$finalUrlRewrite = $this->findFinalUrl($finalCustomUrlRewrite->getTargetPath(), $storeId, true);
44+
$relativeUrl =
45+
$finalCustomUrlRewrite->getRedirectType() == 0
46+
? $finalCustomUrlRewrite->getRequestPath() : $finalUrlRewrite->getRequestPath();
47+
return [
48+
'id' => $finalUrlRewrite->getEntityId(),
49+
'entity_uid' => $this->idEncoder->encode((string)$finalUrlRewrite->getEntityId()),
50+
'canonical_url' => $relativeUrl,
51+
'relative_url' => $relativeUrl,
52+
'redirectCode' => $finalCustomUrlRewrite->getRedirectType(),
53+
'type' => $this->sanitizeType($finalUrlRewrite->getEntityType())
54+
];
55+
}
56+
return null;
57+
}
58+
59+
/**
60+
* Find the final url passing through all redirects if any
61+
*
62+
* @param string $requestPath
63+
* @param int $storeId
64+
* @param bool $findCustom
65+
* @return UrlRewrite|null
66+
*/
67+
protected function findFinalUrl(string $requestPath, int $storeId, bool $findCustom = false): ?UrlRewrite
68+
{
69+
$urlRewrite = $this->findUrlFromRequestPath($requestPath, $storeId);
70+
if ($urlRewrite) {
71+
$this->redirectType = $urlRewrite->getRedirectType();
72+
while ($urlRewrite && $urlRewrite->getRedirectType() > 0) {
73+
$urlRewrite = $this->findUrlFromRequestPath($urlRewrite->getTargetPath(), $storeId);
74+
}
75+
} else {
76+
$urlRewrite = $this->findUrlFromTargetPath($requestPath, $storeId);
77+
}
78+
if ($urlRewrite && ($findCustom && !$urlRewrite->getEntityId() && !$urlRewrite->getIsAutogenerated())) {
79+
$urlRewrite = $this->findUrlFromTargetPath($urlRewrite->getTargetPath(), $storeId);
80+
}
81+
82+
return $urlRewrite;
83+
}
84+
85+
/**
86+
* Find a url from a request url on the current store
87+
*
88+
* @param string $requestPath
89+
* @param int $storeId
90+
* @return UrlRewrite|null
91+
*/
92+
protected function findUrlFromRequestPath(string $requestPath, int $storeId): ?UrlRewrite
93+
{
94+
return $this->urlFinder->findOneByData(
95+
[
96+
'request_path' => $requestPath,
97+
'store_id' => $storeId
98+
]
99+
);
100+
}
101+
102+
/**
103+
* Find a url from a target url on the current store
104+
*
105+
* @param string $targetPath
106+
* @param int $storeId
107+
* @return UrlRewrite|null
108+
*/
109+
protected function findUrlFromTargetPath(string $targetPath, int $storeId): ?UrlRewrite
110+
{
111+
return $this->urlFinder->findOneByData(
112+
[
113+
'target_path' => $targetPath,
114+
'store_id' => $storeId
115+
]
116+
);
117+
}
118+
119+
/**
120+
* Sanitize the type to fit schema specifications
121+
*
122+
* @param string $type
123+
* @return string
124+
*/
125+
protected function sanitizeType(string $type) : string
126+
{
127+
return strtoupper(str_replace('-', '_', $type));
128+
}
129+
}

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

Lines changed: 11 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -9,52 +9,33 @@
99

1010
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1111
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
12-
use Magento\Framework\GraphQl\Query\Uid;
1312
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1413
use Magento\Framework\GraphQl\Config\Element\Field;
1514
use Magento\Framework\GraphQl\Query\ResolverInterface;
1615
use Magento\UrlRewrite\Model\UrlFinderInterface;
17-
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
16+
use Magento\UrlRewriteGraphQl\Model\Resolver\AbstractEntityUrl;
1817
use Magento\UrlRewriteGraphQl\Model\Resolver\UrlRewrite\CustomUrlLocatorInterface;
1918

2019
/**
2120
* UrlRewrite field resolver, used for GraphQL request processing.
2221
*/
23-
class EntityUrl implements ResolverInterface
22+
class EntityUrl extends AbstractEntityUrl implements ResolverInterface
2423
{
25-
/**
26-
* @var UrlFinderInterface
27-
*/
28-
private $urlFinder;
29-
3024
/**
3125
* @var CustomUrlLocatorInterface
3226
*/
3327
private $customUrlLocator;
3428

35-
/**
36-
* @var int
37-
*/
38-
private $redirectType;
39-
40-
/**
41-
* @var Uid
42-
*/
43-
private $idEncoder;
44-
4529
/**
4630
* @param UrlFinderInterface $urlFinder
4731
* @param CustomUrlLocatorInterface $customUrlLocator
48-
* @param Uid $idEncoder
4932
*/
5033
public function __construct(
5134
UrlFinderInterface $urlFinder,
52-
CustomUrlLocatorInterface $customUrlLocator,
53-
Uid $idEncoder
35+
CustomUrlLocatorInterface $customUrlLocator
5436
) {
55-
$this->urlFinder = $urlFinder;
37+
parent::__construct($urlFinder);
5638
$this->customUrlLocator = $customUrlLocator;
57-
$this->idEncoder = $idEncoder;
5839
}
5940

6041
/**
@@ -73,9 +54,7 @@ public function resolve(
7354

7455
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
7556
$result = null;
76-
// phpcs:ignore Magento2.Functions.DiscouragedFunction
77-
$urlParts = parse_url($args['url']);
78-
$url = $urlParts['path'] ?? $args['url'];
57+
$url = $args['url'];
7958
if (substr($url, 0, 1) === '/' && $url !== '/') {
8059
$url = ltrim($url, '/');
8160
}
@@ -86,16 +65,12 @@ public function resolve(
8665
if ($finalUrlRewrite) {
8766
$relativeUrl = $finalUrlRewrite->getRequestPath();
8867
$resultArray = $this->rewriteCustomUrls($finalUrlRewrite, $storeId) ?? [
89-
'id' => $finalUrlRewrite->getEntityId(),
90-
'entity_uid' => $this->idEncoder->encode((string)$finalUrlRewrite->getEntityId()),
91-
'canonical_url' => $relativeUrl,
92-
'relative_url' => $relativeUrl,
93-
'redirectCode' => $this->redirectType,
94-
'type' => $this->sanitizeType($finalUrlRewrite->getEntityType())
95-
];
96-
if (!empty($urlParts['query'])) {
97-
$resultArray['relative_url'] .= '?' . $urlParts['query'];
98-
}
68+
'id' => $finalUrlRewrite->getEntityId(),
69+
'canonical_url' => $relativeUrl,
70+
'relative_url' => $relativeUrl,
71+
'redirectCode' => $this->redirectType,
72+
'type' => $this->sanitizeType($finalUrlRewrite->getEntityType())
73+
];
9974

10075
if (empty($resultArray['id'])) {
10176
throw new GraphQlNoSuchEntityException(
@@ -107,102 +82,4 @@ public function resolve(
10782
}
10883
return $result;
10984
}
110-
111-
/**
112-
* Handle custom urls with and without redirects
113-
*
114-
* @param UrlRewrite $finalUrlRewrite
115-
* @param int $storeId
116-
* @return array|null
117-
*/
118-
private function rewriteCustomUrls(UrlRewrite $finalUrlRewrite, int $storeId): ?array
119-
{
120-
if ($finalUrlRewrite->getEntityType() === 'custom' || !($finalUrlRewrite->getEntityId() > 0)) {
121-
$finalCustomUrlRewrite = clone $finalUrlRewrite;
122-
$finalUrlRewrite = $this->findFinalUrl($finalCustomUrlRewrite->getTargetPath(), $storeId, true);
123-
$relativeUrl =
124-
$finalCustomUrlRewrite->getRedirectType() == 0
125-
? $finalCustomUrlRewrite->getRequestPath() : $finalUrlRewrite->getRequestPath();
126-
return [
127-
'id' => $finalUrlRewrite->getEntityId(),
128-
'entity_uid' => $this->idEncoder->encode((string)$finalUrlRewrite->getEntityId()),
129-
'canonical_url' => $relativeUrl,
130-
'relative_url' => $relativeUrl,
131-
'redirectCode' => $finalCustomUrlRewrite->getRedirectType(),
132-
'type' => $this->sanitizeType($finalUrlRewrite->getEntityType())
133-
];
134-
}
135-
return null;
136-
}
137-
138-
/**
139-
* Find the final url passing through all redirects if any
140-
*
141-
* @param string $requestPath
142-
* @param int $storeId
143-
* @param bool $findCustom
144-
* @return UrlRewrite|null
145-
*/
146-
private function findFinalUrl(string $requestPath, int $storeId, bool $findCustom = false): ?UrlRewrite
147-
{
148-
$urlRewrite = $this->findUrlFromRequestPath($requestPath, $storeId);
149-
if ($urlRewrite) {
150-
$this->redirectType = $urlRewrite->getRedirectType();
151-
while ($urlRewrite && $urlRewrite->getRedirectType() > 0) {
152-
$urlRewrite = $this->findUrlFromRequestPath($urlRewrite->getTargetPath(), $storeId);
153-
}
154-
} else {
155-
$urlRewrite = $this->findUrlFromTargetPath($requestPath, $storeId);
156-
}
157-
if ($urlRewrite && ($findCustom && !$urlRewrite->getEntityId() && !$urlRewrite->getIsAutogenerated())) {
158-
$urlRewrite = $this->findUrlFromTargetPath($urlRewrite->getTargetPath(), $storeId);
159-
}
160-
161-
return $urlRewrite;
162-
}
163-
164-
/**
165-
* Find a url from a request url on the current store
166-
*
167-
* @param string $requestPath
168-
* @param int $storeId
169-
* @return UrlRewrite|null
170-
*/
171-
private function findUrlFromRequestPath(string $requestPath, int $storeId): ?UrlRewrite
172-
{
173-
return $this->urlFinder->findOneByData(
174-
[
175-
'request_path' => $requestPath,
176-
'store_id' => $storeId
177-
]
178-
);
179-
}
180-
181-
/**
182-
* Find a url from a target url on the current store
183-
*
184-
* @param string $targetPath
185-
* @param int $storeId
186-
* @return UrlRewrite|null
187-
*/
188-
private function findUrlFromTargetPath(string $targetPath, int $storeId): ?UrlRewrite
189-
{
190-
return $this->urlFinder->findOneByData(
191-
[
192-
'target_path' => $targetPath,
193-
'store_id' => $storeId
194-
]
195-
);
196-
}
197-
198-
/**
199-
* Sanitize the type to fit schema specifications
200-
*
201-
* @param string $type
202-
* @return string
203-
*/
204-
private function sanitizeType(string $type) : string
205-
{
206-
return strtoupper(str_replace('-', '_', $type));
207-
}
20885
}

0 commit comments

Comments
 (0)