33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6- declare (strict_types=1 );
7-
86namespace Magento \CatalogUrlRewrite \Observer ;
97
108use Magento \Catalog \Model \Product ;
119use Magento \CatalogUrlRewrite \Model \ProductUrlPathGenerator ;
1210use Magento \CatalogUrlRewrite \Model \ProductUrlRewriteGenerator ;
1311use Magento \Framework \App \ObjectManager ;
14- use Magento \UrlRewrite \Model \Exception \UrlAlreadyExistsException ;
1512use Magento \UrlRewrite \Model \UrlPersistInterface ;
1613use Magento \Framework \Event \ObserverInterface ;
17- use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory ;
1814
1915/**
2016 * Class ProductProcessUrlRewriteSavingObserver
@@ -36,29 +32,20 @@ class ProductProcessUrlRewriteSavingObserver implements ObserverInterface
3632 */
3733 private $ productUrlPathGenerator ;
3834
39- /**
40- * @var CollectionFactory
41- */
42- private $ collectionFactory ;
43-
4435 /**
4536 * @param ProductUrlRewriteGenerator $productUrlRewriteGenerator
4637 * @param UrlPersistInterface $urlPersist
47- * @param ProductUrlPathGenerator|null $urlPathGenerator
48- * @param CollectionFactory|null $collectionFactory
38+ * @param ProductUrlPathGenerator|null $productUrlPathGenerator
4939 */
5040 public function __construct (
5141 ProductUrlRewriteGenerator $ productUrlRewriteGenerator ,
5242 UrlPersistInterface $ urlPersist ,
53- ProductUrlPathGenerator $ urlPathGenerator = null ,
54- CollectionFactory $ collectionFactory = null
43+ ProductUrlPathGenerator $ productUrlPathGenerator = null
5544 ) {
5645 $ this ->productUrlRewriteGenerator = $ productUrlRewriteGenerator ;
5746 $ this ->urlPersist = $ urlPersist ;
58- $ this ->productUrlPathGenerator = $ urlPathGenerator ?: ObjectManager::getInstance ()
47+ $ this ->productUrlPathGenerator = $ productUrlPathGenerator ?: ObjectManager::getInstance ()
5948 ->get (ProductUrlPathGenerator::class);
60- $ this ->collectionFactory = $ collectionFactory ?: ObjectManager::getInstance ()
61- ->get (CollectionFactory::class);
6249 }
6350
6451 /**
@@ -82,27 +69,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
8269 $ product ->unsUrlPath ();
8370 $ product ->setUrlPath ($ this ->productUrlPathGenerator ->getUrlPath ($ product ));
8471 $ this ->urlPersist ->replace ($ this ->productUrlRewriteGenerator ->generate ($ product ));
85- return ;
8672 }
8773 }
88- $ this ->validateUrlKey ($ product );
89- }
90-
91- /**
92- * @param Product $product
93- * @throws UrlAlreadyExistsException
94- */
95- private function validateUrlKey (Product $ product )
96- {
97- $ productCollection = $ this ->collectionFactory ->create ();
98- $ productCollection ->addFieldToFilter (
99- 'url_key ' ,
100- ['in ' => $ product ->getUrlKey ()]
101- );
102- $ productCollection ->getSelect ()->where ('e.entity_id != ? ' , $ product ->getId ());
103-
104- if ($ productCollection ->getItems ()) {
105- throw new UrlAlreadyExistsException ();
106- }
10774 }
10875}
0 commit comments