9
9
10
10
use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
11
11
use Magento \Framework \GraphQl \Exception \GraphQlNoSuchEntityException ;
12
- use Magento \Framework \GraphQl \Query \Uid ;
13
12
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
14
13
use Magento \Framework \GraphQl \Config \Element \Field ;
15
14
use Magento \Framework \GraphQl \Query \ResolverInterface ;
16
15
use Magento \UrlRewrite \Model \UrlFinderInterface ;
17
- use Magento \UrlRewrite \ Service \ V1 \ Data \ UrlRewrite ;
16
+ use Magento \UrlRewriteGraphQl \ Model \ Resolver \ AbstractEntityUrl ;
18
17
use Magento \UrlRewriteGraphQl \Model \Resolver \UrlRewrite \CustomUrlLocatorInterface ;
19
18
20
19
/**
21
20
* UrlRewrite field resolver, used for GraphQL request processing.
22
21
*/
23
- class EntityUrl implements ResolverInterface
22
+ class EntityUrl extends AbstractEntityUrl implements ResolverInterface
24
23
{
25
- /**
26
- * @var UrlFinderInterface
27
- */
28
- private $ urlFinder ;
29
-
30
24
/**
31
25
* @var CustomUrlLocatorInterface
32
26
*/
33
27
private $ customUrlLocator ;
34
28
35
- /**
36
- * @var int
37
- */
38
- private $ redirectType ;
39
-
40
- /**
41
- * @var Uid
42
- */
43
- private $ idEncoder ;
44
-
45
29
/**
46
30
* @param UrlFinderInterface $urlFinder
47
31
* @param CustomUrlLocatorInterface $customUrlLocator
48
- * @param Uid $idEncoder
49
32
*/
50
33
public function __construct (
51
34
UrlFinderInterface $ urlFinder ,
52
- CustomUrlLocatorInterface $ customUrlLocator ,
53
- Uid $ idEncoder
35
+ CustomUrlLocatorInterface $ customUrlLocator
54
36
) {
55
- $ this -> urlFinder = $ urlFinder ;
37
+ parent :: __construct ( $ urlFinder) ;
56
38
$ this ->customUrlLocator = $ customUrlLocator ;
57
- $ this ->idEncoder = $ idEncoder ;
58
39
}
59
40
60
41
/**
@@ -73,9 +54,7 @@ public function resolve(
73
54
74
55
$ storeId = (int )$ context ->getExtensionAttributes ()->getStore ()->getId ();
75
56
$ result = null ;
76
- // phpcs:ignore Magento2.Functions.DiscouragedFunction
77
- $ urlParts = parse_url ($ args ['url ' ]);
78
- $ url = $ urlParts ['path ' ] ?? $ args ['url ' ];
57
+ $ url = $ args ['url ' ];
79
58
if (substr ($ url , 0 , 1 ) === '/ ' && $ url !== '/ ' ) {
80
59
$ url = ltrim ($ url , '/ ' );
81
60
}
@@ -86,16 +65,12 @@ public function resolve(
86
65
if ($ finalUrlRewrite ) {
87
66
$ relativeUrl = $ finalUrlRewrite ->getRequestPath ();
88
67
$ 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
+ ];
99
74
100
75
if (empty ($ resultArray ['id ' ])) {
101
76
throw new GraphQlNoSuchEntityException (
@@ -107,102 +82,4 @@ public function resolve(
107
82
}
108
83
return $ result ;
109
84
}
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
- }
208
85
}
0 commit comments