Skip to content

Move additional dependencies from private getters to constructor - Magento_Catalog #26411

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@

namespace Magento\Catalog\Block\Adminhtml\Product\Attribute\Edit\Tab;

use Magento\Backend\Block\Template\Context;
use Magento\Backend\Block\Widget\Form\Generic;
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
use Magento\Config\Model\Config\Source\Yesno;
use Magento\Eav\Block\Adminhtml\Attribute\PropertyLocker;
use Magento\Eav\Helper\Data;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Data\FormFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Registry;
use Magento\Framework\Stdlib\DateTime;

/**
Expand Down Expand Up @@ -47,26 +51,29 @@ class Advanced extends Generic
private $propertyLocker;

/**
* @param \Magento\Backend\Block\Template\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\Data\FormFactory $formFactory
* @param Context $context
* @param Registry $registry
* @param FormFactory $formFactory
* @param Yesno $yesNo
* @param Data $eavData
* @param array $disableScopeChangeList
* @param array $data
* @param PropertyLocker|null $propertyLocker
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Data\FormFactory $formFactory,
Context $context,
Registry $registry,
FormFactory $formFactory,
Yesno $yesNo,
Data $eavData,
array $disableScopeChangeList = ['sku'],
array $data = []
array $data = [],
?PropertyLocker $propertyLocker = null
) {
$this->_yesNo = $yesNo;
$this->_eavData = $eavData;
$this->disableScopeChangeList = $disableScopeChangeList;
$this->propertyLocker = $propertyLocker ?? ObjectManager::getInstance()->get(PropertyLocker::class);
parent::__construct($context, $registry, $formFactory, $data);
}

Expand Down Expand Up @@ -238,14 +245,14 @@ protected function _prepareForm()
}

$scopes = [
\Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE => __('Store View'),
\Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_WEBSITE => __('Website'),
\Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL => __('Global'),
ScopedAttributeInterface::SCOPE_STORE => __('Store View'),
ScopedAttributeInterface::SCOPE_WEBSITE => __('Website'),
ScopedAttributeInterface::SCOPE_GLOBAL => __('Global'),
];

if ($attributeObject->getAttributeCode() == 'status' || $attributeObject->getAttributeCode() == 'tax_class_id'
) {
unset($scopes[\Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE]);
unset($scopes[ScopedAttributeInterface::SCOPE_STORE]);
}

$fieldset->addField(
Expand All @@ -266,7 +273,7 @@ protected function _prepareForm()
$form->getElement('is_global')->setDisabled(1);
}
$this->setForm($form);
$this->getPropertyLocker()->lock($form);
$this->propertyLocker->lock($form);
return $this;
}

Expand All @@ -291,19 +298,6 @@ private function getAttributeObject()
return $this->_coreRegistry->registry('entity_attribute');
}

/**
* Get property locker
*
* @return PropertyLocker
*/
private function getPropertyLocker()
{
if (null === $this->propertyLocker) {
$this->propertyLocker = ObjectManager::getInstance()->get(PropertyLocker::class);
}
return $this->propertyLocker;
}

