Skip to content

Catalog ProductList helper has coupling to Registry #27119 #27648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions app/code/Magento/Catalog/Block/Product/ProductList/Toolbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Magento\Catalog\Model\Product\ProductList\Toolbar as ToolbarModel;
use Magento\Catalog\Model\Product\ProductList\ToolbarMemorizer;
use Magento\Framework\App\ObjectManager;
use Magento\Catalog\Model\Category\Toolbar\Config as ToolbarConfig;

/**
* Product list toolbar
Expand Down Expand Up @@ -138,6 +139,11 @@ class Toolbar extends \Magento\Framework\View\Element\Template
*/
private $formKey;

/**
* @var ToolbarConfig
*/
private $toolbarConfig;

/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Catalog\Model\Session $catalogSession
Expand All @@ -150,6 +156,7 @@ class Toolbar extends \Magento\Framework\View\Element\Template
* @param ToolbarMemorizer|null $toolbarMemorizer
* @param \Magento\Framework\App\Http\Context|null $httpContext
* @param \Magento\Framework\Data\Form\FormKey|null $formKey
* @param ToolbarConfig|null $toolbarConfig
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
Expand All @@ -164,7 +171,8 @@ public function __construct(
array $data = [],
ToolbarMemorizer $toolbarMemorizer = null,
\Magento\Framework\App\Http\Context $httpContext = null,
\Magento\Framework\Data\Form\FormKey $formKey = null
\Magento\Framework\Data\Form\FormKey $formKey = null,
ToolbarConfig $toolbarConfig = null
) {
$this->_catalogSession = $catalogSession;
$this->_catalogConfig = $catalogConfig;
Expand All @@ -181,6 +189,10 @@ public function __construct(
$this->formKey = $formKey ?: ObjectManager::getInstance()->get(
\Magento\Framework\Data\Form\FormKey::class
);
$this->toolbarConfig = $toolbarConfig ?: ObjectManager::getInstance()->get(
ToolbarConfig::class
);

parent::__construct($context, $data);
}

Expand Down Expand Up @@ -756,15 +768,16 @@ public function getWidgetOptionsJson(array $customOptions = [])
}

/**
* Get order field
* Returns an order field for a collection.
*
* @return null|string
*/
protected function getOrderField()
{
if ($this->_orderField === null) {
$this->_orderField = $this->_productListHelper->getDefaultSortField();
$this->_orderField = $this->toolbarConfig->getOrderField();
}

return $this->_orderField;
}

Expand Down
29 changes: 11 additions & 18 deletions app/code/Magento/Catalog/Helper/Product/ProductList.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@

namespace Magento\Catalog\Helper\Product;

use Magento\Catalog\Model\Config;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Registry;
use Magento\Store\Model\ScopeInterface;
use Magento\Catalog\Model\Category\Toolbar\Config as ToolbarConfig;

/**
* Returns data for toolbars of Sorting and Pagination
Expand All @@ -32,11 +30,6 @@ class ProductList
*/
protected $scopeConfig;

/**
* @var Registry
*/
private $coreRegistry;

/**
* Default limits per page
*
Expand All @@ -45,15 +38,21 @@ class ProductList
protected $_defaultAvailableLimit = [10 => 10, 20 => 20, 50 => 50];

/**
* @var ToolbarConfig
*/
private $toolbarConfig;

/**
* ProductList constructor.
* @param ScopeConfigInterface $scopeConfig
* @param Registry $coreRegistry
* @param ToolbarConfig $toolbarConfig
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
Registry $coreRegistry = null
ToolbarConfig $toolbarConfig
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class marked as @api, so we can't just replace existing argument with a new one. Please introduce new dependecy as optional argument

) {
$this->scopeConfig = $scopeConfig;
$this->coreRegistry = $coreRegistry ?? ObjectManager::getInstance()->get(Registry::class);
$this->toolbarConfig = $toolbarConfig;
}

/**
Expand Down Expand Up @@ -100,17 +99,11 @@ public function getDefaultViewMode($options = [])
/**
* Get default sort field
*
* @FIXME Helper should be context-independent
* @return null|string
*/
public function getDefaultSortField()
{
$currentCategory = $this->coreRegistry->registry('current_category');
if ($currentCategory) {
return $currentCategory->getDefaultSortBy();
}

return $this->scopeConfig->getValue(Config::XML_PATH_LIST_DEFAULT_SORT_BY, ScopeInterface::SCOPE_STORE);
return $this->toolbarConfig->getOrderField();
}

