66
77use Baldwin \UrlDataIntegrityChecker \Checker \Catalog \Product \UrlKey as UrlKeyChecker ;
88use Baldwin \UrlDataIntegrityChecker \Console \Progress ;
9+ use Baldwin \UrlDataIntegrityChecker \Util \Configuration as ConfigUtil ;
910use Baldwin \UrlDataIntegrityChecker \Util \Stores as StoresUtil ;
1011use Magento \Catalog \Api \Data \ProductInterface ;
1112use Magento \Catalog \Model \Attribute \ScopeOverriddenValueFactory as AttributeScopeOverriddenValueFactory ;
1213use Magento \Catalog \Model \Product as ProductModel ;
14+ use Magento \Catalog \Model \Product \Visibility as ProductVisibility ;
1315use Magento \Catalog \Model \ResourceModel \Product \Collection as ProductCollection ;
1416use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory as ProductCollectionFactory ;
1517use Magento \Store \Model \Store ;
@@ -25,19 +27,23 @@ class DuplicateUrlKey
2527 private $ progressIndex ;
2628 private $ productCollectionFactory ;
2729 private $ attributeScopeOverriddenValueFactory ;
30+ /** @var array<string, string> */
2831 private $ cachedProductUrlKeyData ;
2932 private $ cachedProductSkusByIds ;
33+ private $ configUtil ;
3034
3135 public function __construct (
3236 StoresUtil $ storesUtil ,
3337 Progress $ progress ,
3438 ProductCollectionFactory $ productCollectionFactory ,
35- AttributeScopeOverriddenValueFactory $ attributeScopeOverriddenValueFactory
39+ AttributeScopeOverriddenValueFactory $ attributeScopeOverriddenValueFactory ,
40+ ConfigUtil $ configUtil
3641 ) {
3742 $ this ->storesUtil = $ storesUtil ;
3843 $ this ->progress = $ progress ;
3944 $ this ->productCollectionFactory = $ productCollectionFactory ;
4045 $ this ->attributeScopeOverriddenValueFactory = $ attributeScopeOverriddenValueFactory ;
46+ $ this ->configUtil = $ configUtil ;
4147
4248 $ this ->progressIndex = 0 ;
4349 $ this ->cachedProductUrlKeyData = [];
@@ -81,12 +87,15 @@ private function checkForDuplicatedUrlKeyAttributeValues(): array
8187 ->addAttributeToSelect (UrlKeyChecker::URL_KEY_ATTRIBUTE )
8288 ->addAttributeToFilter (UrlKeyChecker::URL_KEY_ATTRIBUTE , ['notnull ' => true ], $ joinType )
8389 ->addAttributeToFilter (UrlKeyChecker::URL_KEY_ATTRIBUTE , ['neq ' => '' ], $ joinType )
84- // TODO: remove!
85- // ->addAttributeToFilter('entity_id', [
86- // 'in' => [147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158],
87- // ])
8890 ;
8991
92+ if ($ this ->configUtil ->getOnlyCheckVisibleProducts ()) {
93+ $ collection ->addAttributeToFilter (
94+ ProductInterface::VISIBILITY ,
95+ ['neq ' => ProductVisibility::VISIBILITY_NOT_VISIBLE ]
96+ );
97+ }
98+
9099 if ($ this ->progressIndex === 0 ) {
91100 $ this ->progress ->setGuestimatedSize (count ($ storeIds ), $ collection ->getSize ());
92101 }
@@ -112,6 +121,8 @@ private function checkForDuplicatedUrlKeyAttributeValues(): array
112121 private function storeProductUrlKeyData (int $ storeId , ProductCollection $ collection )
113122 {
114123 foreach ($ collection as $ product ) {
124+ assert (is_numeric ($ product ->getEntityId ()));
125+
115126 $ productId = $ product ->getEntityId ();
116127 $ productSku = $ product ->getSku ();
117128 $ productUrlKey = $ product ->getUrlKey ();
0 commit comments