Skip to content

Commit 58a66a2

Browse files
committed
SwiftOtter-SOP-348 Reduce cyclomatic complexity (<10) + fix Static checks
1 parent 5aca993 commit 58a66a2

File tree

1 file changed

+89
-68
lines changed
  • app/code/Magento/Catalog/Controller/Category

1 file changed

+89
-68
lines changed

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

Lines changed: 89 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
* Copyright 2014 Adobe
44
* All Rights Reserved.
55
*/
6+
7+
declare(strict_types=1);
8+
69
namespace Magento\Catalog\Controller\Category;
710

811
use Magento\Catalog\Api\CategoryRepositoryInterface;
@@ -11,8 +14,8 @@
1114
use Magento\Catalog\Model\Category\Attribute\LayoutUpdateManager;
1215
use Magento\Catalog\Model\Design;
1316
use Magento\Catalog\Model\Layer\Resolver;
14-
use Magento\Catalog\Model\Product\ProductList\ToolbarMemorizer;
1517
use Magento\Catalog\Model\Product\ProductList\Toolbar;
18+
use Magento\Catalog\Model\Product\ProductList\ToolbarMemorizer;
1619
use Magento\Catalog\Model\Session;
1720
use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator;
1821
use Magento\Framework\App\Action\Action;
@@ -21,8 +24,10 @@
2124
use Magento\Framework\App\Action\HttpPostActionInterface;
2225
use Magento\Framework\App\ActionInterface;
2326
use Magento\Framework\App\ObjectManager;
27+
use Magento\Framework\Controller\Result\Forward;
2428
use Magento\Framework\Controller\Result\ForwardFactory;
25-
use Magento\Framework\Controller\ResultFactory;
29+
use Magento\Framework\Controller\Result\Redirect;
30+
use Magento\Framework\Controller\ResultInterface;
2631
use Magento\Framework\DataObject;
2732
use Magento\Framework\Exception\LocalizedException;
2833
use Magento\Framework\Exception\NoSuchEntityException;
@@ -107,8 +112,6 @@ class View extends Action implements HttpGetActionInterface, HttpPostActionInter
107112
private $logger;
108113

109114
/**
110-
* Constructor
111-
*
112115
* @param Context $context
113116
* @param Design $catalogDesign
114117
* @param Session $catalogSession
@@ -121,8 +124,8 @@ class View extends Action implements HttpGetActionInterface, HttpPostActionInter
121124
* @param CategoryRepositoryInterface $categoryRepository
122125
* @param ToolbarMemorizer|null $toolbarMemorizer
123126
* @param LayoutUpdateManager|null $layoutUpdateManager
124-
* @param CategoryHelper $categoryHelper
125-
* @param LoggerInterface $logger
127+
* @param CategoryHelper|null $categoryHelper
128+
* @param LoggerInterface|null $logger
126129
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
127130
*/
128131
public function __construct(
@@ -161,9 +164,7 @@ public function __construct(
161164
}
162165

163166
/**
164-
* Initialize requested category object
165-
*
166-
* @return Category|bool
167+
* @return Category|false
167168
*/
168169
protected function _initCategory()
169170
{
@@ -189,109 +190,132 @@ protected function _initCategory()
189190
['category' => $category, 'controller_action' => $this]
190191
);
191192
} catch (LocalizedException $e) {
192-
$this->logger->critical($e);
193+
$this->logger->critical((string)$e);
193194
return false;
194195
}
195196

196197
return $category;
197198
}
198199

