Skip to content

Commit

Permalink
Merge branch 'feature/SMM-18' into test/stage
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwaclawczyk committed Jan 8, 2025
2 parents f46ac12 + b396eb6 commit 7f27a91
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 53 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Add missing $escaper and $viewModels declarations ([#350](https://github.com/SnowdogApps/magento2-menu/pull/350))
- Import categories by store view ([#352](https://github.com/SnowdogApps/magento2-menu/pull/352))
- Add external vue providers config option
- Improve graphql performance

## [2.27.2] - 2024-11-08
### Fixed
Expand Down
20 changes: 13 additions & 7 deletions Model/GraphQl/Resolver/DataProvider/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Snowdog\Menu\Model\GraphQl\Resolver\DataProvider;

use Magento\Framework\Exception\LocalizedException;
use Exception;
use Magento\Framework\Exception\NoSuchEntityException;
use Snowdog\Menu\Api\Data\NodeInterface;
use Snowdog\Menu\Api\NodeRepositoryInterface;
Expand Down Expand Up @@ -124,6 +124,7 @@ private function convertData(NodeInterface $node): array
*/
private function loadModels($nodes, $storeId): void
{
$modelsToLoadByType = [];
/** @var NodeInterface $node */
foreach ($nodes as $node) {
$type = $node->getType();
Expand All @@ -133,12 +134,18 @@ private function loadModels($nodes, $storeId): void
if (!in_array($type, TypeModel::TYPES)) {
continue;
}
$modelsToLoadByType[$type][] = $node->getContent();
}

foreach ($modelsToLoadByType as $type => $ids) {
if (!is_array($ids)) {
continue;
}
try {
$model = $this->typeModel->getModel($type, $node->getContent(), $storeId);
} catch (NoSuchEntityException|LocalizedException $e) {
$model = null;
$this->loadedModels[$type] = $this->typeModel->getModels($type, $ids ?? [], $storeId);
} catch (Exception $e) {
continue;
}
$this->loadedModels[$type][$node->getContent()] = $model;
}
}

Expand All @@ -152,8 +159,7 @@ private function getUrlKey(NodeInterface $node): ?string
if (!isset($this->loadedModels[$node->getType()][$node->getContent()])) {
return null;
}
$currentModel = $this->loadedModels[$node->getType()][$node->getContent()];
return $this->typeModel->getModelUrlKey($node->getType(), $currentModel);
return $this->loadedModels[$node->getType()][$node->getContent()];
} elseif ($node->getType() == self::NODE_TYPE_CUSTOM_URL) {
return $node->getContent();
} else {
Expand Down
56 changes: 11 additions & 45 deletions Model/GraphQl/Resolver/DataProvider/Node/TypeModel.php
Original file line number Diff line number Diff line change
@@ -1,68 +1,34 @@
<?php
declare(strict_types=1);

namespace Snowdog\Menu\Model\GraphQl\Resolver\DataProvider\Node;

use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Cms\Api\Data\PageInterface;
use Magento\Cms\Api\PageRepositoryInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;

class TypeModel
{
const TYPES = ["category", "product", "cms_page"];

/**
* @var ProductRepositoryInterface
*/
private $productRepository;
/**
* @var CategoryRepositoryInterface
*/
private $categoryRepository;

/**
* @var PageRepositoryInterface
* @var \Snowdog\Menu\Model\ResourceModel\NodeType\AbstractNode[]
*/
private $pageRepository;
private $typeModels = [];

public function __construct(
ProductRepositoryInterface $productRepository,
CategoryRepositoryInterface $categoryRepository,
PageRepositoryInterface $pageRepository
array $typeModels = []
) {
$this->productRepository = $productRepository;
$this->categoryRepository = $categoryRepository;
$this->pageRepository = $pageRepository;
$this->typeModels = $typeModels;
}

/**
* @param $type
* @param $modelId
* @param $storeId
* @return ProductInterface|CategoryInterface|PageInterface|null
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function getModel($type, $modelId, $storeId)
public function getModels($type, $modelIds, $storeId)
{
$model = null;
switch ($type) {
case "product":
$model = $this->productRepository->getById($modelId, false, $storeId);
break;
case "category":
$model = $this->categoryRepository->get($modelId, $storeId);
break;
case "cms_page":
$model = $this->pageRepository->getById($modelId);
break;
default:
break;
if (isset($this->typeModels[$type])) {
return $this->typeModels[$type]->fetchData($storeId, $modelIds);
}
return $model;

return [];
}

public function getModelUrlKey($type, $model): ?string
Expand Down
10 changes: 10 additions & 0 deletions etc/graphql/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,14 @@
</argument>
</arguments>
</type>

<type name="Snowdog\Menu\Model\GraphQl\Resolver\DataProvider\Node\TypeModel">
<arguments>
<argument name="typeModels" xsi:type="array">
<item name="product" xsi:type="object">Snowdog\Menu\Model\ResourceModel\NodeType\Product</item>
<item name="category" xsi:type="object">Snowdog\Menu\Model\ResourceModel\NodeType\Category</item>
<item name="cms_page" xsi:type="object">Snowdog\Menu\Model\ResourceModel\NodeType\CmsPage</item>
</argument>
</arguments>
</type>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ $heroicons = $viewModels->require(HeroiconsOutline::class);
?>
<li
class="
<?= /* @noEscape */ (string) $nodeType === 'wrapper' ? $escaper->escapeHtmlAttr($nodeClasses) : '' ?>"
<?= /* @noEscape */ (string) $nodeType === 'wrapper' ? $escaper->escapeHtmlAttr($nodeClasses) : '' ?>
<?= (/* @noEscape */ (string) $nodeType === 'wrapper') && (/* @noEscape */ (int) $level > 2) ? '-ml-4' : '' ?>
"
>
Expand Down

0 comments on commit 7f27a91

Please sign in to comment.