Skip to content

Commit 47a365a

Browse files
Merge pull request #10 from magento-commerce/imported-magento-commerce-data-export-6
[Imported] 345: Support of url rewrites export
2 parents 738df37 + f0a448c commit 47a365a

File tree

6 files changed

+396
-0
lines changed

6 files changed

+396
-0
lines changed

Diff for: app/code/Magento/CatalogExportApi/Api/Data/Product.php

+24
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ class Product
188188
/** @var bool */
189189
private $lowStock;
190190

191+
/** @var \Magento\CatalogExportApi\Api\Data\UrlRewrite[]|null */
192+
private $urlRewrites;
193+
191194
/**
192195
* Get sku
193196
*
@@ -1342,4 +1345,25 @@ public function setLowStock(?bool $lowStock): void
13421345
{
13431346
$this->lowStock = $lowStock;
13441347
}
1348+
1349+
/**
1350+
* Get url rewrites
1351+
*
1352+
* @return \Magento\CatalogExportApi\Api\Data\UrlRewrite[]|null
1353+
*/
1354+
public function getUrlRewrites(): ?array
1355+
{
1356+
return $this->urlRewrites;
1357+
}
1358+
1359+
/**
1360+
* Set url rewrites
1361+
*
1362+
* @param \Magento\CatalogExportApi\Api\Data\UrlRewrite[] $urlRewrites
1363+
* @return void
1364+
*/
1365+
public function setUrlRewrites(?array $urlRewrites = null): void
1366+
{
1367+
$this->urlRewrites = $urlRewrites;
1368+
}
13451369
}
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*
7+
* Generated from et_schema.xml. DO NOT EDIT!
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace Magento\CatalogExportApi\Api\Data;
13+
14+
/**
15+
* UrlRewrite entity
16+
*
17+
* phpcs:disable Magento2.PHP.FinalImplementation
18+
* @SuppressWarnings(PHPMD.BooleanGetMethodName)
19+
* @SuppressWarnings(PHPMD.TooManyFields)
20+
* @SuppressWarnings(PHPMD.ExcessivePublicCount)
21+
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
22+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
23+
*/
24+
class UrlRewrite
25+
{
26+
/** @var string */
27+
private $url;
28+
29+
/** @var \Magento\CatalogExportApi\Api\Data\UrlRewriteParameter[]|null */
30+
private $parameters;
31+
32+
/**
33+
* Get url
34+
*
35+
* @return string
36+
*/
37+
public function getUrl(): ?string
38+
{
39+
return $this->url;
40+
}
41+
42+
/**
43+
* Set url
44+
*
45+
* @param string $url
46+
* @return void
47+
*/
48+
public function setUrl(?string $url): void
49+
{
50+
$this->url = $url;
51+
}
52+
53+
/**
54+
* Get parameters
55+
*
56+
* @return \Magento\CatalogExportApi\Api\Data\UrlRewriteParameter[]|null
57+
*/
58+
public function getParameters(): ?array
59+
{
60+
return $this->parameters;
61+
}
62+
63+
/**
64+
* Set parameters
65+
*
66+
* @param \Magento\CatalogExportApi\Api\Data\UrlRewriteParameter[] $parameters
67+
* @return void
68+
*/
69+
public function setParameters(?array $parameters = null): void
70+
{
71+
$this->parameters = $parameters;
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*
7+
* Generated from et_schema.xml. DO NOT EDIT!
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace Magento\CatalogExportApi\Api\Data;
13+
14+
/**
15+
* UrlRewriteParameter entity
16+
*
17+
* phpcs:disable Magento2.PHP.FinalImplementation
18+
* @SuppressWarnings(PHPMD.BooleanGetMethodName)
19+
* @SuppressWarnings(PHPMD.TooManyFields)
20+
* @SuppressWarnings(PHPMD.ExcessivePublicCount)
21+
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
22+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
23+
*/
24+
class UrlRewriteParameter
25+
{
26+
/** @var string */
27+
private $name;
28+
29+
/** @var string */
30+
private $value;
31+
32+
/**
33+
* Get name
34+
*
35+
* @return string
36+
*/
37+
public function getName(): ?string
38+
{
39+
return $this->name;
40+
}
41+
42+
/**
43+
* Set name
44+
*
45+
* @param string $name
46+
* @return void
47+
*/
48+
public function setName(?string $name): void
49+
{
50+
$this->name = $name;
51+
}
52+
53+
/**
54+
* Get value
55+
*
56+
* @return string
57+
*/
58+
public function getValue(): ?string
59+
{
60+
return $this->value;
61+
}
62+
63+
/**
64+
* Set value
65+
*
66+
* @param string $value
67+
* @return void
68+
*/
69+
public function setValue(?string $value): void
70+
{
71+
$this->value = $value;
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php
2+
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Magento\CatalogUrlRewriteDataExporter\Model\Provider\Product;
10+
11+
use Magento\CatalogUrlRewriteDataExporter\Model\Query\ProductUrlRewritesQuery;
12+
use Magento\DataExporter\Exception\UnableRetrieveData;
13+
use Magento\Framework\App\ResourceConnection;
14+
use Magento\Framework\UrlInterface;
15+
use Magento\Store\Model\StoreManagerInterface;
16+
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
17+
use Psr\Log\LoggerInterface;
18+
19+
/**
20+
* UrlRewrites data provider
21+
*/
22+
class UrlRewrites
23+
{
24+
/**
25+
* @var ResourceConnection
26+
*/
27+
private $resourceConnection;
28+
29+
/**
30+
* @var StoreManagerInterface
31+
*/
32+
private $storeManager;
33+
34+
/**
35+
* @var ProductUrlRewritesQuery
36+
*/
37+
private $urlRewritesQuery;
38+
39+
/**
40+
* @var LoggerInterface
41+
*/
42+
private $logger;
43+
44+
/**
45+
* @param ResourceConnection $resourceConnection
46+
* @param StoreManagerInterface $storeManager
47+
* @param ProductUrlRewritesQuery $urlRewritesQuery
48+
* @param LoggerInterface $logger
49+
*/
50+
public function __construct(
51+
ResourceConnection $resourceConnection,
52+
StoreManagerInterface $storeManager,
53+
ProductUrlRewritesQuery $urlRewritesQuery,
54+
LoggerInterface $logger
55+
) {
56+
$this->resourceConnection = $resourceConnection;
57+
$this->urlRewritesQuery = $urlRewritesQuery;
58+
$this->storeManager = $storeManager;
59+
$this->logger = $logger;
60+
}
61+
62+
/**
63+
* Format UrlRewrite data
64+
*
65+
* @throws \Magento\Framework\Exception\NoSuchEntityException
66+
* @param array $urlRewrite
67+
* @param string $storeViewCode
68+
* @return array
69+
*/
70+
private function format(array $urlRewrite, string $storeViewCode) : array
71+
{
72+
$baseUrl = $this->storeManager->getStore($storeViewCode)->getBaseUrl(UrlInterface::URL_TYPE_WEB);
73+
74+
return [
75+
'productId' => $urlRewrite[UrlRewrite::ENTITY_ID],
76+
'storeViewCode' => $storeViewCode,
77+
'url_rewrites' => [
78+
'url' => $baseUrl . $urlRewrite[UrlRewrite::REQUEST_PATH],
79+
'parameters' => $this->getUrlParameters($urlRewrite[UrlRewrite::TARGET_PATH])
80+
]
81+
];
82+
}
83+
84+
/**
85+
* Get provider data
86+
*
87+
* @param array $values
88+
* @return array
89+
* @throws UnableRetrieveData
90+
*/
91+
public function get(array $values): array
92+
{
93+
$output = [];
94+
$queryArguments = [];
95+
96+
try {
97+
foreach ($values as $value) {
98+
$queryArguments['productId'][$value['productId']] = $value['productId'];
99+
$queryArguments['storeViewCode'][$value['storeViewCode']] = $value['storeViewCode'];
100+
}
101+
foreach ($queryArguments['storeViewCode'] as $storeViewCode) {
102+
$urlRewrites = $this->getUrlRewrites($queryArguments, $storeViewCode);
103+
foreach ($urlRewrites ?? [] as $urlRewrite) {
104+
$output[] = $this->format($urlRewrite, $storeViewCode);
105+
}
106+
}
107+
} catch (\Exception $exception) {
108+
$this->logger->error($exception->getMessage());
109+
throw new UnableRetrieveData('Unable to retrieve url rewrites data');
110+
}
111+
112+
return $output;
113+
}
114+
115+
/**
116+
* Get url rewrites for products and given store.
117+
*
118+
* @param array $queryArguments
119+
* @param string $storeViewCode
120+
* @return array
121+
* @throws \Magento\Framework\Exception\NoSuchEntityException
122+
*/
123+
private function getUrlRewrites(array $queryArguments, string $storeViewCode): array
124+
{
125+
$storeId = (int) $this->storeManager->getStore($storeViewCode)->getId();
126+
$urlRewritesSelect = $this->urlRewritesQuery->getQuery($queryArguments['productId'], $storeId);
127+
$connection = $this->resourceConnection->getConnection();
128+
129+
return $connection->fetchAll($urlRewritesSelect);
130+
}
131+
132+
/**
133+
* Parses target path and extracts parameters
134+
*
135+
* @param string $targetPath
136+
* @return array
137+
*/
138+
private function getUrlParameters(string $targetPath): array
139+
{
140+
$urlParameters = [];
141+
$targetPathParts = explode('/', trim($targetPath, '/'));
142+
$targetPathPartsCount = count($targetPathParts);
143+
144+
for ($i = 3; $i < $targetPathPartsCount - 1; $i += 2) {
145+
$urlParameters[] = [
146+
'name' => $targetPathParts[$i],
147+
'value' => $targetPathParts[$i + 1]
148+
];
149+
}
150+
151+
return $urlParameters;
152+
}
153+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Magento\CatalogUrlRewriteDataExporter\Model\Query;
10+
11+
use Magento\Framework\DB\Select;
12+
use Magento\Framework\App\ResourceConnection;
13+
14+
/**
15+
* Fetch product url rewrites.
16+
*/
17+
class ProductUrlRewritesQuery
18+
{
19+
/**
20+
* @var ResourceConnection
21+
*/
22+
private $resourceConnection;
23+
24+
/**
25+
* @param ResourceConnection $resourceConnection
26+
*/
27+
public function __construct(
28+
ResourceConnection $resourceConnection
29+
) {
30+
$this->resourceConnection = $resourceConnection;
31+
}
32+
33+
/**
34+
* Return query that fetches a list of products' url rewrites.
35+
*
36+
* @param int[] $productIds
37+
* @param int $storeId
38+
* @return Select
39+
*/
40+
public function getQuery(array $productIds, int $storeId): Select
41+
{
42+
$resourceConnection = $this->resourceConnection;
43+
$connection = $resourceConnection->getConnection();
44+
$urlRewritesTable = $resourceConnection->getTableName('url_rewrite');
45+
46+
return $connection->select()
47+
->from(
48+
['e' => $urlRewritesTable],
49+
[
50+
\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::ENTITY_ID,
51+
\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::REQUEST_PATH,
52+
\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::TARGET_PATH,
53+
]
54+
)
55+
->where('entity_id IN (?)', $productIds)
56+
->where('entity_type = ?', 'product')
57+
->where('store_id = ?', $storeId);
58+
}
59+
}

0 commit comments

Comments
 (0)