|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\UpwardConnector\Model\Computed; |
| 10 | + |
| 11 | +use Magento\Framework\Serialize\Serializer\Json; |
| 12 | +use Magento\Store\Model\StoreManagerInterface; |
| 13 | +use Magento\Upward\Definition; |
| 14 | +use Magento\Upward\DefinitionIterator; |
| 15 | +use Magento\UpwardConnector\Api\ComputedInterface; |
| 16 | +use Magento\UpwardConnector\Model\PageType; |
| 17 | +use Magento\UrlRewriteGraphQl\Model\DataProvider\EntityDataProviderComposite; |
| 18 | + |
| 19 | +class PageInfo implements ComputedInterface |
| 20 | +{ |
| 21 | + /** @var \Magento\UpwardConnector\Model\PageType */ |
| 22 | + private $pageTypeResolver; |
| 23 | + |
| 24 | + /** @var \Magento\Store\Model\StoreManagerInterface */ |
| 25 | + private $storeManager; |
| 26 | + |
| 27 | + /** @var \Magento\Framework\Serialize\Serializer\Json */ |
| 28 | + private $json; |
| 29 | + |
| 30 | + /** @var \Magento\UrlRewriteGraphQl\Model\DataProvider\EntityDataProviderComposite */ |
| 31 | + private $entityDataProviderComposite; |
| 32 | + |
| 33 | + /** |
| 34 | + * @param \Magento\UpwardConnector\Model\PageType $pageTypeResolver |
| 35 | + * @param \Magento\Store\Model\StoreManagerInterface $storeManager |
| 36 | + * @param \Magento\Framework\Serialize\Serializer\Json $json |
| 37 | + * @param \Magento\UrlRewriteGraphQl\Model\DataProvider\EntityDataProviderComposite $entityDataProviderComposite |
| 38 | + */ |
| 39 | + public function __construct( |
| 40 | + PageType $pageTypeResolver, |
| 41 | + StoreManagerInterface $storeManager, |
| 42 | + Json $json, |
| 43 | + EntityDataProviderComposite $entityDataProviderComposite |
| 44 | + ) { |
| 45 | + $this->pageTypeResolver = $pageTypeResolver; |
| 46 | + $this->storeManager = $storeManager; |
| 47 | + $this->json = $json; |
| 48 | + $this->entityDataProviderComposite = $entityDataProviderComposite; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @param \Magento\Upward\DefinitionIterator $iterator |
| 53 | + * @param \Magento\Upward\Definition $definition |
| 54 | + * |
| 55 | + * @return string |
| 56 | + * @throws \Magento\Framework\Exception\NoSuchEntityException |
| 57 | + */ |
| 58 | + public function resolve(DefinitionIterator $iterator, Definition $definition) |
| 59 | + { |
| 60 | + $pageInfo = $this->pageTypeResolver |
| 61 | + ->setContext($iterator->getContext()) |
| 62 | + ->getInfo(); |
| 63 | + |
| 64 | + if (!$pageInfo) { |
| 65 | + return ''; |
| 66 | + } |
| 67 | + |
| 68 | + $storeId = (int) $this->storeManager->getStore()->getId(); |
| 69 | + $type = $pageInfo['type']; |
| 70 | + $additionalMap = $this->getAdditionalMap($definition, $type); |
| 71 | + |
| 72 | + $entityData = $this->isPageInfoComplete($pageInfo, $additionalMap) ? |
| 73 | + $pageInfo : |
| 74 | + $this->entityDataProviderComposite->getData( |
| 75 | + $type, |
| 76 | + (int)$pageInfo['id'], |
| 77 | + null, |
| 78 | + $storeId |
| 79 | + ); |
| 80 | + |
| 81 | + if (empty($entityData)) { |
| 82 | + return ''; |
| 83 | + } |
| 84 | + |
| 85 | + $result = $this->filterData( |
| 86 | + $entityData, |
| 87 | + $additionalMap, |
| 88 | + $type |
| 89 | + ); |
| 90 | + $result['redirect_code'] = $pageInfo['redirect_code']; |
| 91 | + $result['relative_url'] = $pageInfo['relative_url']; |
| 92 | + $result['type'] = $type; |
| 93 | + |
| 94 | + return $this->json->serialize($result); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @param \Magento\Upward\Definition $definition |
| 99 | + * @param string $type |
| 100 | + * |
| 101 | + * @return false|string[] |
| 102 | + */ |
| 103 | + public function getAdditionalMap(Definition $definition, string $type) |
| 104 | + { |
| 105 | + $definitionArray = $definition->toArray(); |
| 106 | + $additional = $definitionArray['additional'] ?? null; |
| 107 | + |
| 108 | + if (!$additional) { |
| 109 | + return false; |
| 110 | + } |
| 111 | + $additionalInfo = []; |
| 112 | + $typeCheck = strtolower($type); |
| 113 | + foreach ($additional as $additionalType) { |
| 114 | + if ($additionalType['type'] === $typeCheck) { |
| 115 | + $additionalInfo = explode(',', $additionalType['fetch']); |
| 116 | + |
| 117 | + break; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return $additionalInfo; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @param array $pageInfo |
| 126 | + * @param string[]|bool $additionalMap |
| 127 | + * |
| 128 | + * @return bool |
| 129 | + */ |
| 130 | + public function isPageInfoComplete($pageInfo, $additionalMap): bool |
| 131 | + { |
| 132 | + $pageInfoHasAllData = true; |
| 133 | + if ($additionalMap) { |
| 134 | + foreach($additionalMap as $key) { |
| 135 | + if (!isset($pageInfo[$key])) { |
| 136 | + $pageInfoHasAllData = false; |
| 137 | + |
| 138 | + break; |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return $pageInfoHasAllData; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * @param array $data |
| 148 | + * @param array{type: string, fetch: string}|bool $map |
| 149 | + * |
| 150 | + * @return array |
| 151 | + */ |
| 152 | + public function filterData($data, $map, $type) |
| 153 | + { |
| 154 | + if (!$map || empty($map) || empty($data)) { |
| 155 | + return []; |
| 156 | + } |
| 157 | + |
| 158 | + $result = []; |
| 159 | + foreach($map as $valueKey) { |
| 160 | + $result[$valueKey] = $this->getEntityValue($data, $valueKey, $type); |
| 161 | + } |
| 162 | + |
| 163 | + return $result; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * @param array $data |
| 168 | + * @param string $key |
| 169 | + * @param string $type |
| 170 | + * |
| 171 | + * @return string|null |
| 172 | + */ |
| 173 | + public function getEntityValue(array $data, string $key, string $type) |
| 174 | + { |
| 175 | + if ($key === '__typename') { |
| 176 | + if ($type === 'PRODUCT') { |
| 177 | + return ucfirst($data['type_id']) . 'Product'; |
| 178 | + } |
| 179 | + |
| 180 | + return $data['type_id'] ? ucfirst($data['type_id']) : ucfirst(strtolower($type)); |
| 181 | + } |
| 182 | + |
| 183 | + if ($key === 'id') { |
| 184 | + return $data['id'] ?? $data['entity_id']; |
| 185 | + } |
| 186 | + |
| 187 | + return $data[$key] ?? null; |
| 188 | + } |
| 189 | +} |
0 commit comments