Skip to content

Commit

Permalink
Merge pull request #6 from mihailspopovs4/menu-organizer-better-errors
Browse files Browse the repository at this point in the history
Improvements for Menu resolver
  • Loading branch information
Ilja Lapkovskis authored Sep 26, 2019
2 parents 8308b09 + 866b400 commit 5a35e51
Showing 1 changed file with 55 additions and 51 deletions.
106 changes: 55 additions & 51 deletions src/Model/Resolver/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@

namespace ScandiPWA\MenuOrganizer\Model\Resolver;

use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Catalog\Api\CategoryRepositoryInterface;

use ScandiPWA\MenuOrganizer\Model\ResourceModel\Menu\CollectionFactory as MenuCollectionFactory;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use ScandiPWA\MenuOrganizer\Model\MenuFactory;
use ScandiPWA\MenuOrganizer\Model\ResourceModel\Item\CollectionFactory as ItemCollectionFactory;
use ScandiPWA\MenuOrganizer\Model\ResourceModel\Menu as MenuResourceModel;

/**
* Class Menu
Expand All @@ -28,15 +26,17 @@
*/
class Menu implements ResolverInterface
{
public const CATEGORY_ID_KEY = 'category_id';

/**
* @var ValueFactory
* @var MenuFactory
*/
private $valueFactory;
protected $menuFactory;

/**
* @var MenuCollectionFactory
* @var MenuResourceModel
*/
protected $menuCollectionFactory;
protected $menuResourceModel;

/**
* @var ItemCollectionFactory
Expand All @@ -50,19 +50,19 @@ class Menu implements ResolverInterface

/**
* Menu constructor.
* @param ValueFactory $valueFactory
* @param MenuCollectionFactory $menuCollectionFactory
* @param MenuFactory $menuFactory
* @param ItemCollectionFactory $itemCollectionFactory
* @param CategoryRepositoryInterface $categoryRepository
*/
public function __construct(
ValueFactory $valueFactory,
MenuCollectionFactory $menuCollectionFactory,
MenuFactory $menuFactory,
MenuResourceModel $menuResourceModel,
ItemCollectionFactory $itemCollectionFactory,
CategoryRepositoryInterface $categoryRepository
) {
$this->valueFactory = $valueFactory;
$this->menuCollectionFactory = $menuCollectionFactory;
)
{
$this->menuFactory = $menuFactory;
$this->menuResourceModel = $menuResourceModel;
$this->itemCollectionFactory = $itemCollectionFactory;
$this->categoryRepository = $categoryRepository;
}
Expand All @@ -75,7 +75,6 @@ public function __construct(
* @param ResolveInfo $info
* @param array|null $value
* @param array|null $args
* @return Value
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function resolve(
Expand All @@ -84,43 +83,48 @@ public function resolve(
ResolveInfo $info,
array $value = null,
array $args = null
): Value {
$result = function () {
return null;
};

if (isset($args['identifier'])) {
$menu = $this->menuCollectionFactory->create();

/** Updated with identifier filtering */
$menu->addFieldToFilter('identifier', $args['identifier'])->load();
)
{
$identifier = $args['identifier'];

$menuData = $menu->getFirstItem()->getData();
$menu = $this->menuFactory->create();
$this->menuResourceModel->load($menu, $identifier);

$items = $this->itemCollectionFactory->create();

/** Updated with menu id taken from menu data */
$items->addMenuFilter($menuData['menu_id'])
->addStatusFilter()
->setParentIdOrder()
->setPositionOrder();

$menuData['items'] = $items->getData();
if ($menu->getId() === null) {
throw new \InvalidArgumentException(sprintf("Could not find menu with identifier '%s'", $identifier));
}

foreach ($menuData['items'] as &$item) {
if (isset($item['category_id'])) {
$category = $this->categoryRepository->get($item['category_id']);
$item['url'] = sprintf('/%s', $category->getUrlPath());
}
}
return array_merge(
$menu->getData(),
[
'items' => $this->getMenuItems($menu['menu_id'])
]);
}

if ($menuData) {
$result = function () use ($menuData) {
return $menuData;
};
/**
* @param string $menuId
* @return array
*/
private function getMenuItems(string $menuId): array
{
$menuItems = $this->itemCollectionFactory
->create()
->addMenuFilter($menuId)
->addStatusFilter()
->setParentIdOrder()
->setPositionOrder()
->getData();

return array_map(function ($item) {
if (isset($item[self::CATEGORY_ID_KEY])) {
$categoryUrlPath = $this->categoryRepository
->get($item[self::CATEGORY_ID_KEY])
->getUrlPath();

$item['url'] = DIRECTORY_SEPARATOR . $categoryUrlPath;
}
}

return $this->valueFactory->create($result);
return $item;
}, $menuItems);
}
}
}

0 comments on commit 5a35e51

Please sign in to comment.