Skip to content

Commit b9fa1a7

Browse files
authored
ENGCOM-4818: Remove @SuppressWarnings and optimize imports on Category View #22074
2 parents c7ac6df + 67b243a commit b9fa1a7

File tree

2 files changed

+88
-50
lines changed

2 files changed

+88
-50
lines changed

app/code/Magento/Catalog/Controller/Category/View.php

Lines changed: 87 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,28 @@
66
*/
77
namespace Magento\Catalog\Controller\Category;
88

9-
use Magento\Framework\App\Action\HttpPostActionInterface;
10-
use Magento\Framework\App\Action\HttpGetActionInterface;
119
use Magento\Catalog\Api\CategoryRepositoryInterface;
10+
use Magento\Catalog\Model\Category;
11+
use Magento\Catalog\Model\Design;
1212
use Magento\Catalog\Model\Layer\Resolver;
1313
use Magento\Catalog\Model\Product\ProductList\ToolbarMemorizer;
14+
use Magento\Catalog\Model\Session;
15+
use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator;
16+
use Magento\Framework\App\Action\Action;
17+
use Magento\Framework\App\Action\Context;
18+
use Magento\Framework\App\Action\HttpGetActionInterface;
19+
use Magento\Framework\App\Action\HttpPostActionInterface;
20+
use Magento\Framework\App\ActionInterface;
21+
use Magento\Framework\Controller\Result\ForwardFactory;
22+
use Magento\Framework\Controller\ResultInterface;
23+
use Magento\Framework\DataObject;
24+
use Magento\Framework\Exception\LocalizedException;
1425
use Magento\Framework\Exception\NoSuchEntityException;
26+
use Magento\Framework\Registry;
27+
use Magento\Framework\View\Result\Page;
1528
use Magento\Framework\View\Result\PageFactory;
16-
use Magento\Framework\App\Action\Action;
29+
use Magento\Store\Model\StoreManagerInterface;
30+
use Psr\Log\LoggerInterface;
1731

1832
/**
1933
* View a category on storefront. Needs to be accessible by POST because of the store switching.
@@ -25,41 +39,41 @@ class View extends Action implements HttpGetActionInterface, HttpPostActionInter
2539
/**
2640
* Core registry
2741
*
28-
* @var \Magento\Framework\Registry
42+
* @var Registry
2943
*/
3044
protected $_coreRegistry = null;
3145

3246
/**
3347
* Catalog session
3448
*
35-
* @var \Magento\Catalog\Model\Session
49+
* @var Session
3650
*/
3751
protected $_catalogSession;
3852

3953
/**
4054
* Catalog design
4155
*
42-
* @var \Magento\Catalog\Model\Design
56+
* @var Design
4357
*/
4458
protected $_catalogDesign;
4559

4660
/**
47-
* @var \Magento\Store\Model\StoreManagerInterface
61+
* @var StoreManagerInterface
4862
*/
4963
protected $_storeManager;
5064

5165
/**
52-
* @var \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator
66+
* @var CategoryUrlPathGenerator
5367
*/
5468
protected $categoryUrlPathGenerator;
5569

5670
/**
57-
* @var \Magento\Framework\View\Result\PageFactory
71+
* @var PageFactory
5872
*/
5973
protected $resultPageFactory;
6074

6175
/**
62-
* @var \Magento\Framework\Controller\Result\ForwardFactory
76+
* @var ForwardFactory
6377
*/
6478
protected $resultForwardFactory;
6579

