@@ -92,27 +92,39 @@ private function resolveByLinkUrl(string $uri): string
9292
9393 private function resolveByWebUrl(string $uri): string
9494 {
95- $matches = [];
95+ $storeMatch = null;
96+ $highestScore = 0;
9697
9798 /** @var Store $store */
9899 foreach ($this->storeRepository->getList() as $store) {
99100 if ($store->getId() && str_starts_with($uri, $store->getBaseUrl(UrlInterface::URL_TYPE_WEB))) {
100101 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();
111107 }
112108 } catch (NoSuchEntityException) {}
113109 }
114110 }
115111
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;
117129 }
118130}
0 commit comments