diff --git a/src/Model/Resolver/DataProvider/Page.php b/src/Model/Resolver/DataProvider/Page.php index f5f74c1..53464ba 100644 --- a/src/Model/Resolver/DataProvider/Page.php +++ b/src/Model/Resolver/DataProvider/Page.php @@ -11,7 +11,10 @@ namespace ScandiPWA\CmsGraphQl\Model\Resolver\DataProvider; +use Magento\Framework\Exception\LocalizedException; use ScandiPWA\CmsGraphQl\Api\Data\PageInterface; +use Magento\Cms\Api\Data\PageInterface as OriginalPageInterface; +use Magento\Cms\Api\GetPageByIdentifierInterface; use Magento\Cms\Api\PageRepositoryInterface; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Widget\Model\Template\FilterEmulate; @@ -22,37 +25,72 @@ class Page extends \Magento\CmsGraphQl\Model\Resolver\DataProvider\Page { /** - * @var FilterEmulate + * @var GetPageByIdentifierInterface */ - private $widgetFilter; + private $pageByIdentifier; /** * @var PageRepositoryInterface */ private $pageRepository; + /** + * @var FilterEmulate + */ + private $widgetFilter; + /** * @param PageRepositoryInterface $pageRepository * @param FilterEmulate $widgetFilter + * @param GetPageByIdentifierInterface $getPageByIdentifier */ public function __construct( PageRepositoryInterface $pageRepository, - FilterEmulate $widgetFilter + FilterEmulate $widgetFilter, + GetPageByIdentifierInterface $getPageByIdentifier ) { + $this->pageRepository = $pageRepository; $this->widgetFilter = $widgetFilter; + $this->pageByIdentifier = $getPageByIdentifier; } /** * @param int $pageId * @return array * @throws NoSuchEntityException + * @throws LocalizedException */ - public function getData(int $pageId): array + public function getDataByPageId(int $pageId): array { $page = $this->pageRepository->getById($pageId); - if (!$page->isActive()) { + return $this->convertPageData($page); + } + + /** + * @param string $pageIdentifier + * @param int $storeId + * @return array + * @throws NoSuchEntityException + */ + public function getDataByPageIdentifier(string $pageIdentifier, int $storeId): array + { + $page = $this->pageByIdentifier->execute($pageIdentifier, $storeId); + + return $this->convertPageData($page); + } + + /** + * Convert page data + * + * @param OriginalPageInterface $page + * @return array + * @throws NoSuchEntityException + */ + private function convertPageData(OriginalPageInterface $page) + { + if (false === $page->isActive()) { throw new NoSuchEntityException(); } @@ -68,6 +106,8 @@ public function getData(int $pageId): array PageInterface::META_TITLE => $page->getMetaTitle(), PageInterface::META_DESCRIPTION => $page->getMetaDescription(), PageInterface::META_KEYWORDS => $page->getMetaKeywords(), + PageInterface::PAGE_ID => $page->getId(), + PageInterface::IDENTIFIER => $page->getIdentifier(), ]; return $pageData; diff --git a/src/Model/Resolver/Page.php b/src/Model/Resolver/Page.php deleted file mode 100644 index 02bb854..0000000 --- a/src/Model/Resolver/Page.php +++ /dev/null @@ -1,122 +0,0 @@ - - * @copyright Copyright (c) 2018 Scandiweb, Ltd (https://scandiweb.com) - */ - -namespace ScandiPWA\CmsGraphQl\Model\Resolver; - -use Magento\CmsGraphQl\Model\Resolver\DataProvider\Page as PageDataProvider; -use Magento\Framework\Exception\NoSuchEntityException; -use Magento\Framework\GraphQl\Config\Element\Field; -use Magento\Framework\GraphQl\Exception\GraphQlInputException; -use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; -use Magento\Framework\GraphQl\Query\Resolver\Value; -use Magento\Framework\GraphQl\Query\Resolver\ValueFactory; -use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; -use Magento\Cms\Model\PageFactory; -use Magento\Store\Model\StoreManagerInterface; - -/** - * Class Page - * @package ScandiPWA\CmsGraphQl\Model\Resolver - */ -class Page extends \Magento\CmsGraphQl\Model\Resolver\Page -{ - /** - * @var PageDataProvider - */ - private $pageDataProvider; - /** - * @var ValueFactory - */ - private $valueFactory; - /** - * @var PageFactory - */ - private $pageFactory; - /** - * @var StoreManagerInterface - */ - private $storeManager; - - /** - * @param PageDataProvider $pageDataProvider - * @param ValueFactory $valueFactory - */ - public function __construct( - PageDataProvider $pageDataProvider, - ValueFactory $valueFactory, - PageFactory $pageFactory, - StoreManagerInterface $storeManager - ) { - parent::__construct( - $pageDataProvider - ); - $this->pageDataProvider = $pageDataProvider; - $this->valueFactory = $valueFactory; - $this->pageFactory = $pageFactory; - $this->storeManager = $storeManager; - } - - /** - * @inheritdoc - */ - public function resolve( - Field $field, - $context, - ResolveInfo $info, - array $value = null, - array $args = null - ): Value { - $result = function () use ($args) { - $pageId = $this->getPageId($args); - $pageData = $this->getPageData($pageId); - - return $pageData; - }; - - return $this->valueFactory->create($result); - } - - /** - * @param array $args - * @return int - * @throws GraphQlInputException - */ - private function getPageId(array $args): int - { - $storeId = $this->storeManager->getStore()->getId(); - if (isset($args['id'])) { - return (int)$args['id']; - } - if (isset($args['url_key'])) { - return (int)$this->pageFactory->create() - ->setStoreId($storeId) - ->load($args['url_key']) - ->setStoreId($storeId) - ->getId(); - } - throw new GraphQlInputException(__('Page id should be specified')); - } - - /** - * @param int $pageId - * @return array - * @throws GraphQlNoSuchEntityException - */ - private function getPageData(int $pageId): array - { - try { - $pageData = $this->pageDataProvider->getData($pageId); - } catch (NoSuchEntityException $e) { - throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e); - } - - return $pageData; - } -} diff --git a/src/etc/graphql/di.xml b/src/etc/graphql/di.xml index 5e213b8..3797502 100644 --- a/src/etc/graphql/di.xml +++ b/src/etc/graphql/di.xml @@ -1,6 +1,5 @@ -