/**
* Get localized date default value
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Controller\Adminhtml\Product\Initialization;

Expand Down Expand Up @@ -132,6 +133,7 @@ class Helper
* @param FormatInterface|null $localeFormat
* @param ProductAuthorization|null $productAuthorization
* @param DateTimeFilter|null $dateTimeFilter
* @param LinkResolver|null $linkResolver
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -148,7 +150,8 @@ public function __construct(
AttributeFilter $attributeFilter = null,
FormatInterface $localeFormat = null,
?ProductAuthorization $productAuthorization = null,
?DateTimeFilter $dateTimeFilter = null
?DateTimeFilter $dateTimeFilter = null,
?LinkResolver $linkResolver = null
) {
$this->request = $request;
$this->storeManager = $storeManager;
Expand All @@ -157,15 +160,18 @@ public function __construct(
$this->jsHelper = $jsHelper;
$this->dateFilter = $dateFilter;

$objectManager = ObjectManager::getInstance();
$this->customOptionFactory = $customOptionFactory ?: $objectManager->get(CustomOptionFactory::class);
$this->productLinkFactory = $productLinkFactory ?: $objectManager->get(ProductLinkFactory::class);
$this->productRepository = $productRepository ?: $objectManager->get(ProductRepositoryInterface::class);
$this->linkTypeProvider = $linkTypeProvider ?: $objectManager->get(LinkTypeProvider::class);
$this->attributeFilter = $attributeFilter ?: $objectManager->get(AttributeFilter::class);
$this->localeFormat = $localeFormat ?: $objectManager->get(FormatInterface::class);
$this->productAuthorization = $productAuthorization ?? $objectManager->get(ProductAuthorization::class);
$this->dateTimeFilter = $dateTimeFilter ?? $objectManager->get(DateTimeFilter::class);
$this->customOptionFactory = $customOptionFactory ?:
ObjectManager::getInstance()->get(CustomOptionFactory::class);
$this->productLinkFactory = $productLinkFactory ?: ObjectManager::getInstance()->get(ProductLinkFactory::class);
$this->productRepository = $productRepository ?:
ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
$this->linkTypeProvider = $linkTypeProvider ?: ObjectManager::getInstance()->get(LinkTypeProvider::class);
$this->attributeFilter = $attributeFilter ?: ObjectManager::getInstance()->get(AttributeFilter::class);
$this->localeFormat = $localeFormat ?: ObjectManager::getInstance()->get(FormatInterface::class);
$this->productAuthorization = $productAuthorization ??
ObjectManager::getInstance()->get(ProductAuthorization::class);
$this->dateTimeFilter = $dateTimeFilter ?? ObjectManager::getInstance()->get(DateTimeFilter::class);
$this->linkResolver = $linkResolver ?? ObjectManager::getInstance()->get(LinkResolver::class);
}

/**
Expand Down Expand Up @@ -271,7 +277,7 @@ public function initialize(Product $product)
*/
protected function setProductLinks(Product $product)
{
$links = $this->getLinkResolver()->getLinks();
$links = $this->linkResolver->getLinks();

$product->setProductLinks([]);

Expand Down Expand Up @@ -396,21 +402,6 @@ private function overwriteValue($optionId, $option, $overwriteOptions)
return $option;
}

/**
* Get link resolver instance
*
* @return LinkResolver
* @deprecated 101.0.0
*/
private function getLinkResolver()
{
if (!is_object($this->linkResolver)) {
$this->linkResolver = ObjectManager::getInstance()->get(LinkResolver::class);
}

return $this->linkResolver;
}

/**
* Remove ids of non selected websites from $websiteIds array and return filtered data
*
Expand Down
96 changes: 43 additions & 53 deletions app/code/Magento/Catalog/Controller/Adminhtml/Product/Validate.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Controller\Adminhtml\Product;

use Magento\Backend\App\Action\Context;
use Magento\Catalog\Model\Product\Validator;
use Magento\Catalog\Model\ProductFactory;
use Magento\Eav\Model\Entity\Attribute\Exception;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
use Magento\Backend\App\Action;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Catalog\Controller\Adminhtml\Product;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\DataObject;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Stdlib\DateTime\Filter\Date;
use Magento\Framework\View\LayoutFactory;
use Magento\Store\Model\StoreManagerInterface;

/**
Expand All @@ -21,29 +30,29 @@
class Validate extends Product implements HttpPostActionInterface, HttpGetActionInterface
{
/**
* @var \Magento\Framework\Stdlib\DateTime\Filter\Date
* @var Date
*
* @deprecated 101.0.0
*/
protected $_dateFilter;

/**
* @var \Magento\Catalog\Model\Product\Validator
* @var Validator
*/
protected $productValidator;

/**
* @var \Magento\Framework\Controller\Result\JsonFactory
* @var JsonFactory
*/
protected $resultJsonFactory;

/**
* @var \Magento\Framework\View\LayoutFactory
* @var LayoutFactory
*/
protected $layoutFactory;

/**
* @var \Magento\Catalog\Model\ProductFactory
* @var ProductFactory
*/
protected $productFactory;

