|
| 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 | +} |
0 commit comments