@@ -31,6 +31,11 @@ class EntityUrl implements ResolverInterface
31
31
*/
32
32
private $ customUrlLocator ;
33
33
34
+ /**
35
+ * @var int
36
+ */
37
+ private $ redirectType ;
38
+
34
39
/**
35
40
* @param UrlFinderInterface $urlFinder
36
41
* @param CustomUrlLocatorInterface $customUrlLocator
@@ -57,49 +62,83 @@ public function resolve(
57
62
throw new GraphQlInputException (__ ('"url" argument should be specified and not empty ' ));
58
63
}
59
64
65
+ $ storeId = (int )$ context ->getExtensionAttributes ()->getStore ()->getId ();
60
66
$ result = null ;
61
67
$ url = $ args ['url ' ];
62
68
if (substr ($ url , 0 , 1 ) === '/ ' && $ url !== '/ ' ) {
63
69
$ url = ltrim ($ url , '/ ' );
64
70
}
71
+ $ this ->redirectType = 0 ;
65
72
$ customUrl = $ this ->customUrlLocator ->locateUrl ($ url );
66
73
$ url = $ customUrl ?: $ url ;
67
- $ urlRewrite = $ this ->findCanonicalUrl ($ url , (int )$ context ->getExtensionAttributes ()->getStore ()->getId ());
68
- if ($ urlRewrite ) {
69
- if (!$ urlRewrite ->getEntityId ()) {
74
+ $ finalUrlRewrite = $ this ->findFinalUrl ($ url , $ storeId );
75
+ if ($ finalUrlRewrite ) {
76
+ $ relativeUrl = $ finalUrlRewrite ->getRequestPath ();
77
+ $ resultArray = $ this ->rewriteCustomUrls ($ finalUrlRewrite , $ storeId ) ?? [
78
+ 'id ' => $ finalUrlRewrite ->getEntityId (),
79
+ 'canonical_url ' => $ relativeUrl ,
80
+ 'relative_url ' => $ relativeUrl ,
81
+ 'redirectCode ' => $ this ->redirectType ,
82
+ 'type ' => $ this ->sanitizeType ($ finalUrlRewrite ->getEntityType ())
83
+ ];
84
+
85
+ if (empty ($ resultArray ['id ' ])) {
70
86
throw new GraphQlNoSuchEntityException (
71
87
__ ('No such entity found with matching URL key: %url ' , ['url ' => $ url ])
72
88
);
73
89
}
74
- $ result = [
75
- 'id ' => $ urlRewrite ->getEntityId (),
76
- 'canonical_url ' => $ urlRewrite ->getTargetPath (),
77
- 'relative_url ' => $ urlRewrite ->getTargetPath (),
78
- 'type ' => $ this ->sanitizeType ($ urlRewrite ->getEntityType ())
79
- ];
90
+
91
+ $ result = $ resultArray ;
80
92
}
81
93
return $ result ;
82
94
}
83
95
84
96
/**
85
- * Find the canonical url passing through all redirects if any
97
+ * Handle custom urls with and without redirects
98
+ *
99
+ * @param UrlRewrite $finalUrlRewrite
100
+ * @param int $storeId
101
+ * @return array|null
102
+ */
103
+ private function rewriteCustomUrls (UrlRewrite $ finalUrlRewrite , int $ storeId ): ?array
104
+ {
105
+ if ($ finalUrlRewrite ->getEntityType () === 'custom ' || !($ finalUrlRewrite ->getEntityId () > 0 )) {
106
+ $ finalCustomUrlRewrite = clone $ finalUrlRewrite ;
107
+ $ finalUrlRewrite = $ this ->findFinalUrl ($ finalCustomUrlRewrite ->getTargetPath (), $ storeId , true );
108
+ $ relativeUrl =
109
+ $ finalCustomUrlRewrite ->getRedirectType () == 0
110
+ ? $ finalCustomUrlRewrite ->getRequestPath () : $ finalUrlRewrite ->getRequestPath ();
111
+ return [
112
+ 'id ' => $ finalUrlRewrite ->getEntityId (),
113
+ 'canonical_url ' => $ relativeUrl ,
114
+ 'relative_url ' => $ relativeUrl ,
115
+ 'redirectCode ' => $ finalCustomUrlRewrite ->getRedirectType (),
116
+ 'type ' => $ this ->sanitizeType ($ finalUrlRewrite ->getEntityType ())
117
+ ];
118
+ }
119
+ return null ;
120
+ }
121
+
122
+ /**
123
+ * Find the final url passing through all redirects if any
86
124
*
87
125
* @param string $requestPath
88
126
* @param int $storeId
127
+ * @param bool $findCustom
89
128
* @return UrlRewrite|null
90
129
*/
91
- private function findCanonicalUrl (string $ requestPath , int $ storeId) : ?UrlRewrite
130
+ private function findFinalUrl (string $ requestPath , int $ storeId, bool $ findCustom = false ) : ?UrlRewrite
92
131
{
93
132
$ urlRewrite = $ this ->findUrlFromRequestPath ($ requestPath , $ storeId );
94
- if ($ urlRewrite && $ urlRewrite ->getRedirectType () > 0 ) {
133
+ if ($ urlRewrite ) {
134
+ $ this ->redirectType = $ urlRewrite ->getRedirectType ();
95
135
while ($ urlRewrite && $ urlRewrite ->getRedirectType () > 0 ) {
96
136
$ urlRewrite = $ this ->findUrlFromRequestPath ($ urlRewrite ->getTargetPath (), $ storeId );
97
137
}
98
- }
99
- if (!$ urlRewrite ) {
138
+ } else {
100
139
$ urlRewrite = $ this ->findUrlFromTargetPath ($ requestPath , $ storeId );
101
140
}
102
- if ($ urlRewrite && !$ urlRewrite ->getEntityId () && !$ urlRewrite ->getIsAutogenerated ()) {
141
+ if ($ urlRewrite && ( $ findCustom && !$ urlRewrite ->getEntityId () && !$ urlRewrite ->getIsAutogenerated () )) {
103
142
$ urlRewrite = $ this ->findUrlFromTargetPath ($ urlRewrite ->getTargetPath (), $ storeId );
104
143
}
105
144
@@ -113,7 +152,7 @@ private function findCanonicalUrl(string $requestPath, int $storeId) : ?UrlRewri
113
152
* @param int $storeId
114
153
* @return UrlRewrite|null
115
154
*/
116
- private function findUrlFromRequestPath (string $ requestPath , int $ storeId ) : ?UrlRewrite
155
+ private function findUrlFromRequestPath (string $ requestPath , int $ storeId ): ?UrlRewrite
117
156
{
118
157
return $ this ->urlFinder ->findOneByData (
119
158
[
@@ -130,7 +169,7 @@ private function findUrlFromRequestPath(string $requestPath, int $storeId) : ?Ur
130
169
* @param int $storeId
131
170
* @return UrlRewrite|null
132
171
*/
133
- private function findUrlFromTargetPath (string $ targetPath , int $ storeId ) : ?UrlRewrite
172
+ private function findUrlFromTargetPath (string $ targetPath , int $ storeId ): ?UrlRewrite
134
173
{
135
174
return $ this ->urlFinder ->findOneByData (
136
175
[
0 commit comments