Skip to content

Commit

Permalink
Merge branch 'master' into refactor-action
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksandrsKondratjevs authored Feb 2, 2023
2 parents 0b2c088 + a8db01f commit d166e2c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
14 changes: 7 additions & 7 deletions src/Controller/Pwa.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ class Pwa extends Action implements HttpGetActionInterface, HttpPostActionInterf
protected $display_mode;

/**
* @var string
* @var array|null
*/
protected $identifier;
protected $cmsPage;

/**
* @var string
Expand Down Expand Up @@ -152,12 +152,12 @@ public function setDisplayMode(string $display_mode): self
}

/**
* @param string $identifier
* @param $cmsPage
* @return Pwa
*/
public function setIdentifier(string $identifier): self
public function setCmsPage($cmsPage): self
{
$this->identifier = $identifier;
$this->cmsPage = $cmsPage;
return $this;
}

Expand Down Expand Up @@ -207,7 +207,7 @@ public function __construct(
$this->sku = '';
$this->name = '';
$this->display_mode = '';
$this->identifier = '';
$this->cmsPage = null;
$this->description = '';
$this->catalogDefaultSortBy = '';
$this->storeConfig = null;
Expand All @@ -228,7 +228,7 @@ public function execute()
$resultLayout->setId($this->id);
$resultLayout->setSku($this->sku);
$resultLayout->setName($this->name);
$resultLayout->setIdentifier($this->identifier);
$resultLayout->setCmsPage($this->cmsPage);
$resultLayout->setDisplayMode($this->display_mode);
$resultLayout->setDescription($this->description);
$resultLayout->setCatalogDefaultSortBy($this->catalogDefaultSortBy);
Expand Down
33 changes: 23 additions & 10 deletions src/Controller/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Magento\Catalog\Model\ProductRepository;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use ScandiPWA\CmsGraphQl\Model\Resolver\DataProvider\Page as PageProvider;

class Router extends BaseRouter
{
Expand Down Expand Up @@ -105,6 +106,11 @@ class Router extends BaseRouter
*/
protected $productRepository;

/**
* @var PageProvider
*/
protected PageProvider $pageProvider;

/**
* Router constructor.
* @param ActionList $actionList
Expand All @@ -123,6 +129,7 @@ class Router extends BaseRouter
* @param PageFactory $pageFactory
* @param ProductRepository $productRepository
* @param CategoryRepositoryInterface $categoryRepository
* @param PageProvider $pageProvider
*/
public function __construct(
ActionList $actionList,
Expand All @@ -141,6 +148,7 @@ public function __construct(
PageFactory $pageFactory,
ProductRepository $productRepository,
CategoryRepositoryInterface $categoryRepository,
PageProvider $pageProvider,
array $ignoredURLs = []
) {
$this->scopeConfig = $scopeConfig;
Expand All @@ -151,6 +159,7 @@ public function __construct(
$this->pageFactory = $pageFactory;
$this->productRepository = $productRepository;
$this->categoryRepository = $categoryRepository;
$this->pageProvider = $pageProvider;
$this->ignoredURLs = $ignoredURLs;
$this->storeId = $this->storeManager->getStore()->getId();

Expand Down Expand Up @@ -277,8 +286,15 @@ protected function setResponseHomePage(ActionInterface $action)
$this->storeId
);

$action->setType(self::PAGE_TYPE_CMS_PAGE);
$action->setIdentifier($homePageIdentifier ?? '');
try {
$page = $this->pageProvider->getDataByPageIdentifier($homePageIdentifier, (int) $this->storeId);

$action->setType(self::PAGE_TYPE_CMS_PAGE);
$action->setId($page['page_id'] ?? '');
$action->setCmsPage($page);
} catch (\Throwable $th) {
$action->setType('PWA_ROUTER');
}
}

/**
Expand All @@ -291,17 +307,14 @@ protected function setResponseHomePage(ActionInterface $action)
*/
protected function setResponseCmsPage($id, ActionInterface $action)
{
$page = $this->pageFactory->create()
->setStoreId($this->storeId)
->load($id);
try {
$page = $this->pageProvider->getDataByPageId((int) $id, (int) $this->storeId);

if (!$page->getId() || !$page->isActive()) {
$action->setId($page['page_id'] ?? '');
$action->setCmsPage($page);
} catch (\Throwable $th) {
$this->setNotFound($action);
return;
}

$action->setId($page->getId() ?? '');
$action->setIdentifier($page->getIdentifier() ?? '');
}

/**
Expand Down
19 changes: 9 additions & 10 deletions src/View/Result/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ class Page extends ExtendedPage
protected $display_mode;

/**
* @var string
* @var array|null
*/
protected $identifier;
protected $cmsPage;

/**
* @var string
Expand Down Expand Up @@ -185,7 +185,7 @@ public function __construct(
$this->sku = '';
$this->name = '';
$this->display_mode = '';
$this->identifier = '';
$this->cmsPage = null;
$this->description = '';
$this->catalogDefaultSortBy = '';
$this->storeConfig = null;
Expand Down Expand Up @@ -277,19 +277,19 @@ public function getDisplayMode()
return $this->display_mode;
}

public function setIdentifier(string $identifier)
public function setCmsPage($cmsPage)
{
if($this->identifier === '') {
$this->identifier = $identifier;
if($this->cmsPage === null) {
$this->cmsPage = $cmsPage;
return $this;
}

return '';
return null;
}

public function getIdentifier()
public function getCmsPage()
{
return $this->identifier;
return $this->cmsPage;
}

public function setDescription(string $description)
Expand Down Expand Up @@ -351,5 +351,4 @@ public function setRootTemplate($template)
}
return $this;
}

}

0 comments on commit d166e2c

Please sign in to comment.