@@ -92,27 +92,39 @@ private function resolveByLinkUrl(string $uri): string
92
92
93
93
private function resolveByWebUrl (string $ uri ): string
94
94
{
95
- $ matches = [];
95
+ $ storeMatch = null ;
96
+ $ highestScore = 0 ;
96
97
97
98
/** @var Store $store */
98
99
foreach ($ this ->storeRepository ->getList () as $ store ) {
99
100
if ($ store ->getId () && str_starts_with ($ uri , $ store ->getBaseUrl (UrlInterface::URL_TYPE_WEB ))) {
100
101
try {
101
- $ website = $ store ->getWebsite ();
102
- if ($ website ->getIsDefault ()) {
103
- if ($ store ->isDefault ()) {
104
- return $ store ->getCode ();
105
- }
106
- $ matches [0 ] = $ store ->getCode ();
107
- } elseif ($ store ->isDefault ()) {
108
- $ matches [1 ] = $ store ->getCode ();
109
- } else {
110
- $ matches [2 ] = $ store ->getCode ();
102
+ $ score = $ this ->calculatePreferenceScore ($ store );
103
+ $ storeMatch ??= $ store ->getCode ();
104
+ if ($ highestScore < $ score ) {
105
+ $ highestScore = $ score ;
106
+ $ storeMatch = $ store ->getCode ();
111
107
}
112
108
} catch (NoSuchEntityException ) {}
113
109
}
114
110
}
115
111
116
- return $ matches [0 ] ?? $ matches [1 ] ?? $ matches [2 ] ?? '' ;
112
+ return $ storeMatch ?? '' ;
113
+ }
114
+
115
+ /**
116
+ * @throws NoSuchEntityException
117
+ */
118
+ private function calculatePreferenceScore (Store $ store ): int
119
+ {
120
+ $ website = $ store ->getWebsite ();
121
+ // Bonus point for the stores which are part of one of the groups from the default website.
122
+ $ score = $ website ->getIsDefault () ? 2 : 0 ;
123
+ // Extra point for the stores which are part of the default group of its website.
124
+ $ score += (int )$ website ->getDefaultGroup ()->getDefaultStoreId () === (int )$ store ->getId () ? 1 : 0 ;
125
+ // Extra point is the store is the default one of its group.
126
+ $ score += $ store ->isDefault () ? 1 : 0 ;
127
+
128
+ return $ score ;
117
129
}
118
130
}
0 commit comments