11<?php
22/**
3- * Catalog product copier. Creates product duplicate
4- *
53 * Copyright © Magento, Inc. All rights reserved.
64 * See COPYING.txt for license details.
75 */
86namespace Magento \Catalog \Model \Product ;
97
108use Magento \Catalog \Api \Data \ProductInterface ;
9+ use Magento \Catalog \Model \Product ;
10+ use Magento \Framework \App \ObjectManager ;
11+ use Magento \Framework \EntityManager \MetadataPool ;
12+ use Magento \Catalog \Model \ProductFactory ;
13+ use Magento \Catalog \Model \Product \Option \Repository as OptionRepository ;
1114
15+ /**
16+ * Catalog product copier. Creates product duplicate
17+ */
1218class Copier
1319{
1420 /**
15- * @var Option\Repository
21+ * @var OptionRepository
1622 */
1723 protected $ optionRepository ;
1824
@@ -22,22 +28,22 @@ class Copier
2228 protected $ copyConstructor ;
2329
2430 /**
25- * @var \Magento\Catalog\Model\ ProductFactory
31+ * @var ProductFactory
2632 */
2733 protected $ productFactory ;
2834
2935 /**
30- * @var \Magento\Framework\EntityManager\ MetadataPool
36+ * @var MetadataPool
3137 */
3238 protected $ metadataPool ;
3339
3440 /**
3541 * @param CopyConstructorInterface $copyConstructor
36- * @param \Magento\Catalog\Model\ ProductFactory $productFactory
42+ * @param ProductFactory $productFactory
3743 */
3844 public function __construct (
3945 CopyConstructorInterface $ copyConstructor ,
40- \ Magento \ Catalog \ Model \ ProductFactory $ productFactory
46+ ProductFactory $ productFactory
4147 ) {
4248 $ this ->productFactory = $ productFactory ;
4349 $ this ->copyConstructor = $ copyConstructor ;
@@ -46,18 +52,16 @@ public function __construct(
4652 /**
4753 * Create product duplicate
4854 *
49- * @param \Magento\Catalog\Model\ Product $product
50- * @return \Magento\Catalog\Model\ Product
55+ * @param Product $product
56+ * @return Product
5157 */
52- public function copy (\ Magento \ Catalog \ Model \ Product $ product )
58+ public function copy (Product $ product )
5359 {
5460 $ product ->getWebsiteIds ();
5561 $ product ->getCategoryIds ();
5662
57- /** @var \Magento\Framework\EntityManager\EntityMetadataInterface $metadata */
5863 $ metadata = $ this ->getMetadataPool ()->getMetadata (ProductInterface::class);
5964
60- /** @var \Magento\Catalog\Model\Product $duplicate */
6165 $ duplicate = $ this ->productFactory ->create ();
6266 $ productData = $ product ->getData ();
6367 $ productData = $ this ->removeStockItem ($ productData );
@@ -83,6 +87,7 @@ public function copy(\Magento\Catalog\Model\Product $product)
8387 $ duplicate ->save ();
8488 $ isDuplicateSaved = true ;
8589 } catch (\Magento \Framework \Exception \AlreadyExistsException $ e ) {
90+ } catch (\Magento \UrlRewrite \Model \Exception \UrlAlreadyExistsException $ e ) {
8691 }
8792 } while (!$ isDuplicateSaved );
8893 $ this ->getOptionRepository ()->duplicate ($ product , $ duplicate );
@@ -94,27 +99,25 @@ public function copy(\Magento\Catalog\Model\Product $product)
9499 }
95100
96101 /**
97- * @return Option\Repository
102+ * @return OptionRepository
98103 * @deprecated 101.0.0
99104 */
100105 private function getOptionRepository ()
101106 {
102107 if (null === $ this ->optionRepository ) {
103- $ this ->optionRepository = \Magento \Framework \App \ObjectManager::getInstance ()
104- ->get (\Magento \Catalog \Model \Product \Option \Repository::class);
108+ $ this ->optionRepository = ObjectManager::getInstance ()->get (OptionRepository::class);
105109 }
106110 return $ this ->optionRepository ;
107111 }
108112
109113 /**
110- * @return \Magento\Framework\EntityManager\ MetadataPool
114+ * @return MetadataPool
111115 * @deprecated 101.0.0
112116 */
113117 private function getMetadataPool ()
114118 {
115119 if (null === $ this ->metadataPool ) {
116- $ this ->metadataPool = \Magento \Framework \App \ObjectManager::getInstance ()
117- ->get (\Magento \Framework \EntityManager \MetadataPool::class);
120+ $ this ->metadataPool = ObjectManager::getInstance ()->get (MetadataPool::class);
118121 }
119122 return $ this ->metadataPool ;
120123 }
0 commit comments