Expand All @@ -58,41 +67,47 @@ class Validate extends Product implements HttpPostActionInterface, HttpGetAction
private $storeManager;

/**
* @param Action\Context $context
* @param Context $context
* @param Builder $productBuilder
* @param \Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter
* @param \Magento\Catalog\Model\Product\Validator $productValidator
* @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
* @param \Magento\Framework\View\LayoutFactory $layoutFactory
* @param \Magento\Catalog\Model\ProductFactory $productFactory
* @param Date $dateFilter
* @param Validator $productValidator
* @param JsonFactory $resultJsonFactory
* @param LayoutFactory $layoutFactory
* @param ProductFactory $productFactory
* @param StoreManagerInterface $storeManager
* @param Initialization\Helper $initializationHelper
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
Context $context,
Product\Builder $productBuilder,
\Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter,
\Magento\Catalog\Model\Product\Validator $productValidator,
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
\Magento\Framework\View\LayoutFactory $layoutFactory,
\Magento\Catalog\Model\ProductFactory $productFactory
Date $dateFilter,
Validator $productValidator,
JsonFactory $resultJsonFactory,
LayoutFactory $layoutFactory,
ProductFactory $productFactory,
StoreManagerInterface $storeManager,
Initialization\Helper $initializationHelper
) {
$this->_dateFilter = $dateFilter;
$this->productValidator = $productValidator;
parent::__construct($context, $productBuilder);
$this->resultJsonFactory = $resultJsonFactory;
$this->layoutFactory = $layoutFactory;
$this->productFactory = $productFactory;
$this->storeManager = $storeManager;
$this->initializationHelper = $initializationHelper;
}

/**
* Validate product
*
* @return \Magento\Framework\Controller\Result\Json
* @return Json
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function execute()
{
$response = new \Magento\Framework\DataObject();
$response = new DataObject();
$response->setError(false);

try {
Expand All @@ -102,8 +117,8 @@ public function execute()
$productData['stock_data']['use_config_manage_stock'] = 0;
}
$storeId = $this->getRequest()->getParam('store', 0);
$store = $this->getStoreManager()->getStore($storeId);
$this->getStoreManager()->setCurrentStore($store->getCode());
$store = $this->storeManager->getStore($storeId);
$this->storeManager->setCurrentStore($store->getCode());
/* @var $product \Magento\Catalog\Model\Product */
$product = $this->productFactory->create();
$product->setData('_edit_mode', true);
Expand All @@ -122,7 +137,7 @@ public function execute()
if ($productId) {
$product->load($productId);
}
$product = $this->getInitializationHelper()->initializeFromData($product, $productData);
$product = $this->initializationHelper->initializeFromData($product, $productData);

/* set restrictions for date ranges */
$resource = $product->getResource();
Expand All @@ -131,11 +146,11 @@ public function execute()
$resource->getAttribute('custom_design_from')->setMaxValue($product->getCustomDesignTo());

$this->productValidator->validate($product, $this->getRequest(), $response);
} catch (\Magento\Eav\Model\Entity\Attribute\Exception $e) {
} catch (Exception $e) {
$response->setError(true);
$response->setAttribute($e->getAttributeCode());
$response->setMessages([$e->getMessage()]);
} catch (\Magento\Framework\Exception\LocalizedException $e) {
} catch (LocalizedException $e) {
$response->setError(true);
$response->setMessages([$e->getMessage()]);
} catch (\Exception $e) {
Expand All @@ -148,29 +163,4 @@ public function execute()

return $this->resultJsonFactory->create()->setData($response);
}

/**
* @return StoreManagerInterface
* @deprecated 101.0.0
*/
private function getStoreManager()
{
if (null === $this->storeManager) {
$this->storeManager = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Store\Model\StoreManagerInterface::class);
}
return $this->storeManager;
}

/**
* @return Initialization\Helper
* @deprecated 101.0.0
*/
protected function getInitializationHelper()
{
if (null === $this->initializationHelper) {
$this->initializationHelper = ObjectManager::getInstance()->get(Initialization\Helper::class);
}
return $this->initializationHelper;
}
}
Loading