From f0df33294dc9085c9584469516e86befb59b096e Mon Sep 17 00:00:00 2001 From: Alfreds Genkins Date: Tue, 25 Aug 2020 12:29:22 +0300 Subject: [PATCH] Removed unused validators. Patched rredirect URL. --- src/Controller/Router.php | 32 +++++----- src/Validator/Category.php | 93 --------------------------- src/Validator/Cms.php | 53 ---------------- src/Validator/Product.php | 126 ------------------------------------- src/etc/di.xml | 6 +- 5 files changed, 16 insertions(+), 294 deletions(-) delete mode 100644 src/Validator/Category.php delete mode 100644 src/Validator/Cms.php delete mode 100644 src/Validator/Product.php diff --git a/src/Controller/Router.php b/src/Controller/Router.php index 20d9fab..30ac100 100644 --- a/src/Controller/Router.php +++ b/src/Controller/Router.php @@ -37,32 +37,32 @@ class Router extends BaseRouter * @var ValidationManagerInterface */ protected $validationManager; - + /** * @var array */ protected $paths; - + /** * @var StoreManagerInterface */ private $storeManager; - + /** * @var UrlFinderInterface */ private $urlFinder; - + /** * @var ThemeProviderInterface */ private $themeProvider; - + /** * @var array */ private $ignoredURLs; - + /** * Router constructor. * @param ActionList $actionList @@ -113,7 +113,7 @@ public function __construct( $pathConfig ); } - + /** * @param RequestInterface $request * @return ActionInterface|null @@ -238,12 +238,8 @@ protected function redirectOn301(RequestInterface $request): void if ($rewrite && in_array($rewrite->getRedirectType(), [301, 302])) { $target = $rewrite->getTargetPath(); - - if ($target[0] !== '/') { - $target = '/' . $target; - } - - $this->_performRedirect($target, $rewrite->getRedirectType()); + $url = $this->_url->getDirectUrl($target); + $this->_performRedirect($url, $rewrite->getRedirectType()); } } @@ -269,17 +265,17 @@ protected function _performRedirect(string $url, int $type = 302): void protected function isRequestIgnored(RequestInterface $request): bool { $requestPath = $request->getPathInfo(); - + foreach ($this->ignoredURLs as $pattern) { // Use | as delimiter to allow / without escaping if (preg_match('|' . $pattern . '|', $requestPath)) { return true; } } - + return false; } - + /** * @param RequestInterface $request * @param string $path @@ -290,11 +286,11 @@ protected function _checkShouldBeSecure(RequestInterface $request, $path = '') if ($request->getPostValue()) { return; } - + if ($this->pathConfig->shouldBeSecure($path) && !$request->isSecure()) { $alias = $request->getAlias(Url::REWRITE_REQUEST_PATH_ALIAS) ?: $request->getPathInfo(); $url = $this->storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_WEB) . "$alias"; - + if ($this->_shouldRedirectToSecure()) { $url = $this->_url->getRedirectUrl($url); } diff --git a/src/Validator/Category.php b/src/Validator/Category.php deleted file mode 100644 index 1ddeda0..0000000 --- a/src/Validator/Category.php +++ /dev/null @@ -1,93 +0,0 @@ - - * @copyright Copyright (c) 2019 Scandiweb, Ltd (http://scandiweb.com) - * @license OSL-3.0 - */ - -namespace ScandiPWA\Router\Validator; - - -use Magento\Catalog\Model\Category as MagentoCategory; -use Magento\Framework\App\Config\ScopeConfigInterface; -use Magento\Framework\App\RequestInterface; -use Magento\Framework\Exception\LocalizedException; -use ScandiPWA\Router\PathTrait; -use ScandiPWA\Router\ValidatorInterface; -use Magento\Catalog\Model\ResourceModel\Category\Collection; - -/** - * Class Category - * @package ScandiPWA\Router\Validator - */ -class Category implements ValidatorInterface -{ - use PathTrait; - - /** @var Collection */ - protected $categoryCollection; - - /** @var ScopeConfigInterface */ - protected $scopeConfig; - - /** - * Category constructor. - * @param Collection $categoryCollection - * @param ScopeConfigInterface $scopeConfig - */ - public function __construct( - Collection $categoryCollection, - ScopeConfigInterface $scopeConfig - ) { - $this->categoryCollection = $categoryCollection; - $this->scopeConfig = $scopeConfig; - } - - /** - * @inheritdoc - * @throws LocalizedException - */ - public function validateRequest(RequestInterface $request): bool - { - $urlPath = $this->getPathFrontName($request); - - /** @var $category MagentoCategory */ - $category = $this->categoryCollection - ->addAttributeToFilter('url_path', $urlPath) - ->addAttributeToSelect(['entity_id']) - ->getFirstItem(); - - $categoryId = $category->getEntityId(); - - if (!$categoryId) { - return false; - } - - $pageNumber = $request->getParam('page') ?? 1; - $productsCount = $category->getProductCollection()->count(); - - return $this->pageExist($pageNumber, $productsCount); - } - - /** - * Checks whether page exists on category - * @param int $pageNumber - * @param int $productsCount - * @return bool - */ - private function pageExist(int $pageNumber, int $productsCount): bool - { - if ($pageNumber <= 0) { - return false; - } - - $pageSize = $this->scopeConfig->getValue('catalog/frontend/grid_per_page'); - // '< 0' - previous Page does not exist, '== 0' - previous page is the last page, '> 0' next page does exist - $remainingProducts = $productsCount - ($pageSize * ($pageNumber - 1)); - - return $remainingProducts > 0; - } -} diff --git a/src/Validator/Cms.php b/src/Validator/Cms.php deleted file mode 100644 index 5359c4d..0000000 --- a/src/Validator/Cms.php +++ /dev/null @@ -1,53 +0,0 @@ - - * @copyright Copyright (c) 2019 Scandiweb, Ltd (http://scandiweb.com) - * @license OSL-3.0 - */ - -namespace ScandiPWA\Router\Validator; - -use Magento\Cms\Model\GetPageByIdentifier; -use Magento\Framework\App\RequestInterface; -use ScandiPWA\Router\PathTrait; -use ScandiPWA\Router\ValidatorInterface; -use Magento\Framework\Exception\NoSuchEntityException; - -/** - * Class Cms - * @package ScandiPWA\Router\Validator - */ -class Cms implements ValidatorInterface -{ - use PathTrait; - /** - * @var GetPageByIdentifier - */ - protected $getPageById; - - /** - * Cms constructor. - * @param GetPageByIdentifier $getPageByIdentifier - */ - public function __construct(GetPageByIdentifier $getPageByIdentifier) - { - $this->getPageById = $getPageByIdentifier; - } - - /** - * @inheritdoc - */ - public function validateRequest(RequestInterface $request): bool - { - $cmsPageId = $this->getPathFrontName($request); - - try { - $this->getPageById->execute($cmsPageId, 0); - } catch (NoSuchEntityException $e) { - return false; - } - return true; - } -} diff --git a/src/Validator/Product.php b/src/Validator/Product.php deleted file mode 100644 index 91a08a0..0000000 --- a/src/Validator/Product.php +++ /dev/null @@ -1,126 +0,0 @@ - - * @copyright Copyright (c) 2019 Scandiweb, Ltd (http://scandiweb.com) - * @license OSL-3.0 - */ - -namespace ScandiPWA\Router\Validator; - -use Magento\Catalog\Model\Product as ProductModel; -use Magento\ConfigurableProduct\Model\Product\Type\Configurable; -use Magento\Framework\App\RequestInterface; -use ScandiPWA\Router\PathTrait; -use ScandiPWA\Router\ValidatorInterface; -use \Magento\Catalog\Model\ResourceModel\Product\Collection; - -/** - * Class Product - * @package ScandiPWA\Router\Validator - */ -class Product implements ValidatorInterface -{ - use PathTrait; - - /** @var Collection */ - protected $productCollection; - /** - * @var Configurable - */ - private $configurable; - /** - * @var ProductModel - */ - private $product; - - /** - * Product constructor. - * @param Collection $productCollection - * @param Configurable $configurable - * @param ProductModel $product - */ - public function __construct( - Collection $productCollection, - Configurable $configurable, - ProductModel $product - ) { - $this->productCollection = $productCollection; - $this->configurable = $configurable; - $this->product = $product; - } - - /** - * @inheritdoc - */ - public function validateRequest(RequestInterface $request): bool - { - $urlKey = $this->getPathFrontName($request); - $parameters = $request->getParams(); - - $productCollection = $this->productCollection->clear(); - $productCollection->addAttributeToFilter('url_key', $urlKey); - $ids = $productCollection->getAllIds(); - $productId = reset($ids); - - if (!$productId) { - return false; - } - - $typeIds = $productCollection->getProductTypeIds(); - $type = reset($typeIds); - - switch ($type) { - case Configurable::TYPE_CODE: - return $this->checkConfigurableProduct($productId, $parameters); - default: - return true; - } - } - - /** - * Checks whether configurable product with specified parameters does exist - * @param int $productId - * @param array $parameters - * @return bool - */ - protected function checkConfigurableProduct(int $productId, array $parameters): bool - { - if (!count($parameters)) { - return true; - } - - $product = $this->product->load($productId); - $attributes = $this->configurable->getConfigurableAttributes($product); - $attributeCount = 0; - - // loop through all configurable product attributes - foreach ($attributes as $attribute) { - $attributeCode = $attribute->getProductAttribute()->getAttributeCode(); - - // if configurable attribute code is mentioned URL param - if (array_key_exists($attributeCode, $parameters)) { - $parameterValue = $parameters[$attributeCode]; - unset($parameters[$attributeCode]); - $options = $attribute->getOptions(); - - if (!in_array($parameterValue, array_column($options, 'value_index'), true)) { - return false; - } - - $attributeCount++; - } - } - - if ($attributeCount !== count($attributes)) { // if all configurable attributes are not matched - return false; - } - - if (!empty($parameters)) { // if all parameters were processed - return false; - } - - return true; - } -} diff --git a/src/etc/di.xml b/src/etc/di.xml index b30bcfa..6a6185f 100644 --- a/src/etc/di.xml +++ b/src/etc/di.xml @@ -16,14 +16,12 @@ + type="ScandiPWA\Router\Controller\Customer\RedirectPlugin" + /> - ScandiPWA\Router\Validator\Category - ScandiPWA\Router\Validator\Product - ScandiPWA\Router\Validator\Cms ScandiPWA\Router\Validator\AlwaysPass ScandiPWA\Router\Validator\Wishlist ScandiPWA\Router\Validator\AlwaysPass