Skip to content

Commit f3760ab

Browse files
committed
Added duplicate url key checker for categories, this uses the calculated url path's to determine this.
1 parent 467db5f commit f3760ab

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

Checker/Catalog/Category/UrlKey/DuplicateUrlKey.php

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,28 @@
44

55
namespace Baldwin\UrlDataIntegrityChecker\Checker\Catalog\Category\UrlKey;
66

7+
use Baldwin\UrlDataIntegrityChecker\Checker\Catalog\Category\UrlPath as UrlPathChecker;
8+
use Baldwin\UrlDataIntegrityChecker\Util\Stores as StoresUtil;
9+
710
class DuplicateUrlKey
811
{
12+
const DUPLICATED_PROBLEM_DESCRIPTION =
13+
'%s categories were found which have a duplicated url_key value: "%s" within the same parent.'
14+
. ' Please fix because this will cause problems.';
15+
16+
private $storesUtil;
17+
private $urlPathChecker;
18+
private $urlPathsInfo;
19+
20+
public function __construct(
21+
StoresUtil $storesUtil,
22+
UrlPathChecker $urlPathChecker
23+
) {
24+
$this->storesUtil = $storesUtil;
25+
$this->urlPathChecker = $urlPathChecker;
26+
$this->urlPathsInfo = [];
27+
}
28+
929
/**
1030
* @return array<array<string, mixed>>
1131
*/
@@ -23,8 +43,59 @@ private function checkForDuplicatedUrlKeyAttributeValues(): array
2343
{
2444
$categoriesWithProblems = [];
2545

26-
// TODO !!!!!
46+
$storeIds = $this->storesUtil->getAllStoreIds();
47+
foreach ($storeIds as $storeId) {
48+
$categoryUrlPaths = $this->getCategoryUrlPathsByStoreId($storeId);
49+
$urlPathsCount = array_count_values($categoryUrlPaths);
50+
51+
foreach ($urlPathsCount as $urlPath => $count) {
52+
if ($count === 1) {
53+
continue;
54+
}
55+
56+
$categories = $this->urlPathsInfo[$urlPath];
57+
58+
foreach ($categories as $category) {
59+
$categoriesWithProblems[] = [
60+
'catId' => (int) $category->getEntityId(),
61+
'name' => $category->getName(),
62+
'storeId' => $storeId,
63+
'problem' => sprintf(
64+
self::DUPLICATED_PROBLEM_DESCRIPTION,
65+
$count,
66+
$category->getUrlKey()
67+
),
68+
];
69+
}
70+
}
71+
}
2772

2873
return $categoriesWithProblems;
2974
}
75+
76+
/**
77+
* @return array<string>
78+
*/
79+
private function getCategoryUrlPathsByStoreId(int $storeId): array
80+
{
81+
$urlPaths = [];
82+
83+
$categories = $this->urlPathChecker->getAllVisibleCategoriesWithStoreId($storeId);
84+
foreach ($categories as $category) {
85+
$urlPath = $this->urlPathChecker->getCalculatedUrlPathForCategory($category, $storeId);
86+
87+
$rootCatId = 0;
88+
$path = $category->getPath() ?: '';
89+
if (preg_match('#^(\d+)/(\d+)/.+#', $path, $matches) === 1) {
90+
$rootCatId = $matches[2];
91+
}
92+
93+
$urlPath = $rootCatId . UrlPathChecker::URL_PATH_SEPARATOR . $urlPath;
94+
95+
$urlPaths[] = $urlPath;
96+
$this->urlPathsInfo[$urlPath][] = $category;
97+
}
98+
99+
return $urlPaths;
100+
}
30101
}

Checker/Catalog/Category/UrlPath.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function checkForIncorrectUrlPathAttributeValues(): array
9191
/**
9292
* @return CategoryCollection<Category>
9393
*/
94-
private function getAllVisibleCategoriesWithStoreId(int $storeId): CategoryCollection
94+
public function getAllVisibleCategoriesWithStoreId(int $storeId): CategoryCollection
9595
{
9696
$categories = $this->categoryCollectionFactory->create()
9797
->addAttributeToSelect('name')
@@ -111,7 +111,7 @@ private function doesCategoryUrlPathMatchCalculatedUrlPath(Category $category, i
111111
return $calculatedUrlPath === $currentUrlPath;
112112
}
113113

114-
private function getCalculatedUrlPathForCategory(Category $category, int $storeId): string
114+
public function getCalculatedUrlPathForCategory(Category $category, int $storeId): string
115115
{
116116
if ($this->calculatedUrlPathPerCategoryAndStoreId === null) {
117117
$this->fetchAllCategoriesWithUrlPathCalculatedByUrlKey();

0 commit comments

Comments
 (0)