-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from krystian15/patch-2
Update Page.php
- Loading branch information
Showing
1 changed file
with
21 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,10 @@ | |
/** | ||
* ScandiPWA_CmsGraphQl | ||
* | ||
* @category Scandiweb | ||
* @package ScandiPWA_CmsGraphQl | ||
* @author Kriss Andrejevs <[email protected]> | ||
* @copyright Copyright (c) 2018 Scandiweb, Ltd (https://scandiweb.com) | ||
* @category Scandiweb | ||
* @package ScandiPWA_CmsGraphQl | ||
* @author Kriss Andrejevs <[email protected]> | ||
* @copyright Copyright (c) 2018 Scandiweb, Ltd (https://scandiweb.com) | ||
*/ | ||
|
||
namespace ScandiPWA\CmsGraphQl\Model\Resolver; | ||
|
@@ -19,28 +19,30 @@ | |
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 | ||
|
@@ -49,14 +51,16 @@ class Page extends \Magento\CmsGraphQl\Model\Resolver\Page | |
public function __construct( | ||
PageDataProvider $pageDataProvider, | ||
ValueFactory $valueFactory, | ||
PageFactory $pageFactory | ||
PageFactory $pageFactory, | ||
StoreManagerInterface $storeManager | ||
) { | ||
parent::__construct( | ||
$pageDataProvider | ||
); | ||
$this->pageDataProvider = $pageDataProvider; | ||
$this->valueFactory = $valueFactory; | ||
$this->pageFactory = $pageFactory; | ||
$this->storeManager = $storeManager; | ||
} | ||
|
||
/** | ||
|
@@ -68,14 +72,14 @@ public function resolve( | |
ResolveInfo $info, | ||
array $value = null, | ||
array $args = null | ||
) : Value { | ||
|
||
): Value { | ||
$result = function () use ($args) { | ||
$pageId = $this->getPageId($args); | ||
$pageData = $this->getPageData($pageId); | ||
|
||
return $pageData; | ||
}; | ||
|
||
return $this->valueFactory->create($result); | ||
} | ||
|
||
|
@@ -86,16 +90,18 @@ public function resolve( | |
*/ | ||
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()->load($args['url_key'])->getId(); | ||
return (int)$this->pageFactory->create() | ||
->setStoreId($storeId) | ||
->load($args['url_key']) | ||
->setStoreId($storeId) | ||
->getId(); | ||
} | ||
|
||
throw new GraphQlInputException(__('Page id should be specified')); | ||
|
||
} | ||
|
||
/** | ||
|
@@ -110,7 +116,7 @@ private function getPageData(int $pageId): array | |
} catch (NoSuchEntityException $e) { | ||
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e); | ||
} | ||
|
||
return $pageData; | ||
} | ||
|
||
} |