From 154988363074c8ea08dc294d6f0af0f80bbb04b2 Mon Sep 17 00:00:00 2001 From: IrinaZhadzinets Date: Thu, 4 Aug 2022 10:53:37 +0300 Subject: [PATCH 1/2] scandipwa/scandipwa#4680 - Return 404 status code for disabled pages --- src/Controller/Router.php | 85 +++++++++++++++++++++++++++++++++------ 1 file changed, 72 insertions(+), 13 deletions(-) diff --git a/src/Controller/Router.php b/src/Controller/Router.php index 30ac100..0ffb5c3 100644 --- a/src/Controller/Router.php +++ b/src/Controller/Router.php @@ -9,6 +9,10 @@ namespace ScandiPWA\Router\Controller; +use Magento\Catalog\Api\CategoryRepositoryInterface; +use Magento\Catalog\Model\Product\Attribute\Source\Status; +use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; +use Magento\Cms\Api\GetPageByIdentifierInterface; use Magento\Framework\App\ActionFactory; use Magento\Framework\App\ActionInterface; use Magento\Framework\App\Config\ScopeConfigInterface; @@ -58,6 +62,22 @@ class Router extends BaseRouter */ private $themeProvider; + /** + * @var CollectionFactory + */ + protected CollectionFactory $productCollectionFactory; + + /** + * @var CategoryRepositoryInterface + */ + protected CategoryRepositoryInterface $categoryRepository; + + /** + * @var GetPageByIdentifierInterface + */ + protected GetPageByIdentifierInterface $getPageByIdentifier; + + /** * @var array */ @@ -65,19 +85,22 @@ class Router extends BaseRouter /** * Router constructor. - * @param ActionList $actionList - * @param ActionFactory $actionFactory - * @param DefaultPathInterface $defaultPath - * @param ResponseFactory $responseFactory - * @param ConfigInterface $routeConfig - * @param UrlInterface $url - * @param NameBuilder $nameBuilder - * @param PathConfigInterface $pathConfig - * @param ValidationManagerInterface $validationManager - * @param UrlFinderInterface $urlFinder - * @param StoreManagerInterface $storeManager - * @param ScopeConfigInterface $scopeConfig - * @param ThemeProviderInterface $themeProvider + * @param ActionList $actionList + * @param ActionFactory $actionFactory + * @param DefaultPathInterface $defaultPath + * @param ResponseFactory $responseFactory + * @param ConfigInterface $routeConfig + * @param UrlInterface $url + * @param NameBuilder $nameBuilder + * @param PathConfigInterface $pathConfig + * @param ValidationManagerInterface $validationManager + * @param UrlFinderInterface $urlFinder + * @param StoreManagerInterface $storeManager + * @param ScopeConfigInterface $scopeConfig + * @param ThemeProviderInterface $themeProvider + * @param CollectionFactory $productCollectionFactory + * @param CategoryRepositoryInterface $categoryRepository + * @param GetPageByIdentifierInterface $getPageByIdentifier */ public function __construct( ActionList $actionList, @@ -93,6 +116,9 @@ public function __construct( StoreManagerInterface $storeManager, ScopeConfigInterface $scopeConfig, ThemeProviderInterface $themeProvider, + CollectionFactory $productCollectionFactory, + CategoryRepositoryInterface $categoryRepository, + GetPageByIdentifierInterface $getPageByIdentifier, array $ignoredURLs = [] ) { $this->_scopeConfig = $scopeConfig; @@ -101,6 +127,9 @@ public function __construct( $this->urlFinder = $urlFinder; $this->storeManager = $storeManager; $this->ignoredURLs = $ignoredURLs; + $this->productCollectionFactory = $productCollectionFactory; + $this->categoryRepository = $categoryRepository; + $this->getPageByIdentifier = $getPageByIdentifier; parent::__construct( $actionList, @@ -154,6 +183,36 @@ public function match(RequestInterface $request) // Otherwise properly hint response for correct FE app placeholders $action->setType($this->getDefaultActionType($rewrite)); $action->setCode(200)->setPhrase('OK'); + + if ($this->getDefaultActionType($rewrite) === 'PRODUCT') { + $collection = $this->productCollectionFactory->create() + ->addAttributeToFilter('status', ['eq' => Status::STATUS_ENABLED]) + ->addStoreFilter($rewrite->getStoreId()); + $product = $collection->addIdFilter($rewrite->getEntityId())->getFirstItem(); + + if (!$product->hasData()) { + $action->setType('NOT_FOUND'); + $action->setCode(404)->setPhrase('Not Found'); + } + } elseif ($this->getDefaultActionType($rewrite) === 'CATEGORY') { + $storeId = $this->storeManager->getStore()->getId(); + $category = $this->categoryRepository->get($rewrite->getEntityId(), $storeId); + + if (!$category->getIsActive()) { + $action->setType('NOT_FOUND'); + $action->setCode(404)->setPhrase('Not Found'); + } + } + elseif ($this->getDefaultActionType($rewrite) === 'CMS_PAGE') { + $storeId = (int)$this->storeManager->getStore()->getId(); + + try { + $page = $this->getPageByIdentifier->execute($rewrite->getRequestPath(), $storeId); + } catch (NoSuchEntityException $e) { + $action->setType('NOT_FOUND'); + $action->setCode(404)->setPhrase('Not Found'); + } + } } elseif ($this->validationManager->validate($request)) { // Validate custom PWA routing $action->setType('PWA_ROUTER'); $action->setCode(200)->setPhrase('OK'); From bb14b2919fa5e8406fbf4a7693612ec9dbe41f9a Mon Sep 17 00:00:00 2001 From: IrinaZhadzinets Date: Thu, 4 Aug 2022 10:55:55 +0300 Subject: [PATCH 2/2] #4680 - Remove unnessesary empty line --- src/Controller/Router.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Controller/Router.php b/src/Controller/Router.php index 0ffb5c3..4f9492e 100644 --- a/src/Controller/Router.php +++ b/src/Controller/Router.php @@ -77,7 +77,6 @@ class Router extends BaseRouter */ protected GetPageByIdentifierInterface $getPageByIdentifier; - /** * @var array */