From 10324c98103a1f4c58214949c2dfa6bdcd1b96be Mon Sep 17 00:00:00 2001 From: Alfreds Genkins Date: Fri, 8 May 2020 18:02:01 +0300 Subject: [PATCH] Switched the way how menu items were loading the category paths. Now it is much more effictent. --- src/Model/Item.php | 12 +--------- src/Model/Resolver/Menu.php | 47 +++++++++++++++++++++++++------------ 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/src/Model/Item.php b/src/Model/Item.php index ea41764..51c68b8 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -17,16 +17,6 @@ */ class Item extends AbstractModel implements ItemInterface, IdentityInterface { - /** - * CMS page cache tag - */ - const CACHE_TAG = 'sw_mi'; - - /** - * @var string - */ - protected $_cacheTag = 'sw_mi'; - /** * Prefix of model events names * @@ -51,7 +41,7 @@ protected function _construct() */ public function getIdentities() { - return [self::CACHE_TAG . '_' . $this->getId()]; + return [Menu::CACHE_TAG . '_' . $this->getMenuId()]; } /** diff --git a/src/Model/Resolver/Menu.php b/src/Model/Resolver/Menu.php index 952f64e..0b3d837 100644 --- a/src/Model/Resolver/Menu.php +++ b/src/Model/Resolver/Menu.php @@ -12,12 +12,14 @@ namespace ScandiPWA\MenuOrganizer\Model\Resolver; use Magento\Catalog\Api\CategoryRepositoryInterface; +use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\Store\Model\StoreManagerInterface; +use ScandiPWA\MenuOrganizer\Api\Data\ItemInterface; use ScandiPWA\MenuOrganizer\Model\MenuFactory; use ScandiPWA\MenuOrganizer\Model\ResourceModel\Item\CollectionFactory as ItemCollectionFactory; use ScandiPWA\MenuOrganizer\Model\ResourceModel\Menu as MenuResourceModel; @@ -47,14 +49,14 @@ class Menu implements ResolverInterface protected $itemCollectionFactory; /** - * @var CategoryRepositoryInterface + * @var StoreManagerInterface */ - protected $categoryRepository; + protected $storeManager; /** - * @var StoreManagerInterface + * @var CollectionFactory */ - protected $storeManager; + protected $collectionFactory; /** * Menu constructor. @@ -62,20 +64,19 @@ class Menu implements ResolverInterface * @param MenuFactory $menuFactory * @param MenuResourceModel $menuResourceModel * @param ItemCollectionFactory $itemCollectionFactory - * @param CategoryRepositoryInterface $categoryRepository */ public function __construct( StoreManagerInterface $storeManager, MenuFactory $menuFactory, MenuResourceModel $menuResourceModel, ItemCollectionFactory $itemCollectionFactory, - CategoryRepositoryInterface $categoryRepository + CollectionFactory $collectionFactory ) { $this->storeManager = $storeManager; $this->menuFactory = $menuFactory; $this->menuResourceModel = $menuResourceModel; $this->itemCollectionFactory = $itemCollectionFactory; - $this->categoryRepository = $categoryRepository; + $this->collectionFactory = $collectionFactory; } /** @@ -131,16 +132,32 @@ private function getMenuItems(string $menuId): array ->setPositionOrder() ->getData(); - return array_map(function ($item) { - if (isset($item[self::CATEGORY_ID_KEY])) { - $categoryUrlPath = $this->categoryRepository - ->get($item[self::CATEGORY_ID_KEY]) - ->getUrlPath(); + $categoryIds = []; + $itemsMap = []; + + foreach ($menuItems as $item) { + $itemId = $item[ItemInterface::ITEM_ID]; - $item['url'] = DIRECTORY_SEPARATOR . $categoryUrlPath; + if (isset($item[self::CATEGORY_ID_KEY])) { + $catId = $item[self::CATEGORY_ID_KEY]; + $categoryIds[$catId] = $itemId; } - return $item; - }, $menuItems); + $itemsMap[$itemId] = $item; + } + + $collection = $this->collectionFactory->create(); + $categories = $collection + ->addFieldToSelect('url_path') + ->addIdFilter($categoryIds) + ->getItems(); + + foreach ($categories as $category) { + $catId = $category->getId(); + $itemId = $categoryIds[$catId]; + $itemsMap[$itemId]['url'] = DIRECTORY_SEPARATOR . $category->getUrlPath(); + } + + return array_values($itemsMap); } }