Skip to content

Commit

Permalink
Switched the way how menu items were loading the category paths. Now …
Browse files Browse the repository at this point in the history
…it is much more effictent.
  • Loading branch information
alfredsgenkins committed May 8, 2020
1 parent 6f0ab80 commit 10324c9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
12 changes: 1 addition & 11 deletions src/Model/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -51,7 +41,7 @@ protected function _construct()
*/
public function getIdentities()
{
return [self::CACHE_TAG . '_' . $this->getId()];
return [Menu::CACHE_TAG . '_' . $this->getMenuId()];
}

/**
Expand Down
47 changes: 32 additions & 15 deletions src/Model/Resolver/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -47,35 +49,34 @@ class Menu implements ResolverInterface
protected $itemCollectionFactory;

/**
* @var CategoryRepositoryInterface
* @var StoreManagerInterface
*/
protected $categoryRepository;
protected $storeManager;

/**
* @var StoreManagerInterface
* @var CollectionFactory
*/
protected $storeManager;
protected $collectionFactory;

/**
* Menu constructor.
* @param StoreManagerInterface $storeManager
* @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;
}

/**
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 10324c9

Please sign in to comment.