199200
/**
200-
* Category view action
201-
*
201+
* @return ResultInterface
202202
* @throws NoSuchEntityException
203203
*/
204204
public function execute()
205205
{
206-
$result = null;
207-
208206
if ($this->_request->getParam(ActionInterface::PARAM_NAME_URL_ENCODED)) {
209207
//phpcs:ignore Magento2.Legacy.ObsoleteResponse
210208
return $this->resultRedirectFactory->create()->setUrl($this->_redirect->getRedirectUrl());
211209
}
212210

213211
$category = $this->_initCategory();
214-
if ($category) {
215-
// Negative ?p= value is not supported, redirect to a base version of category page.
216-
if ($this->_request->getParam(Toolbar::PAGE_PARM_NAME) < 0) {
217-
return $this->resultRedirectFactory->create()
218-
->setHttpResponseCode(301)
219-
->setUrl($category->getUrl());
220-
}
212+
if (!$category) {
213+
return $this->resultForwardFactory->create()->forward('noroute');
214+
}
221215

222-
$this->layerResolver->create(Resolver::CATALOG_LAYER_CATEGORY);
223-
$settings = $this->_catalogDesign->getDesignSettings($category);
216+
$pageRedirect = $this->handlePageRedirect($category);
217+
if ($pageRedirect) {
218+
return $pageRedirect;
219+
}
224220

225-
// apply custom design
226-
if ($settings->getCustomDesign()) {
227-
$this->_catalogDesign->applyCustomDesign($settings->getCustomDesign());
228-
}
221+
$page = $this->preparePage($category);
229222

230-
$this->_catalogSession->setLastViewedCategoryId($category->getId());
223+
if ($this->shouldRedirectOnToolbarAction()) {
224+
$this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
225+
}
231226

232-
$page = $this->resultPageFactory->create();
233-
// apply custom layout (page) template once the blocks are generated
234-
if ($settings->getPageLayout()) {
235-
$page->getConfig()->setPageLayout($settings->getPageLayout());
236-
}
227+
return $page;
228+
}
237229

238-
$pageType = $this->getPageType($category);
230+
/**
231+
* @param Category $category
232+
* @return Redirect|null
233+
*/
234+
private function handlePageRedirect(Category $category): ?Redirect
235+
{
236+
if ($this->_request->getParam(Toolbar::PAGE_PARM_NAME) < 0) {
237+
return $this->resultRedirectFactory->create()
238+
->setHttpResponseCode(301)
239+
->setUrl($category->getUrl());
240+
}
239241

240-
if (!$category->hasChildren()) {
241-
// Two levels removed from parent. Need to add default page type.
242-
$parentPageType = strtok($pageType, '_');
243-
$page->addPageLayoutHandles(['type' => $parentPageType], null, false);
244-
}
245-
$page->addPageLayoutHandles(['type' => $pageType], null, false);
246-
$categoryDisplayMode = is_string($category->getDisplayMode()) ?
247-
strtolower($category->getDisplayMode()) : '';
248-
$page->addPageLayoutHandles(['displaymode' => $categoryDisplayMode], null, false);
249-
$page->addPageLayoutHandles(['id' => $category->getId()]);
242+
return null;
243+
}
250244

251-
// apply custom layout update once layout is loaded
252-
$this->applyLayoutUpdates($page, $settings);
245+
/**
246+
* @param Category $category
247+
* @return Page
248+
*/
249+
private function preparePage(Category $category): Page
250+
{
251+
$this->layerResolver->create(Resolver::CATALOG_LAYER_CATEGORY);
252+
$settings = $this->_catalogDesign->getDesignSettings($category);
253+
254+
if ($settings->getCustomDesign()) {
255+
$this->_catalogDesign->applyCustomDesign($settings->getCustomDesign());
256+
}
253257

254-
$page->getConfig()->addBodyClass('page-products')
255-
->addBodyClass('categorypath-' . $this->categoryUrlPathGenerator->getUrlPath($category))
256-
->addBodyClass('category-' . $category->getUrlKey());
258+
$this->_catalogSession->setLastViewedCategoryId($category->getId());
257259

258-
if ($this->shouldRedirectOnToolbarAction()) {
259-
$this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
260-
}
261-
return $page;
262-
} elseif (!$this->getResponse()->isRedirect()) {
263-
$result = $this->resultForwardFactory->create()->forward('noroute');
260+
$page = $this->resultPageFactory->create();
261+
if ($settings->getPageLayout()) {
262+
$page->getConfig()->setPageLayout($settings->getPageLayout());
264263
}
265-
return $result;
264+
265+
$this->addPageLayoutHandles($page, $category);
266+
$this->applyLayoutUpdates($page, $settings);
267+
268+
$page->getConfig()->addBodyClass('page-products')
269+
->addBodyClass('categorypath-' . $this->categoryUrlPathGenerator->getUrlPath($category))
270+
->addBodyClass('category-' . $category->getUrlKey());
271+
272+
return $page;
273+
}
274+
275+
/**
276+
* @param Page $page
277+
* @param Category $category
278+
* @return void
279+
*/
280+
private function addPageLayoutHandles(Page $page, Category $category): void
281+
{
282+
$pageType = $this->getPageType($category);
283+
284+
if (!$category->hasChildren()) {
285+
$parentPageType = strtok($pageType, '_');
286+
$page->addPageLayoutHandles(['type' => $parentPageType], null, false);
287+
}
288+
$page->addPageLayoutHandles(['type' => $pageType], null, false);
289+
290+
$categoryDisplayMode = is_string($category->getDisplayMode()) ?
291+
strtolower($category->getDisplayMode()) : '';
292+
$page->addPageLayoutHandles(['displaymode' => $categoryDisplayMode], null, false);
293+
$page->addPageLayoutHandles(['id' => $category->getId()]);
266294
}
267295

268296
/**
269-
* Get page type based on category
270-
*
271297
* @param Category $category
272298
* @return string
273299
*/
274-
private function getPageType(Category $category) : string
300+
private function getPageType(Category $category): string
275301
{
276302
$hasChildren = $category->hasChildren();
277303
if ($category->getIsAnchor()) {
278-
return $hasChildren ? 'layered' : 'layered_without_children';
304+
return $hasChildren ? 'layered' : 'layered_without_children';
279305
}
280306

281307
return $hasChildren ? 'default' : 'default_without_children';
282308
}
283309

284310
/**
285-
* Apply custom layout updates
286-
*
287311
* @param Page $page
288312
* @param DataObject $settings
289313
* @return void
290314
*/
291315
private function applyLayoutUpdates(
292316
Page $page,
293317
DataObject $settings
294-
) {
318+
): void {
295319
$layoutUpdates = $settings->getLayoutUpdates();
296320
if ($layoutUpdates && is_array($layoutUpdates)) {
297321
foreach ($layoutUpdates as $layoutUpdate) {
@@ -300,26 +324,23 @@ private function applyLayoutUpdates(
300324
}
301325
}
302326

303-
//Selected files
304327
if ($settings->getPageLayoutHandles()) {
305328
$page->addPageLayoutHandles($settings->getPageLayoutHandles());
306329
}
307330
}
308331

309332
/**
310-
* Checks for toolbar actions
311-
*
312333
* @return bool
313334
*/
314335
private function shouldRedirectOnToolbarAction(): bool
315336
{
316337
$params = $this->getRequest()->getParams();
317338

318-
return $this->toolbarMemorizer->isMemorizingAllowed() && empty(array_intersect([
339+
return $this->toolbarMemorizer->isMemorizingAllowed() && !empty(array_intersect([
319340
Toolbar::ORDER_PARAM_NAME,
320341
Toolbar::DIRECTION_PARAM_NAME,
321342
Toolbar::MODE_PARAM_NAME,
322343
Toolbar::LIMIT_PARAM_NAME
323-
], array_keys($params))) === false;
344+
], array_keys($params)));
324345
}
325346
}

0 commit comments

Comments
 (0)