/**
Expand Down
44 changes: 44 additions & 0 deletions app/code/Magento/Catalog/Model/Category/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Model\Category;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Catalog\Model\Config as CatalogConfig;
use Magento\Store\Model\ScopeInterface;

class Config
{
/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* Config constructor.
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(ScopeConfigInterface $scopeConfig)
{
$this->scopeConfig = $scopeConfig;
}

/**
* Returns a sort field from the configuration
*
* @param string|int|null $scopeCode
* @return string
*/
public function getDefaultSortField($scopeCode = null): string
{
return (string) $this->scopeConfig->getValue(
CatalogConfig::XML_PATH_LIST_DEFAULT_SORT_BY,
ScopeInterface::SCOPE_STORE,
$scopeCode
);
}
}
58 changes: 58 additions & 0 deletions app/code/Magento/Catalog/Model/Category/Toolbar/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Model\Category\Toolbar;

use Magento\Catalog\Model\Category\Config as CategoryConfig;
use Magento\Catalog\Model\CurrentCategory;
use Magento\Framework\Exception\NoSuchEntityException;

class Config
{
/**
* @var CurrentCategory
*/
private $currentCategory;

/**
* @var CategoryConfig
*/
private $categoryConfig;

/**
* Config constructor.
* @param CurrentCategory $currentCategory
* @param CategoryConfig $categoryConfig
*/
public function __construct(
CurrentCategory $currentCategory,
CategoryConfig $categoryConfig
) {
$this->currentCategory = $currentCategory;
$this->categoryConfig = $categoryConfig;
}

/**
* Returns a category default_sort_by attribute, or default sort field from the configuration.
*
* @return string
*/
public function getOrderField(): string
Copy link

@sshymko sshymko Jul 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking how to improve this implementation to be simpler and easier to follow. Here's an equivalent version that avoids duplication of the default behavior.

        try {
            $category = $this->currentCategory->get();
            if ($category->getDefaultSortBy()) {
                return $category->getDefaultSortBy();
            }
        } catch (NoSuchEntityException $exception) {
            // Fall back to the default configuration
        }
        return $this->categoryConfig->getDefaultSortField();

I think, in most cases it's better to keep the "positive" logic inside try..catch as it guarantees that all its preconditions are met (variable $category is defined in this case). Such code is also easier to follow as the sequential logic is grouped together.

{
try {
$category = $this->currentCategory->get();
} catch (NoSuchEntityException $exception) {
return $this->categoryConfig->getDefaultSortField();
}

if ($category->getDefaultSortBy()) {
return $category->getDefaultSortBy();
}

return $this->categoryConfig->getDefaultSortField();
}
}
52 changes: 52 additions & 0 deletions app/code/Magento/Catalog/Model/CurrentCategory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Model;

use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Exception\NoSuchEntityException;

class CurrentCategory
{
/**
* @var RequestInterface
*/
private $request;

/**
* @var CategoryRepositoryInterface
*/
private $repository;

/**
* CurrentCategory constructor.
* @param RequestInterface $request
* @param CategoryRepositoryInterface $categoryRepository
*/
public function __construct(
RequestInterface $request,
CategoryRepositoryInterface $categoryRepository
) {
$this->request = $request;
$this->repository = $categoryRepository;
}

/**
* Returns a category based on an id request parameter.
*
* @param int|null $storeId
*
* @return CategoryInterface|Category
* @throws NoSuchEntityException
*/
public function get($storeId = null): CategoryInterface
{
return $this->repository->get((int) $this->request->getParam('id'), $storeId);
}
}
Loading