Skip to content

Commit

Permalink
Merge pull request #27 from matesich/refactor-action
Browse files Browse the repository at this point in the history
added store configs to action
  • Loading branch information
AleksandrsKondratjevs authored Feb 2, 2023
2 parents a8db01f + d166e2c commit 22bb87c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 9 deletions.
17 changes: 17 additions & 0 deletions src/Controller/Pwa.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ class Pwa extends Action implements HttpGetActionInterface, HttpPostActionInterf
*/
protected $catalogDefaultSortBy;

/**
* @var array|null
*/
protected $storeConfig;

/**
* @param string $type
* @return $this
Expand Down Expand Up @@ -176,6 +181,16 @@ public function setCatalogDefaultSortBy(string $catalogDefaultSortBy): self
return $this;
}

/**
* @param $storeConfig
* @return Pwa
*/
public function setStoreConfig($storeConfig): self
{
$this->storeConfig = $storeConfig;
return $this;
}

/**
* Rewrite constructor.
* @param Context $context
Expand All @@ -195,6 +210,7 @@ public function __construct(
$this->cmsPage = null;
$this->description = '';
$this->catalogDefaultSortBy = '';
$this->storeConfig = null;
parent::__construct($context);
}

Expand All @@ -216,6 +232,7 @@ public function execute()
$resultLayout->setDisplayMode($this->display_mode);
$resultLayout->setDescription($this->description);
$resultLayout->setCatalogDefaultSortBy($this->catalogDefaultSortBy);
$resultLayout->setStoreConfig($this->storeConfig);
try{
$templateName = 'pwa-root';
$resultLayout->setRootTemplate($templateName);
Expand Down
37 changes: 28 additions & 9 deletions src/Controller/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ class Router extends BaseRouter
const PAGE_TYPE_CATEGORY = 'CATEGORY';
const PAGE_TYPE_CMS_PAGE = 'CMS_PAGE';

const CRUCIAL_STORE_CONFIG_VALUES = [
'cms_home_page' => self::XML_PATH_CMS_HOME_PAGE,
'catalog_default_sort_by' => self::XML_PATH_CATALOG_DEFAULT_SORT_BY
];

/**
* @var ValidationManagerInterface
*/
Expand Down Expand Up @@ -218,13 +223,6 @@ public function match(RequestInterface $request)
$action = $this->actionFactory->create(Pwa::class);
$rewrite = $this->getRewrite($request);

$catalogDefaultSortBy = $this->scopeConfig->getValue(
self::XML_PATH_CATALOG_DEFAULT_SORT_BY,
ScopeInterface::SCOPE_STORE,
$this->storeId
);

$action->setCatalogDefaultSortBy($catalogDefaultSortBy);

if ($rewrite) {
// Do not execute any action for external rewrites,
Expand All @@ -245,7 +243,7 @@ public function match(RequestInterface $request)
$action->setCode(404)->setPhrase('Not Found');
}

if ($this->isHomePage($request)) {
if ($this->isHomePage($request, $action)) {
$this->setResponseHomePage($action);
}

Expand Down Expand Up @@ -276,6 +274,8 @@ protected function setPageDetails(UrlRewrite $urlRewrite, ActionInterface $actio
$this->setResponseCategory($entityId, $action);
break;
}

$this->setStoreConfigs($action);
}

protected function setResponseHomePage(ActionInterface $action)
Expand Down Expand Up @@ -365,11 +365,30 @@ protected function setResponseCategory($id, ActionInterface $action)
$action->setName($category->getName() ?? '');
$action->setDisplayMode($category->getDisplayMode() ?? '');
$action->setDescription($category->getDescription() ?? '');
$action->setCatalogDefaultSortBy($category->getCatalogDefaultSortBy() ?? '');
} catch (NoSuchEntityException $e) {
$this->setNotFound($action);
}
}

protected function setStoreConfigs(ActionInterface $action)
{
$storeConfig = [];
$crucialStoreConfigs = self::CRUCIAL_STORE_CONFIG_VALUES;

foreach ($crucialStoreConfigs as $configKey => $path) {
$configValue = $this->scopeConfig->getValue(
$path,
ScopeInterface::SCOPE_STORE,
$this->storeId
);

$storeConfig[$configKey] = $configValue;
}

$action->setStoreConfig($storeConfig);
}

/**
* Set "404 Not Found" response
*
Expand Down Expand Up @@ -496,7 +515,7 @@ protected function isHomePage(RequestInterface $request): bool
{
$requestPath = $request->getPathInfo();

if(!$requestPath) {
if(!$requestPath || $requestPath === '/') {
return true;
}

Expand Down
21 changes: 21 additions & 0 deletions src/View/Result/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ class Page extends ExtendedPage
*/
protected $catalogDefaultSortBy;

/**
* @var array|null
*/
protected $storeConfig;

/**
* Constructor
*
Expand Down Expand Up @@ -183,6 +188,7 @@ public function __construct(
$this->cmsPage = null;
$this->description = '';
$this->catalogDefaultSortBy = '';
$this->storeConfig = null;
}

/**
Expand Down Expand Up @@ -316,6 +322,21 @@ public function getCatalogDefaultSortBy()
return $this->catalogDefaultSortBy;
}

public function setStoreConfig($storeConfig)
{
if($this->storeConfig === null) {
$this->storeConfig = $storeConfig;
return $this;
}

return null;
}

public function getCatalogDefaultSortByConfig()
{
return $this->storeConfig;
}

/**
* @param string $template
* @return Page
Expand Down

0 comments on commit 22bb87c

Please sign in to comment.