Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UrlRewriteFinder.php
Go to the documentation of this file.
1 <?php
7 
11 use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory;
12 
21 {
22  const ENTITY_TYPE_CATEGORY = 'category';
23  const ENTITY_TYPE_PRODUCT = 'product';
24 
28  private $databaseMapPool;
29 
33  private $urlFinder;
34 
38  private $urlRewritePrototype;
39 
43  private $urlRewriteClassNames = [];
44 
51  public function __construct(
52  DatabaseMapPool $databaseMapPool,
53  UrlFinderInterface $urlFinder,
54  UrlRewriteFactory $urlRewriteFactory,
55  array $urlRewriteClassNames = []
56  ) {
57  $this->databaseMapPool = $databaseMapPool;
58  $this->urlFinder = $urlFinder;
59  $this->urlRewriteClassNames = $urlRewriteClassNames;
60  $this->urlRewritePrototype = $urlRewriteFactory->create();
61  }
62 
73  public function findAllByData($entityId, $storeId, $entityType, $rootCategoryId = null)
74  {
75  if ($rootCategoryId
76  && is_numeric($entityId)
77  && is_numeric($storeId)
78  && is_string($entityType)
79  && isset($this->urlRewriteClassNames[$entityType])
80  ) {
81  $map = $this->databaseMapPool->getDataMap($this->urlRewriteClassNames[$entityType], $rootCategoryId);
82  if ($map) {
83  $key = $storeId . '_' . $entityId;
84  return $this->arrayToUrlRewriteObject($map->getData($rootCategoryId, $key));
85  }
86  }
87 
88  return $this->urlFinder->findAllByData(
89  [
91  UrlRewrite::ENTITY_ID => $entityId,
93  ]
94  );
95  }
96 
103  private function arrayToUrlRewriteObject(array $data)
104  {
105  foreach ($data as $key => $array) {
106  $data[$key] = $this->createUrlRewrite($array);
107  }
108  return $data;
109  }
110 
117  private function createUrlRewrite(array $data)
118  {
119  $dataObject = clone $this->urlRewritePrototype;
120  $dataObject->setUrlRewriteId($data['url_rewrite_id']);
121  $dataObject->setEntityType($data['entity_type']);
122  $dataObject->setEntityId($data['entity_id']);
123  $dataObject->setRequestPath($data['request_path']);
124  $dataObject->setTargetPath($data['target_path']);
125  $dataObject->setRedirectType($data['redirect_type']);
126  $dataObject->setStoreId($data['store_id']);
127  $dataObject->setDescription($data['description']);
128  $dataObject->setIsAutogenerated($data['is_autogenerated']);
129  $dataObject->setMetadata($data['metadata']);
130 
131  return $dataObject;
132  }
133 }
findAllByData($entityId, $storeId, $entityType, $rootCategoryId=null)
__construct(DatabaseMapPool $databaseMapPool, UrlFinderInterface $urlFinder, UrlRewriteFactory $urlRewriteFactory, array $urlRewriteClassNames=[])