-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Changes from all commits
174bb19
d8dc2c1
caf2d57
4a7f2b3
97616ca
0624642
1d9e208
2371eeb
dc9abe5
053d8ea
aef6ec0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
); | ||
} | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
$category = $this->currentCategory->get(); | ||
} catch (NoSuchEntityException $exception) { | ||
return $this->categoryConfig->getDefaultSortField(); | ||
} | ||
|
||
if ($category->getDefaultSortBy()) { | ||
return $category->getDefaultSortBy(); | ||
} | ||
|
||
return $this->categoryConfig->getDefaultSortField(); | ||
} | ||
} |
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); | ||
} | ||
} |
There was a problem hiding this comment.
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