@@ -83,28 +97,28 @@ class View extends Action implements HttpGetActionInterface, HttpPostActionInter
8397
/**
8498
* Constructor
8599
*
86-
* @param \Magento\Framework\App\Action\Context $context
87-
* @param \Magento\Catalog\Model\Design $catalogDesign
88-
* @param \Magento\Catalog\Model\Session $catalogSession
89-
* @param \Magento\Framework\Registry $coreRegistry
90-
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
91-
* @param \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator
92-
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
93-
* @param \Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory
100+
* @param Context $context
101+
* @param Design $catalogDesign
102+
* @param Session $catalogSession
103+
* @param Registry $coreRegistry
104+
* @param StoreManagerInterface $storeManager
105+
* @param CategoryUrlPathGenerator $categoryUrlPathGenerator
106+
* @param PageFactory $resultPageFactory
107+
* @param ForwardFactory $resultForwardFactory
94108
* @param Resolver $layerResolver
95109
* @param CategoryRepositoryInterface $categoryRepository
96110
* @param ToolbarMemorizer|null $toolbarMemorizer
97111
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
98112
*/
99113
public function __construct(
100-
\Magento\Framework\App\Action\Context $context,
101-
\Magento\Catalog\Model\Design $catalogDesign,
102-
\Magento\Catalog\Model\Session $catalogSession,
103-
\Magento\Framework\Registry $coreRegistry,
104-
\Magento\Store\Model\StoreManagerInterface $storeManager,
105-
\Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator,
114+
Context $context,
115+
Design $catalogDesign,
116+
Session $catalogSession,
117+
Registry $coreRegistry,
118+
StoreManagerInterface $storeManager,
119+
CategoryUrlPathGenerator $categoryUrlPathGenerator,
106120
PageFactory $resultPageFactory,
107-
\Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory,
121+
ForwardFactory $resultForwardFactory,
108122
Resolver $layerResolver,
109123
CategoryRepositoryInterface $categoryRepository,
110124
ToolbarMemorizer $toolbarMemorizer = null
@@ -125,7 +139,7 @@ public function __construct(
125139
/**
126140
* Initialize requested category object
127141
*
128-
* @return \Magento\Catalog\Model\Category|bool
142+
* @return Category|bool
129143
*/
130144
protected function _initCategory()
131145
{
@@ -150,8 +164,8 @@ protected function _initCategory()
150164
'catalog_controller_category_init_after',
151165
['category' => $category, 'controller_action' => $this]
152166
);
153-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
154-
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
167+
} catch (LocalizedException $e) {
168+
$this->_objectManager->get(LoggerInterface::class)->critical($e);
155169
return false;
156170
}
157171

@@ -161,13 +175,12 @@ protected function _initCategory()
161175
/**
162176
* Category view action
163177
*
164-
* @return \Magento\Framework\Controller\ResultInterface
165-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
166-
* @SuppressWarnings(PHPMD.NPathComplexity)
178+
* @return ResultInterface
179+
* @throws NoSuchEntityException
167180
*/
168181
public function execute()
169182
{
170-
if ($this->_request->getParam(\Magento\Framework\App\ActionInterface::PARAM_NAME_URL_ENCODED)) {
183+
if ($this->_request->getParam(ActionInterface::PARAM_NAME_URL_ENCODED)) {
171184
return $this->resultRedirectFactory->create()->setUrl($this->_redirect->getRedirectUrl());
172185
}
173186
$category = $this->_initCategory();
@@ -188,29 +201,18 @@ public function execute()
188201
$page->getConfig()->setPageLayout($settings->getPageLayout());
189202
}
190203

191-
$hasChildren = $category->hasChildren();
192-
if ($category->getIsAnchor()) {
193-
$type = $hasChildren ? 'layered' : 'layered_without_children';
194-
} else {
195-
$type = $hasChildren ? 'default' : 'default_without_children';
196-
}
204+
$pageType = $this->getPageType($category);
197205

198-
if (!$hasChildren) {
206+
if (!$category->hasChildren()) {
199207
// Two levels removed from parent. Need to add default page type.
200-
$parentType = strtok($type, '_');
201-
$page->addPageLayoutHandles(['type' => $parentType], null, false);
208+
$parentPageType = strtok($pageType, '_');
209+
$page->addPageLayoutHandles(['type' => $parentPageType], null, false);
202210
}
203-
$page->addPageLayoutHandles(['type' => $type], null, false);
211+
$page->addPageLayoutHandles(['type' => $pageType], null, false);
204212
$page->addPageLayoutHandles(['id' => $category->getId()]);
205213

206214
// apply custom layout update once layout is loaded
207-
$layoutUpdates = $settings->getLayoutUpdates();
208-
if ($layoutUpdates && is_array($layoutUpdates)) {
209-
foreach ($layoutUpdates as $layoutUpdate) {
210-
$page->addUpdate($layoutUpdate);
211-
$page->addPageLayoutHandles(['layout_update' => sha1($layoutUpdate)], null, false);
212-
}
213-
}
215+
$this->applyLayoutUpdates($page, $settings);
214216

215217
$page->getConfig()->addBodyClass('page-products')
216218
->addBodyClass('categorypath-' . $this->categoryUrlPathGenerator->getUrlPath($category))
@@ -221,4 +223,40 @@ public function execute()
221223
return $this->resultForwardFactory->create()->forward('noroute');
222224
}
223225
}
226+
227+
/**
228+
* Get page type based on category
229+
*
230+
* @param Category $category
231+
* @return string
232+
*/
233+
private function getPageType(Category $category) : string
234+
{
235+
$hasChildren = $category->hasChildren();
236+
if ($category->getIsAnchor()) {
237+
return $hasChildren ? 'layered' : 'layered_without_children';
238+
}
239+
240+
return $hasChildren ? 'default' : 'default_without_children';
241+
}
242+
243+
/**
244+
* Apply custom layout updates
245+
*
246+
* @param Page $page
247+
* @param DataObject $settings
248+
* @return void
249+
*/
250+
private function applyLayoutUpdates(
251+
Page $page,
252+
DataObject $settings
253+
) {
254+
$layoutUpdates = $settings->getLayoutUpdates();
255+
if ($layoutUpdates && is_array($layoutUpdates)) {
256+
foreach ($layoutUpdates as $layoutUpdate) {
257+
$page->addUpdate($layoutUpdate);
258+
$page->addPageLayoutHandles(['layout_update' => sha1($layoutUpdate)], null, false);
259+
}
260+
}
261+
}
224262
}

app/code/Magento/Catalog/Test/Unit/Controller/Category/ViewTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ protected function setUp()
124124
->disableOriginalConstructor()->getMock();
125125
$this->pageConfig->expects($this->any())->method('addBodyClass')->will($this->returnSelf());
126126

127-
$this->page = $this->getMockBuilder(\Magento\Framework\View\Page::class)
127+
$this->page = $this->getMockBuilder(\Magento\Framework\View\Result\Page::class)
128128
->setMethods(['getConfig', 'initLayout', 'addPageLayoutHandles', 'getLayout', 'addUpdate'])
129129
->disableOriginalConstructor()->getMock();
130130
$this->page->expects($this->any())->method('getConfig')->will($this->returnValue($this->pageConfig));

0 commit comments

Comments
 (0)