33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+ declare (strict_types=1 );
7+
68namespace Magento \CloudComponents \Console \Command ;
79
10+ use Magento \CloudComponents \Model \UrlFinder \Product ;
11+ use Magento \CloudComponents \Model \UrlFinderFactory ;
812use Magento \Framework \App \Area ;
913use Magento \Framework \App \State ;
1014use Magento \Framework \Console \Cli ;
1115use Magento \Framework \Exception \LocalizedException ;
12- use Magento \Framework \UrlFactory ;
16+ use Magento \Framework \Exception \ NoSuchEntityException ;
1317use Magento \Store \Api \Data \StoreInterface ;
1418use Magento \Store \Model \StoreManagerInterface ;
1519use Magento \UrlRewrite \Controller \Adminhtml \Url \Rewrite ;
16- use Magento \UrlRewrite \Model \UrlFinderInterface ;
1720use Symfony \Component \Console \Command \Command ;
1821use Symfony \Component \Console \Input \InputInterface ;
1922use Symfony \Component \Console \Input \InputOption ;
2023use Symfony \Component \Console \Output \OutputInterface ;
21- use Magento \UrlRewrite \Service \V1 \Data \UrlRewrite ;
2224
2325/**
2426 * Returns list of category or cms-page urls for given stores
27+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2528 */
2629class ConfigShowEntityUrlsCommand extends Command
2730{
@@ -30,47 +33,45 @@ class ConfigShowEntityUrlsCommand extends Command
3033 */
3134 const INPUT_OPTION_STORE_ID = 'store-id ' ;
3235 const INPUT_OPTION_ENTITY_TYPE = 'entity-type ' ;
36+ const INPUT_OPTION_PRODUCT_SKU = 'product-sku ' ;
37+ const INPUT_OPTION_PRODUCT_LIMIT = 'product-limit ' ;
3338
3439 /**
3540 * @var StoreManagerInterface
3641 */
3742 private $ storeManager ;
3843
3944 /**
40- * @var UrlFinderInterface
41- */
42- private $ urlFinder ;
43-
44- /**
45- * @var UrlFactory
45+ * @var State
4646 */
47- private $ urlFactory ;
47+ private $ state ;
4848
4949 /**
50- * @var State
50+ * @var UrlFinderFactory
5151 */
52- private $ state ;
52+ private $ urlFinderFactory ;
5353
5454 /**
5555 * @var array
5656 */
57- private $ possibleEntities = [Rewrite::ENTITY_TYPE_CMS_PAGE , Rewrite::ENTITY_TYPE_CATEGORY ];
57+ private $ possibleEntities = [
58+ Rewrite::ENTITY_TYPE_CMS_PAGE ,
59+ Rewrite::ENTITY_TYPE_CATEGORY ,
60+ Rewrite::ENTITY_TYPE_PRODUCT
61+ ];
5862
5963 /**
6064 * @param StoreManagerInterface $storeManager
61- * @param UrlFinderInterface $urlFinder
62- * @param UrlFactory $urlFactory
65+ * @param UrlFinderFactory $urlFinderFactory
6366 * @param State $state
6467 */
6568 public function __construct (
6669 StoreManagerInterface $ storeManager ,
67- UrlFinderInterface $ urlFinder ,
68- UrlFactory $ urlFactory ,
70+ UrlFinderFactory $ urlFinderFactory ,
6971 State $ state
7072 ) {
7173 $ this ->storeManager = $ storeManager ;
72- $ this ->urlFinder = $ urlFinder ;
73- $ this ->urlFactory = $ urlFactory ;
74+ $ this ->urlFinderFactory = $ urlFinderFactory ;
7475 $ this ->state = $ state ;
7576
7677 parent ::__construct ();
@@ -89,7 +90,7 @@ protected function configure()
8990 $ this ->addOption (
9091 self ::INPUT_OPTION_STORE_ID ,
9192 null ,
92- InputOption::VALUE_OPTIONAL ,
93+ InputOption::VALUE_IS_ARRAY | InputOption:: VALUE_OPTIONAL ,
9394 'Store ID '
9495 );
9596 $ this ->addOption (
@@ -98,6 +99,19 @@ protected function configure()
9899 InputOption::VALUE_REQUIRED ,
99100 'Entity type: ' . implode (', ' , $ this ->possibleEntities )
100101 );
102+ $ this ->addOption (
103+ self ::INPUT_OPTION_PRODUCT_SKU ,
104+ null ,
105+ InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL ,
106+ 'Product SKUs '
107+ );
108+ $ this ->addOption (
109+ self ::INPUT_OPTION_PRODUCT_LIMIT ,
110+ null ,
111+ InputOption::VALUE_OPTIONAL ,
112+ 'Product limit per store uses in case when product SKUs isn \'t provided ' ,
113+ Product::PRODUCT_LIMIT
114+ );
101115
102116 parent ::configure ();
103117 }
@@ -121,17 +135,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
121135 return Cli::RETURN_FAILURE ;
122136 }
123137
124- $ storeId = $ input ->getOption (self ::INPUT_OPTION_STORE_ID );
125-
126- if ($ storeId === null ) {
127- $ stores = $ this ->storeManager ->getStores ();
128- } else {
129- $ stores = [$ this ->storeManager ->getStore ($ storeId )];
130- }
131-
132- $ urls = $ this ->getPageUrls ($ stores , $ entityType );
138+ $ urlFinder = $ this ->urlFinderFactory ->create ($ entityType , [
139+ 'stores ' => $ this ->getStores ($ input ),
140+ 'productSku ' => $ input ->getOption (self ::INPUT_OPTION_PRODUCT_SKU ),
141+ 'productLimit ' => $ input ->getOption (self ::INPUT_OPTION_PRODUCT_LIMIT ),
142+ ]);
133143
134- $ output ->write (json_encode (array_unique ($ urls )));
144+ $ output ->writeln (json_encode (array_unique ($ urlFinder -> get () )));
135145 return Cli::RETURN_SUCCESS ;
136146 } catch (\Exception $ e ) {
137147 $ output ->writeln ($ e ->getMessage ());
@@ -140,28 +150,24 @@ protected function execute(InputInterface $input, OutputInterface $output)
140150 }
141151
142152 /**
143- * @param StoreInterface[] $stores
144- * @param string $entityType
145- * @return array
153+ * @param InputInterface $input
154+ * @return StoreInterface[]
155+ * @throws NoSuchEntityException
146156 */
147- private function getPageUrls ( array $ stores , string $ entityType ): array
157+ private function getStores ( InputInterface $ input ): array
148158 {
149- $ urls = [];
150-
151- foreach ($ stores as $ store ) {
152- $ url = $ this ->urlFactory ->create ()->setScope ($ store ->getId ());
153-
154- $ entities = $ this ->urlFinder ->findAllByData ([
155- UrlRewrite::STORE_ID => $ store ->getId (),
156- UrlRewrite::ENTITY_TYPE => $ entityType
157- ]);
159+ $ storeIds = $ input ->getOption (self ::INPUT_OPTION_STORE_ID );
158160
159- foreach ($ entities as $ urlRewrite ) {
160- $ urls [] = $ url ->getUrl ($ urlRewrite ->getRequestPath ());
161+ if (!empty ($ storeIds )) {
162+ $ stores = [];
163+ foreach ($ storeIds as $ storeId ) {
164+ $ stores [] = $ this ->storeManager ->getStore ($ storeId );
161165 }
166+ } else {
167+ $ stores = $ this ->storeManager ->getStores ();
162168 }
163169
164- return $ urls ;
170+ return $ stores ;
165171 }
166172
167173 /**
0 commit comments