From 393d1f33a36c9641a40a3e583b225738b6f682d0 Mon Sep 17 00:00:00 2001 From: Anshu Mishra Date: Mon, 18 Jun 2018 21:54:59 +0530 Subject: [PATCH] Admin controller product set save refactor --- .../Controller/Adminhtml/Product/Set/Save.php | 49 ++++++++++++++----- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/app/code/Magento/Catalog/Controller/Adminhtml/Product/Set/Save.php b/app/code/Magento/Catalog/Controller/Adminhtml/Product/Set/Save.php index 00a836309e58e..dfddcf7e92b97 100644 --- a/app/code/Magento/Catalog/Controller/Adminhtml/Product/Set/Save.php +++ b/app/code/Magento/Catalog/Controller/Adminhtml/Product/Set/Save.php @@ -6,6 +6,11 @@ */ namespace Magento\Catalog\Controller\Adminhtml\Product\Set; +use Magento\Framework\App\ObjectManager; + +/** + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ class Save extends \Magento\Catalog\Controller\Adminhtml\Product\Set { /** @@ -17,22 +22,49 @@ class Save extends \Magento\Catalog\Controller\Adminhtml\Product\Set * @var \Magento\Framework\Controller\Result\JsonFactory */ protected $resultJsonFactory; - + + /* + * @var \Magento\Eav\Model\Entity\Attribute\SetFactory + */ + private $attributeSetFactory; + + /* + * @var \Magento\Framework\Filter\FilterManager + */ + private $filterManager; + + /* + * @var \Magento\Framework\Json\Helper\Data + */ + private $jsonHelper; + /** * @param \Magento\Backend\App\Action\Context $context * @param \Magento\Framework\Registry $coreRegistry * @param \Magento\Framework\View\LayoutFactory $layoutFactory * @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory + * @param \Magento\Eav\Model\Entity\Attribute\SetFactory $attributeSetFactory + * @param \Magento\Framework\Filter\FilterManager $filterManager + * @param \Magento\Framework\Json\Helper\Data $jsonHelper */ public function __construct( \Magento\Backend\App\Action\Context $context, \Magento\Framework\Registry $coreRegistry, \Magento\Framework\View\LayoutFactory $layoutFactory, - \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory + \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory, + \Magento\Eav\Model\Entity\Attribute\SetFactory $attributeSetFactory = null, + \Magento\Framework\Filter\FilterManager $filterManager = null, + \Magento\Framework\Json\Helper\Data $jsonHelper = null ) { parent::__construct($context, $coreRegistry); $this->layoutFactory = $layoutFactory; $this->resultJsonFactory = $resultJsonFactory; + $this->attributeSetFactory = $attributeSetFactory ?: ObjectManager::getInstance() + ->get(\Magento\Eav\Model\Entity\Attribute\SetFactory::class); + $this->filterManager = $filterManager ?: ObjectManager::getInstance() + ->get(\Magento\Framework\Filter\FilterManager::class); + $this->jsonHelper = $jsonHelper ?: ObjectManager::getInstance() + ->get(\Magento\Framework\Json\Helper\Data::class); } /** @@ -65,16 +97,12 @@ public function execute() $isNewSet = $this->getRequest()->getParam('gotoEdit', false) == '1'; /* @var $model \Magento\Eav\Model\Entity\Attribute\Set */ - $model = $this->_objectManager->create(\Magento\Eav\Model\Entity\Attribute\Set::class) - ->setEntityTypeId($entityTypeId); - - /** @var $filterManager \Magento\Framework\Filter\FilterManager */ - $filterManager = $this->_objectManager->get(\Magento\Framework\Filter\FilterManager::class); + $model = $this->attributeSetFactory->create()->setEntityTypeId($entityTypeId); try { if ($isNewSet) { //filter html tags - $name = $filterManager->stripTags($this->getRequest()->getParam('attribute_set_name')); + $name = $this->filterManager->stripTags($this->getRequest()->getParam('attribute_set_name')); $model->setAttributeSetName(trim($name)); } else { if ($attributeSetId) { @@ -85,11 +113,10 @@ public function execute() __('This attribute set no longer exists.') ); } - $data = $this->_objectManager->get(\Magento\Framework\Json\Helper\Data::class) - ->jsonDecode($this->getRequest()->getPost('data')); + $data = $this->jsonHelper->jsonDecode($this->getRequest()->getPost('data')); //filter html tags - $data['attribute_set_name'] = $filterManager->stripTags($data['attribute_set_name']); + $data['attribute_set_name'] = $this->filterManager->stripTags($data['attribute_set_name']); $model->organizeData($data); }