4
4
5
5
namespace Baldwin \UrlDataIntegrityChecker \Checker \Catalog \Category \UrlKey ;
6
6
7
+ use Baldwin \UrlDataIntegrityChecker \Checker \Catalog \Category \UrlPath as UrlPathChecker ;
8
+ use Baldwin \UrlDataIntegrityChecker \Util \Stores as StoresUtil ;
9
+
7
10
class DuplicateUrlKey
8
11
{
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
+
9
29
/**
10
30
* @return array<array<string, mixed>>
11
31
*/
@@ -23,8 +43,59 @@ private function checkForDuplicatedUrlKeyAttributeValues(): array
23
43
{
24
44
$ categoriesWithProblems = [];
25
45
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
+ }
27
72
28
73
return $ categoriesWithProblems ;
29
74
}
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
+ }
30
101
}
0 commit comments