diff --git a/app/code/Magento/Bundle/Model/ResourceModel/Indexer/Price.php b/app/code/Magento/Bundle/Model/ResourceModel/Indexer/Price.php index 3d8eac2a982ce..7789f79d907f8 100644 --- a/app/code/Magento/Bundle/Model/ResourceModel/Indexer/Price.php +++ b/app/code/Magento/Bundle/Model/ResourceModel/Indexer/Price.php @@ -157,7 +157,7 @@ protected function _prepareBundlePriceByType($priceType, $entityIds = null) '=?', \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED ); - $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(); + $linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(); $this->_addAttributeToSelect($select, 'status', "e.$linkField", 'cs.store_id', $statusCond, true); if ($this->moduleManager->isEnabled('Magento_Tax')) { $taxClassId = $this->_addAttributeToSelect($select, 'tax_class_id', "e.$linkField", 'cs.store_id'); @@ -404,7 +404,7 @@ protected function _calculateBundleSelectionPrice($priceType) ); } - $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(); + $linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(); $select = $connection->select()->from( ['i' => $this->_getBundlePriceTable()], ['entity_id', 'customer_group_id', 'website_id'] @@ -484,7 +484,7 @@ protected function _prepareBundlePrice($entityIds = null) protected function _prepareTierPriceIndex($entityIds = null) { $connection = $this->getConnection(); - $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(); + $linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(); // remove index by bundle products $select = $connection->select()->from( ['i' => $this->_getTierPriceIndexTable()], diff --git a/app/code/Magento/Bundle/Model/ResourceModel/Indexer/Stock.php b/app/code/Magento/Bundle/Model/ResourceModel/Indexer/Stock.php index a7191b8788074..0013ba1f17f1b 100644 --- a/app/code/Magento/Bundle/Model/ResourceModel/Indexer/Stock.php +++ b/app/code/Magento/Bundle/Model/ResourceModel/Indexer/Stock.php @@ -47,7 +47,7 @@ protected function _getBundleOptionTable() protected function _prepareBundleOptionStockData($entityIds = null, $usePrimaryTable = false) { $this->_cleanBundleOptionStockData(); - $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(); + $linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(); $idxTable = $usePrimaryTable ? $this->getMainTable() : $this->getIdxTable(); $connection = $this->getConnection(); $select = $connection->select()->from( @@ -118,7 +118,7 @@ protected function _prepareBundleOptionStockData($entityIds = null, $usePrimaryT protected function _getStockStatusSelect($entityIds = null, $usePrimaryTable = false) { $this->_prepareBundleOptionStockData($entityIds, $usePrimaryTable); - $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(); + $linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(); $connection = $this->getConnection(); $select = $connection->select()->from( ['e' => $this->getTable('catalog_product_entity')], diff --git a/app/code/Magento/Catalog/Controller/Adminhtml/Category/Edit.php b/app/code/Magento/Catalog/Controller/Adminhtml/Category/Edit.php index 080735b6d1395..3f81b2bbd0ea1 100644 --- a/app/code/Magento/Catalog/Controller/Adminhtml/Category/Edit.php +++ b/app/code/Magento/Catalog/Controller/Adminhtml/Category/Edit.php @@ -28,18 +28,15 @@ class Edit extends \Magento\Catalog\Controller\Adminhtml\Category * @param \Magento\Backend\App\Action\Context $context * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory * @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory - * @param \Magento\Store\Model\StoreManagerInterface $storeManager */ public function __construct( \Magento\Backend\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory, - \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory, - \Magento\Store\Model\StoreManagerInterface $storeManager + \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory ) { parent::__construct($context); $this->resultPageFactory = $resultPageFactory; $this->resultJsonFactory = $resultJsonFactory; - $this->storeManager = $storeManager; } /** @@ -52,20 +49,20 @@ public function __construct( public function execute() { $storeId = (int)$this->getRequest()->getParam('store'); - $store = $this->storeManager->getStore($storeId); - $this->storeManager->setCurrentStore($store->getCode()); + $store = $this->getStoreManager()->getStore($storeId); + $this->getStoreManager()->setCurrentStore($store->getCode()); $categoryId = (int)$this->getRequest()->getParam('id'); if (!$categoryId) { if ($storeId) { - $categoryId = (int)$this->storeManager->getStore($storeId)->getRootCategoryId(); + $categoryId = (int)$this->getStoreManager()->getStore($storeId)->getRootCategoryId(); } else { - $defaultStoreView = $this->storeManager->getDefaultStoreView(); + $defaultStoreView = $this->getStoreManager()->getDefaultStoreView(); if ($defaultStoreView) { $categoryId = (int)$defaultStoreView->getRootCategoryId(); } else { - $stores = $this->storeManager->getStores(); + $stores = $this->getStoreManager()->getStores(); if (count($stores)) { $store = reset($stores); $categoryId = (int)$store->getRootCategoryId(); @@ -114,4 +111,16 @@ public function execute() return $resultPage; } + + /** + * @return \Magento\Store\Model\StoreManagerInterface + */ + private function getStoreManager() + { + if (null === $this->storeManager) { + $this->storeManager = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Store\Model\StoreManagerInterface'); + } + return $this->storeManager; + } } diff --git a/app/code/Magento/Catalog/Controller/Adminhtml/Product/AddAttributeToTemplate.php b/app/code/Magento/Catalog/Controller/Adminhtml/Product/AddAttributeToTemplate.php index 11e3d641d0471..5f215a8594bd9 100644 --- a/app/code/Magento/Catalog/Controller/Adminhtml/Product/AddAttributeToTemplate.php +++ b/app/code/Magento/Catalog/Controller/Adminhtml/Product/AddAttributeToTemplate.php @@ -64,7 +64,7 @@ class AddAttributeToTemplate extends \Magento\Catalog\Controller\Adminhtml\Produ protected $attributeGroupFactory; /** - * @var AttributeManagementInterfaces + * @var AttributeManagementInterface */ protected $attributeManagement; @@ -82,42 +82,15 @@ class AddAttributeToTemplate extends \Magento\Catalog\Controller\Adminhtml\Produ * @param \Magento\Backend\App\Action\Context $context * @param Builder $productBuilder * @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory - * @param AttributeRepositoryInterface $attributeRepository - * @param AttributeSetRepositoryInterface $attributeSetRepository - * @param AttributeGroupRepositoryInterface $attributeGroupRepository - * @param AttributeGroupInterfaceFactory $attributeGroupFactory - * @param SearchCriteriaBuilder $searchCriteriaBuilder - * @param SortOrderBuilder $sortOrderBuilder - * @param AttributeManagementInterface $attributeManagement - * @param LoggerInterface $logger - * @param ExtensionAttributesFactory $extensionAttributesFactory * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct( \Magento\Backend\App\Action\Context $context, \Magento\Catalog\Controller\Adminhtml\Product\Builder $productBuilder, - \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory, - AttributeRepositoryInterface $attributeRepository, - AttributeSetRepositoryInterface $attributeSetRepository, - AttributeGroupRepositoryInterface $attributeGroupRepository, - AttributeGroupInterfaceFactory $attributeGroupFactory, - SearchCriteriaBuilder $searchCriteriaBuilder, - SortOrderBuilder $sortOrderBuilder, - AttributeManagementInterface $attributeManagement, - LoggerInterface $logger, - ExtensionAttributesFactory $extensionAttributesFactory + \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory ) { parent::__construct($context, $productBuilder); $this->resultJsonFactory = $resultJsonFactory; - $this->attributeRepository = $attributeRepository; - $this->attributeSetRepository = $attributeSetRepository; - $this->attributeGroupRepository = $attributeGroupRepository; - $this->attributeGroupFactory = $attributeGroupFactory; - $this->searchCriteriaBuilder = $searchCriteriaBuilder; - $this->sortOrderBuilder = $sortOrderBuilder; - $this->attributeManagement = $attributeManagement; - $this->logger = $logger; - $this->extensionAttributesFactory = $extensionAttributesFactory; } /** @@ -133,22 +106,22 @@ public function execute() try { /** @var AttributeSetInterface $attributeSet */ - $attributeSet = $this->attributeSetRepository->get($request->getParam('templateId')); + $attributeSet = $this->getAttributeSetRepository()->get($request->getParam('templateId')); $groupCode = $request->getParam('groupCode'); $groupName = $request->getParam('groupName'); $groupSortOrder = $request->getParam('groupSortOrder'); $attributeSearchCriteria = $this->getBasicAttributeSearchCriteriaBuilder()->create(); - $attributeGroupSearchCriteria = $this->searchCriteriaBuilder + $attributeGroupSearchCriteria = $this->getSearchCriteriaBuilder() ->addFilter('attribute_set_id', $attributeSet->getAttributeSetId()) ->addFilter('attribute_group_code', $groupCode) - ->addSortOrder($this->sortOrderBuilder->setAscendingDirection()->create()) + ->addSortOrder($this->getSortOrderBuilder()->setAscendingDirection()->create()) ->setPageSize(1) ->create(); try { /** @var AttributeGroupInterface[] $attributeGroupItems */ - $attributeGroupItems = $this->attributeGroupRepository->getList($attributeGroupSearchCriteria) + $attributeGroupItems = $this->getAttributeGroupRepository()->getList($attributeGroupSearchCriteria) ->getItems(); if (!$attributeGroupItems) { @@ -159,11 +132,11 @@ public function execute() $attributeGroup = reset($attributeGroupItems); } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { /** @var AttributeGroupInterface $attributeGroup */ - $attributeGroup = $this->attributeGroupFactory->create(); + $attributeGroup = $this->getAttributeGroupFactory()->create(); } $extensionAttributes = $attributeGroup->getExtensionAttributes() - ?: $this->extensionAttributesFactory->create(AttributeGroupInterface::class); + ?: $this->getExtensionAttributesFactory()->create(AttributeGroupInterface::class); $extensionAttributes->setAttributeGroupCode($groupCode); $extensionAttributes->setSortOrder($groupSortOrder); @@ -171,16 +144,16 @@ public function execute() $attributeGroup->setAttributeSetId($attributeSet->getAttributeSetId()); $attributeGroup->setExtensionAttributes($extensionAttributes); - $this->attributeGroupRepository->save($attributeGroup); + $this->getAttributeGroupRepository()->save($attributeGroup); /** @var AttributeInterface[] $attributesItems */ - $attributesItems = $this->attributeRepository->getList( + $attributesItems = $this->getAttributeRepository()->getList( ProductAttributeInterface::ENTITY_TYPE_CODE, $attributeSearchCriteria )->getItems(); array_walk($attributesItems, function (AttributeInterface $attribute) use ($attributeSet, $attributeGroup) { - $this->attributeManagement->assign( + $this->getAttributeManagement()->assign( ProductAttributeInterface::ENTITY_TYPE_CODE, $attributeSet->getAttributeSetId(), $attributeGroup->getAttributeGroupId(), @@ -192,7 +165,7 @@ public function execute() $response->setError(true); $response->setMessage($e->getMessage()); } catch (\Exception $e) { - $this->logger->critical($e); + $this->getLogger()->critical($e); $response->setError(true); $response->setMessage(__('Unable to add attribute')); } @@ -214,8 +187,116 @@ private function getBasicAttributeSearchCriteriaBuilder() throw new LocalizedException(__('Please, specify attributes')); } - return $this->searchCriteriaBuilder + return $this->getSearchCriteriaBuilder() ->addFilter('attribute_set_id', new \Zend_Db_Expr('null'), 'is') ->addFilter('attribute_id', [$attributeIds['selected']], 'in'); } + + /** + * @return AttributeRepositoryInterface + */ + private function getAttributeRepository() + { + if (null === $this->attributeRepository) { + $this->attributeRepository = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Eav\Api\AttributeRepositoryInterface'); + } + return $this->attributeRepository; + } + + /** + * @return AttributeSetRepositoryInterface + */ + private function getAttributeSetRepository() + { + if (null === $this->attributeSetRepository) { + $this->attributeSetRepository = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Catalog\Api\AttributeSetRepositoryInterface'); + } + return $this->attributeSetRepository; + } + + /** + * @return AttributeGroupRepositoryInterface + */ + private function getAttributeGroupRepository() + { + if (null === $this->attributeGroupRepository) { + $this->attributeGroupRepository = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Eav\Api\AttributeGroupRepositoryInterface'); + } + return $this->attributeGroupRepository; + } + + /** + * @return AttributeGroupInterfaceFactory + */ + private function getAttributeGroupFactory() + { + if (null === $this->attributeGroupFactory) { + $this->attributeGroupFactory = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Eav\Api\Data\AttributeGroupInterfaceFactory'); + } + return $this->attributeGroupFactory; + } + + /** + * @return SearchCriteriaBuilder + */ + private function getSearchCriteriaBuilder() + { + if (null === $this->searchCriteriaBuilder) { + $this->searchCriteriaBuilder = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Framework\Api\SearchCriteriaBuilder'); + } + return $this->searchCriteriaBuilder; + } + + /** + * @return SortOrderBuilder + */ + private function getSortOrderBuilder() + { + if (null === $this->sortOrderBuilder) { + $this->sortOrderBuilder = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Framework\Api\SortOrderBuilder'); + } + return $this->sortOrderBuilder; + } + + /** + * @return AttributeManagementInterface + */ + private function getAttributeManagement() + { + if (null === $this->attributeManagement) { + $this->attributeManagement = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Eav\Api\AttributeManagementInterface'); + } + return $this->attributeManagement; + } + + /** + * @return LoggerInterface + */ + private function getLogger() + { + if (null === $this->logger) { + $this->logger = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Psr\Log\LoggerInterface'); + } + return $this->logger; + } + + /** + * @return ExtensionAttributesFactory + */ + private function getExtensionAttributesFactory() + { + if (null === $this->extensionAttributesFactory) { + $this->extensionAttributesFactory = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Framework\Api\ExtensionAttributesFactory'); + } + return $this->extensionAttributesFactory; + } } diff --git a/app/code/Magento/Catalog/Controller/Adminhtml/Product/Builder.php b/app/code/Magento/Catalog/Controller/Adminhtml/Product/Builder.php index 72e0ac4b7e4b0..7004413c5c569 100644 --- a/app/code/Magento/Catalog/Controller/Adminhtml/Product/Builder.php +++ b/app/code/Magento/Catalog/Controller/Adminhtml/Product/Builder.php @@ -45,20 +45,17 @@ class Builder * @param Logger $logger * @param Registry $registry * @param WysiwygModel\Config $wysiwygConfig - * @param StoreFactory $storeFactory */ public function __construct( ProductFactory $productFactory, Logger $logger, Registry $registry, - WysiwygModel\Config $wysiwygConfig, - StoreFactory $storeFactory + WysiwygModel\Config $wysiwygConfig ) { $this->productFactory = $productFactory; $this->logger = $logger; $this->registry = $registry; $this->wysiwygConfig = $wysiwygConfig; - $this->storeFactory = $storeFactory; } /** @@ -73,7 +70,7 @@ public function build(RequestInterface $request) /** @var $product \Magento\Catalog\Model\Product */ $product = $this->productFactory->create(); $product->setStoreId($request->getParam('store', 0)); - $store = $this->storeFactory->create(); + $store = $this->getStoreFactory()->create(); $store->load($request->getParam('store', 0)); $typeId = $request->getParam('type'); @@ -102,4 +99,16 @@ public function build(RequestInterface $request) $this->wysiwygConfig->setStoreId($request->getParam('store')); return $product; } + + /** + * @return StoreFactory + */ + private function getStoreFactory() + { + if (null === $this->storeFactory) { + $this->storeFactory = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Store\Model\StoreFactory'); + } + return $this->storeFactory; + } } diff --git a/app/code/Magento/Catalog/Controller/Adminhtml/Product/Initialization/Helper.php b/app/code/Magento/Catalog/Controller/Adminhtml/Product/Initialization/Helper.php index b26f77b9bea77..b399d2a46bf95 100644 --- a/app/code/Magento/Catalog/Controller/Adminhtml/Product/Initialization/Helper.php +++ b/app/code/Magento/Catalog/Controller/Adminhtml/Product/Initialization/Helper.php @@ -33,6 +33,11 @@ class Helper */ protected $stockFilter; + /** + * @var \Magento\Backend\Helper\Js + */ + protected $jsHelper; + /** * @var \Magento\Framework\Stdlib\DateTime\Filter\Date */ @@ -64,33 +69,28 @@ class Helper private $linkResolver; /** + * Helper constructor. * @param \Magento\Framework\App\RequestInterface $request * @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param StockDataFilter $stockFilter * @param ProductLinks $productLinks + * @param \Magento\Backend\Helper\Js $jsHelper * @param \Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter - * @param CustomOptionFactory $customOptionFactory - * @param ProductLinkFactory $productLinkFactory - * @param ProductRepository $productRepository */ public function __construct( \Magento\Framework\App\RequestInterface $request, \Magento\Store\Model\StoreManagerInterface $storeManager, StockDataFilter $stockFilter, - ProductLinks $productLinks, - \Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter, - CustomOptionFactory $customOptionFactory, - ProductLinkFactory $productLinkFactory, - ProductRepository $productRepository + \Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks $productLinks, + \Magento\Backend\Helper\Js $jsHelper, + \Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter ) { $this->request = $request; $this->storeManager = $storeManager; $this->stockFilter = $stockFilter; $this->productLinks = $productLinks; + $this->jsHelper = $jsHelper; $this->dateFilter = $dateFilter; - $this->customOptionFactory = $customOptionFactory; - $this->productLinkFactory = $productLinkFactory; - $this->productRepository = $productRepository; } /** @@ -185,7 +185,7 @@ public function initialize(\Magento\Catalog\Model\Product $product) $customOptions = []; foreach ($options as $customOptionData) { if (empty($customOptionData['is_delete'])) { - $customOption = $this->customOptionFactory->create(['data' => $customOptionData]); + $customOption = $this->getCustomOptionFactory()->create(['data' => $customOptionData]); $customOption->setProductSku($product->getSku()); $customOption->setOptionId(null); $customOptions[] = $customOption; @@ -201,18 +201,6 @@ public function initialize(\Magento\Catalog\Model\Product $product) return $product; } - /** - * @deprecated - * @return LinkResolver - */ - private function getLinkResolver() - { - if (!is_object($this->linkResolver)) { - $this->linkResolver = ObjectManager::getInstance()->get(LinkResolver::class); - } - return $this->linkResolver; - } - /** * Setting product links * @@ -241,8 +229,8 @@ protected function setProductLinks(\Magento\Catalog\Model\Product $product) continue; } - $linkProduct = $this->productRepository->getById($linkData['id']); - $link = $this->productLinkFactory->create(); + $linkProduct = $this->getProductRepository()->getById($linkData['id']); + $link = $this->getProductLinkFactory()->create(); $link->setSku($product->getSku()) ->setLinkedProductSku($linkProduct->getSku()) ->setLinkType($linkType) @@ -304,4 +292,52 @@ public function mergeProductOptions($productOptions, $overwriteOptions) return $options; } + + /** + * @return CustomOptionFactory + */ + private function getCustomOptionFactory() + { + if (null === $this->customOptionFactory) { + $this->customOptionFactory = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory'); + } + return $this->customOptionFactory; + } + + /** + * @return ProductLinkFactory + */ + private function getProductLinkFactory() + { + if (null === $this->productLinkFactory) { + $this->productLinkFactory = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Catalog\Api\Data\ProductLinkInterfaceFactory'); + } + return $this->productLinkFactory; + } + + /** + * @return ProductRepository + */ + private function getProductRepository() + { + if (null === $this->productRepository) { + $this->productRepository = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Catalog\Api\ProductRepositoryInterface\Proxy'); + } + return $this->productRepository; + } + + /** + * @deprecated + * @return LinkResolver + */ + private function getLinkResolver() + { + if (!is_object($this->linkResolver)) { + $this->linkResolver = ObjectManager::getInstance()->get(LinkResolver::class); + } + return $this->linkResolver; + } } diff --git a/app/code/Magento/Catalog/Controller/Adminhtml/Product/Save.php b/app/code/Magento/Catalog/Controller/Adminhtml/Product/Save.php index 889abf03b8219..d7cd6ba398424 100644 --- a/app/code/Magento/Catalog/Controller/Adminhtml/Product/Save.php +++ b/app/code/Magento/Catalog/Controller/Adminhtml/Product/Save.php @@ -50,9 +50,7 @@ class Save extends \Magento\Catalog\Controller\Adminhtml\Product * @param Initialization\Helper $initializationHelper * @param \Magento\Catalog\Model\Product\Copier $productCopier * @param \Magento\Catalog\Model\Product\TypeTransitionManager $productTypeManager - * @param \Magento\Catalog\Api\CategoryLinkManagementInterface $categoryLinkManagement * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository - * @param StoreManagerInterface $storeManager */ public function __construct( \Magento\Backend\App\Action\Context $context, @@ -60,16 +58,12 @@ public function __construct( Initialization\Helper $initializationHelper, \Magento\Catalog\Model\Product\Copier $productCopier, \Magento\Catalog\Model\Product\TypeTransitionManager $productTypeManager, - \Magento\Catalog\Api\CategoryLinkManagementInterface $categoryLinkManagement, - \Magento\Catalog\Api\ProductRepositoryInterface $productRepository, - StoreManagerInterface $storeManager + \Magento\Catalog\Api\ProductRepositoryInterface $productRepository ) { $this->initializationHelper = $initializationHelper; $this->productCopier = $productCopier; $this->productTypeManager = $productTypeManager; - $this->categoryLinkManagement = $categoryLinkManagement; $this->productRepository = $productRepository; - $this->storeManager = $storeManager; parent::__construct($context, $productBuilder); } @@ -83,8 +77,8 @@ public function __construct( public function execute() { $storeId = $this->getRequest()->getParam('store', 0); - $store = $this->storeManager->getStore($storeId); - $this->storeManager->setCurrentStore($store->getCode()); + $store = $this->getStoreManager()->getStore($storeId); + $this->getStoreManager()->setCurrentStore($store->getCode()); $redirectBack = $this->getRequest()->getParam('back', false); $productId = $this->getRequest()->getParam('id'); $resultRedirect = $this->resultRedirectFactory->create(); @@ -105,7 +99,7 @@ public function execute() $originalSku = $product->getSku(); $product->save(); $this->handleImageRemoveError($data, $product->getId()); - $this->categoryLinkManagement->assignProductToCategories( + $this->getCategoryLinkManagement()->assignProductToCategories( $product->getSku(), $product->getCategoryIds() ); @@ -228,4 +222,28 @@ protected function copyToStores($data, $productId) } } } + + /** + * @return \Magento\Catalog\Api\CategoryLinkManagementInterface + */ + private function getCategoryLinkManagement() + { + if (null === $this->categoryLinkManagement) { + $this->categoryLinkManagement = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Catalog\Api\CategoryLinkManagementInterface'); + } + return $this->categoryLinkManagement; + } + + /** + * @return StoreManagerInterface + */ + private function getStoreManager() + { + if (null === $this->storeManager) { + $this->storeManager = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Store\Model\StoreManagerInterface'); + } + return $this->storeManager; + } } diff --git a/app/code/Magento/Catalog/Model/CategoryLinkManagement.php b/app/code/Magento/Catalog/Model/CategoryLinkManagement.php index 7c875f93634d9..da6901ec88d13 100644 --- a/app/code/Magento/Catalog/Model/CategoryLinkManagement.php +++ b/app/code/Magento/Catalog/Model/CategoryLinkManagement.php @@ -45,26 +45,14 @@ class CategoryLinkManagement implements \Magento\Catalog\Api\CategoryLinkManagem * CategoryLinkManagement constructor. * * @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository - * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository - * @param ResourceModel\Product $productResource - * @param \Magento\Catalog\Api\CategoryLinkRepositoryInterface $categoryLinkRepository * @param \Magento\Catalog\Api\Data\CategoryProductLinkInterfaceFactory $productLinkFactory - * @param \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry */ public function __construct( \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository, - \Magento\Catalog\Api\ProductRepositoryInterface $productRepository, - ResourceModel\Product $productResource, - \Magento\Catalog\Api\CategoryLinkRepositoryInterface $categoryLinkRepository, - \Magento\Catalog\Api\Data\CategoryProductLinkInterfaceFactory $productLinkFactory, - \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry + \Magento\Catalog\Api\Data\CategoryProductLinkInterfaceFactory $productLinkFactory ) { $this->categoryRepository = $categoryRepository; - $this->productRepository = $productRepository; - $this->productResource = $productResource; - $this->categoryLinkRepository = $categoryLinkRepository; $this->productLinkFactory = $productLinkFactory; - $this->indexerRegistry = $indexerRegistry; } /** @@ -102,10 +90,10 @@ public function getAssignedProducts($categoryId) */ public function assignProductToCategories($productSku, array $categoryIds) { - $product = $this->productRepository->get($productSku); - $assignedCategories = $this->productResource->getCategoryIds($product); + $product = $this->getProductRepository()->get($productSku); + $assignedCategories = $this->getProductResource()->getCategoryIds($product); foreach (array_diff($assignedCategories, $categoryIds) as $categoryId) { - $this->categoryLinkRepository->deleteByIds($categoryId, $productSku); + $this->getCategoryLinkRepository()->deleteByIds($categoryId, $productSku); } foreach (array_diff($categoryIds, $assignedCategories) as $categoryId) { @@ -114,12 +102,68 @@ public function assignProductToCategories($productSku, array $categoryIds) $categoryProductLink->setSku($productSku); $categoryProductLink->setCategoryId($categoryId); $categoryProductLink->setPosition(0); - $this->categoryLinkRepository->save($categoryProductLink); + $this->getCategoryLinkRepository()->save($categoryProductLink); } - $productCategoryIndexer = $this->indexerRegistry->get(Indexer\Product\Category::INDEXER_ID); + $productCategoryIndexer = $this->getIndexerRegistry()->get(Indexer\Product\Category::INDEXER_ID); if (!$productCategoryIndexer->isScheduled()) { $productCategoryIndexer->reindexRow($product->getId()); } return true; } + + /** + * Retrieve product repository instance + * + * @return \Magento\Catalog\Api\ProductRepositoryInterface + */ + private function getProductRepository() + { + if (null === $this->productRepository) { + $this->productRepository = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Catalog\Api\ProductRepositoryInterface'); + } + return $this->productRepository; + } + + /** + * Retrieve product resource instance + * + * @return ResourceModel\Product + */ + private function getProductResource() + { + if (null === $this->productResource) { + $this->productResource = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Catalog\Model\ResourceModel\Product'); + } + return $this->productResource; + } + + /** + * Retrieve category link repository instance + * + * @return \Magento\Catalog\Api\CategoryLinkRepositoryInterface + */ + private function getCategoryLinkRepository() + { + if (null === $this->categoryLinkRepository) { + $this->categoryLinkRepository = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Catalog\Api\CategoryLinkRepositoryInterface'); + } + return $this->categoryLinkRepository; + } + + /** + * Retrieve indexer registry instance + * + * @return \Magento\Framework\Indexer\IndexerRegistry + */ + private function getIndexerRegistry() + { + if (null === $this->indexerRegistry) { + $this->indexerRegistry = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Framework\Indexer\IndexerRegistry'); + } + return $this->indexerRegistry; + } } diff --git a/app/code/Magento/Catalog/Model/CategoryRepository.php b/app/code/Magento/Catalog/Model/CategoryRepository.php index 91176a3214eb7..f9603659ab1ea 100644 --- a/app/code/Magento/Catalog/Model/CategoryRepository.php +++ b/app/code/Magento/Catalog/Model/CategoryRepository.php @@ -58,18 +58,15 @@ class CategoryRepository implements \Magento\Catalog\Api\CategoryRepositoryInter * @param \Magento\Catalog\Model\CategoryFactory $categoryFactory * @param \Magento\Catalog\Model\ResourceModel\Category $categoryResource * @param \Magento\Store\Model\StoreManagerInterface $storeManager - * @param \Magento\Framework\Model\Entity\MetadataPool $metadataPool */ public function __construct( \Magento\Catalog\Model\CategoryFactory $categoryFactory, \Magento\Catalog\Model\ResourceModel\Category $categoryResource, - \Magento\Store\Model\StoreManagerInterface $storeManager, - \Magento\Framework\Model\Entity\MetadataPool $metadataPool + \Magento\Store\Model\StoreManagerInterface $storeManager ) { $this->categoryFactory = $categoryFactory; $this->categoryResource = $categoryResource; $this->storeManager = $storeManager; - $this->metadataPool = $metadataPool; } /** @@ -84,7 +81,7 @@ public function save(\Magento\Catalog\Api\Data\CategoryInterface $category) $existingData['store_id'] = $storeId; if ($category->getId()) { - $metadata = $this->metadataPool->getMetadata( + $metadata = $this->getMetadataPool()->getMetadata( CategoryInterface::class ); @@ -216,4 +213,16 @@ private function getExtensibleDataObjectConverter() } return $this->extensibleDataObjectConverter; } + + /** + * @return \Magento\Framework\Model\Entity\MetadataPool + */ + private function getMetadataPool() + { + if (null === $this->metadataPool) { + $this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Framework\Model\Entity\MetadataPool'); + } + return $this->metadataPool; + } } diff --git a/app/code/Magento/Catalog/Model/Indexer/Category/Flat/AbstractAction.php b/app/code/Magento/Catalog/Model/Indexer/Category/Flat/AbstractAction.php index ad5def50520a0..c4e435cf4f3c0 100644 --- a/app/code/Magento/Catalog/Model/Indexer/Category/Flat/AbstractAction.php +++ b/app/code/Magento/Catalog/Model/Indexer/Category/Flat/AbstractAction.php @@ -74,17 +74,13 @@ class AbstractAction public function __construct( ResourceConnection $resource, \Magento\Store\Model\StoreManagerInterface $storeManager, - \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper, - MetadataPool $metadataPool, - $skipStaticColumns = [] + \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper ) { $this->resource = $resource; $this->connection = $resource->getConnection(); $this->storeManager = $storeManager; $this->resourceHelper = $resourceHelper; - $this->skipStaticColumns = $skipStaticColumns; $this->columns = array_merge($this->getStaticColumns(), $this->getEavColumns()); - $this->categoryMetadata = $metadataPool->getMetadata(\Magento\Catalog\Api\Data\CategoryInterface::class); } /** @@ -199,7 +195,7 @@ protected function getStaticColumns() ); foreach ($describe as $column) { - if (in_array($column['COLUMN_NAME'], $this->skipStaticColumns)) { + if (in_array($column['COLUMN_NAME'], $this->getSkipStaticColumns())) { continue; } $isUnsigned = ''; @@ -380,11 +376,11 @@ protected function getAttributeValues($entityIds, $storeId) $attributesType = ['varchar', 'int', 'decimal', 'text', 'datetime']; foreach ($attributesType as $type) { foreach ($this->getAttributeTypeValues($type, $entityIds, $storeId) as $row) { - if (isset($row[$this->categoryMetadata->getLinkField()]) && isset($row['attribute_id'])) { + if (isset($row[$this->getCategoryMetadata()->getLinkField()]) && isset($row['attribute_id'])) { $attributeId = $row['attribute_id']; if (isset($attributes[$attributeId])) { $attributeCode = $attributes[$attributeId]['attribute_code']; - $values[$row[$this->categoryMetadata->getLinkField()]][$attributeCode] = $row['value']; + $values[$row[$this->getCategoryMetadata()->getLinkField()]][$attributeCode] = $row['value']; } } } @@ -402,7 +398,7 @@ protected function getAttributeValues($entityIds, $storeId) */ protected function getAttributeTypeValues($type, $entityIds, $storeId) { - $linkField = $this->categoryMetadata->getLinkField(); + $linkField = $this->getCategoryMetadata()->getLinkField(); $select = $this->connection->select()->from( [ 'def' => $this->connection->getTableName($this->getTableName('catalog_category_entity_' . $type)), @@ -469,4 +465,30 @@ protected function getTableName($name) { return $this->resource->getTableName($name); } + + /** + * @return \Magento\Framework\Model\Entity\EntityMetadata + */ + private function getCategoryMetadata() + { + if (null === $this->categoryMetadata) { + $metadataPool = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Framework\Model\Entity\MetadataPool'); + $this->categoryMetadata = $metadataPool->getMetadata(\Magento\Catalog\Api\Data\CategoryInterface::class); + } + return $this->categoryMetadata; + } + + /** + * @return array + */ + private function getSkipStaticColumns() + { + if (null === $this->skipStaticColumns) { + $provider = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Catalog\Model\Indexer\Category\Flat\SkipStaticColumnsProvider'); + $this->skipStaticColumns = $provider->get(); + } + return $this->skipStaticColumns; + } } diff --git a/app/code/Magento/Catalog/Model/Indexer/Category/Flat/Action/Rows.php b/app/code/Magento/Catalog/Model/Indexer/Category/Flat/Action/Rows.php index 5891e9ddb7b47..79d3a9250e639 100644 --- a/app/code/Magento/Catalog/Model/Indexer/Category/Flat/Action/Rows.php +++ b/app/code/Magento/Catalog/Model/Indexer/Category/Flat/Action/Rows.php @@ -7,7 +7,6 @@ use Magento\Catalog\Api\CategoryRepositoryInterface; use Magento\Framework\Exception\NoSuchEntityException; -use Magento\Framework\Model\Entity\MetadataPool; class Rows extends \Magento\Catalog\Model\Indexer\Category\Flat\AbstractAction { @@ -20,20 +19,16 @@ class Rows extends \Magento\Catalog\Model\Indexer\Category\Flat\AbstractAction * @param \Magento\Framework\App\ResourceConnection $resource * @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper - * @param MetadataPool $metadataPool - * @param array $skipStaticColumns * @param CategoryRepositoryInterface $categoryRepository */ public function __construct( \Magento\Framework\App\ResourceConnection $resource, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper, - MetadataPool $metadataPool, - CategoryRepositoryInterface $categoryRepository, - $skipStaticColumns = [] + CategoryRepositoryInterface $categoryRepository ) { $this->categoryRepository = $categoryRepository; - parent::__construct($resource, $storeManager, $resourceHelper, $metadataPool, $skipStaticColumns); + parent::__construct($resource, $storeManager, $resourceHelper); } /** diff --git a/app/code/Magento/Catalog/Model/Indexer/Category/Flat/SkipStaticColumnsProvider.php b/app/code/Magento/Catalog/Model/Indexer/Category/Flat/SkipStaticColumnsProvider.php new file mode 100644 index 0000000000000..1da141f477141 --- /dev/null +++ b/app/code/Magento/Catalog/Model/Indexer/Category/Flat/SkipStaticColumnsProvider.php @@ -0,0 +1,32 @@ +skipStaticColumns = $skipStaticColumns; + } + + /** + * @return array + */ + public function get() + { + return $this->skipStaticColumns; + } +} diff --git a/app/code/Magento/Catalog/Model/Indexer/Category/Product/AbstractAction.php b/app/code/Magento/Catalog/Model/Indexer/Category/Product/AbstractAction.php index 348f944307b82..bcf27560f0e5d 100644 --- a/app/code/Magento/Catalog/Model/Indexer/Category/Product/AbstractAction.php +++ b/app/code/Magento/Catalog/Model/Indexer/Category/Product/AbstractAction.php @@ -101,19 +101,16 @@ abstract class AbstractAction * @param ResourceConnection $resource * @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param \Magento\Catalog\Model\Config $config - * @param MetadataPool $metadataPool */ public function __construct( \Magento\Framework\App\ResourceConnection $resource, \Magento\Store\Model\StoreManagerInterface $storeManager, - \Magento\Catalog\Model\Config $config, - MetadataPool $metadataPool + \Magento\Catalog\Model\Config $config ) { $this->resource = $resource; $this->connection = $resource->getConnection(); $this->storeManager = $storeManager; $this->config = $config; - $this->metadataPool = $metadataPool; } /** @@ -214,7 +211,7 @@ protected function getNonAnchorCategoriesSelect(\Magento\Store\Model\Store $stor $rootPath = $this->getPathFromCategoryId($store->getRootCategoryId()); - $metadata = $this->metadataPool->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class); + $metadata = $this->getMetadataPool()->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class); $linkField = $metadata->getLinkField(); $select = $this->connection->select()->from( ['cc' => $this->getTable('catalog_category_entity')], @@ -371,8 +368,8 @@ protected function createAnchorSelect(\Magento\Store\Model\Store $store) $rootCatIds = explode('/', $this->getPathFromCategoryId($store->getRootCategoryId())); array_pop($rootCatIds); - $productMetadata = $this->metadataPool->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class); - $categoryMetadata = $this->metadataPool->getMetadata(\Magento\Catalog\Api\Data\CategoryInterface::class); + $productMetadata = $this->getMetadataPool()->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class); + $categoryMetadata = $this->getMetadataPool()->getMetadata(\Magento\Catalog\Api\Data\CategoryInterface::class); $productLinkField = $productMetadata->getLinkField(); $categoryLinkField = $categoryMetadata->getLinkField(); @@ -516,7 +513,7 @@ protected function getAllProducts(\Magento\Store\Model\Store $store) 'visibility' )->getId(); - $metadata = $this->metadataPool->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class); + $metadata = $this->getMetadataPool()->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class); $linkField = $metadata->getLinkField(); $select = $this->connection->select()->from( @@ -629,4 +626,16 @@ protected function reindexRootCategory(\Magento\Store\Model\Store $store) } } } + + /** + * @return \Magento\Framework\Model\Entity\MetadataPool + */ + private function getMetadataPool() + { + if (null === $this->metadataPool) { + $this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Framework\Model\Entity\MetadataPool'); + } + return $this->metadataPool; + } } diff --git a/app/code/Magento/Catalog/Model/Indexer/Product/Flat/AbstractAction.php b/app/code/Magento/Catalog/Model/Indexer/Product/Flat/AbstractAction.php index 71fd044f47e78..464b0e3f9434b 100644 --- a/app/code/Magento/Catalog/Model/Indexer/Product/Flat/AbstractAction.php +++ b/app/code/Magento/Catalog/Model/Indexer/Product/Flat/AbstractAction.php @@ -79,7 +79,6 @@ abstract class AbstractAction private $metadataPool; /** - * @param MetadataPool $metadataPool * @param \Magento\Framework\App\ResourceConnection $resource * @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param \Magento\Catalog\Helper\Product\Flat\Indexer $productHelper @@ -88,7 +87,6 @@ abstract class AbstractAction * @param FlatTableBuilder $flatTableBuilder */ public function __construct( - MetadataPool $metadataPool, \Magento\Framework\App\ResourceConnection $resource, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Helper\Product\Flat\Indexer $productHelper, @@ -102,7 +100,6 @@ public function __construct( $this->_connection = $resource->getConnection(); $this->_tableBuilder = $tableBuilder; $this->_flatTableBuilder = $flatTableBuilder; - $this->metadataPool = $metadataPool; } /** @@ -204,7 +201,7 @@ protected function _updateRelationProducts($storeId, $productIds = null) return $this; } - $metadata = $this->metadataPool->getMetadata(ProductInterface::class); + $metadata = $this->getMetadataPool()->getMetadata(ProductInterface::class); foreach ($this->_getProductTypeInstances() as $typeInstance) { /** @var $typeInstance \Magento\Catalog\Model\Product\Type\AbstractType */ @@ -263,7 +260,7 @@ protected function _cleanRelationProducts($storeId) return $this; } - $metadata = $this->metadataPool->getMetadata(ProductInterface::class); + $metadata = $this->getMetadataPool()->getMetadata(ProductInterface::class); foreach ($this->_getProductTypeInstances() as $typeInstance) { /** @var $typeInstance \Magento\Catalog\Model\Product\Type\AbstractType */ @@ -336,4 +333,16 @@ protected function _isFlatTableExists($storeId) return $this->_flatTablesExist[$storeId]; } + + /** + * @return \Magento\Framework\Model\Entity\MetadataPool + */ + private function getMetadataPool() + { + if (null === $this->metadataPool) { + $this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Framework\Model\Entity\MetadataPool'); + } + return $this->metadataPool; + } } diff --git a/app/code/Magento/Catalog/Model/Indexer/Product/Flat/Action/Indexer.php b/app/code/Magento/Catalog/Model/Indexer/Product/Flat/Action/Indexer.php index 71277034bc2c3..036f9d12349b0 100644 --- a/app/code/Magento/Catalog/Model/Indexer/Product/Flat/Action/Indexer.php +++ b/app/code/Magento/Catalog/Model/Indexer/Product/Flat/Action/Indexer.php @@ -38,16 +38,13 @@ class Indexer /** * @param ResourceConnection $resource * @param \Magento\Catalog\Helper\Product\Flat\Indexer $productHelper - * @param MetadataPool $metadataPool */ public function __construct( \Magento\Framework\App\ResourceConnection $resource, - \Magento\Catalog\Helper\Product\Flat\Indexer $productHelper, - MetadataPool $metadataPool + \Magento\Catalog\Helper\Product\Flat\Indexer $productHelper ) { $this->_productIndexerHelper = $productHelper; $this->_connection = $resource->getConnection(); - $this->metadataPool = $metadataPool; } /** @@ -100,7 +97,7 @@ public function write($storeId, $productId, $valueFieldSuffix = '') $ids[$attribute->getId()] = $columnName; } } - $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(); + $linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(); $select->joinLeft( ['t' => $tableName], sprintf('e.%s = t.%s ', $linkField, $linkField) . $this->_connection->quoteInto( @@ -181,4 +178,16 @@ public function write($storeId, $productId, $valueFieldSuffix = '') return $this; } + + /** + * @return \Magento\Framework\Model\Entity\MetadataPool + */ + private function getMetadataPool() + { + if (null === $this->metadataPool) { + $this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Framework\Model\Entity\MetadataPool'); + } + return $this->metadataPool; + } } diff --git a/app/code/Magento/Catalog/Model/Indexer/Product/Flat/Action/Row.php b/app/code/Magento/Catalog/Model/Indexer/Product/Flat/Action/Row.php index 3d08063b5e7c1..e944940496664 100644 --- a/app/code/Magento/Catalog/Model/Indexer/Product/Flat/Action/Row.php +++ b/app/code/Magento/Catalog/Model/Indexer/Product/Flat/Action/Row.php @@ -7,7 +7,6 @@ use Magento\Catalog\Model\Indexer\Product\Flat\FlatTableBuilder; use Magento\Catalog\Model\Indexer\Product\Flat\TableBuilder; -use Magento\Framework\Model\Entity\MetadataPool; /** * Class Row reindex action @@ -25,7 +24,6 @@ class Row extends \Magento\Catalog\Model\Indexer\Product\Flat\AbstractAction protected $flatItemEraser; /** - * @param MetadataPool $metadataPool * @param \Magento\Framework\App\ResourceConnection $resource * @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param \Magento\Catalog\Helper\Product\Flat\Indexer $productHelper @@ -36,7 +34,6 @@ class Row extends \Magento\Catalog\Model\Indexer\Product\Flat\AbstractAction * @param Eraser $flatItemEraser */ public function __construct( - MetadataPool $metadataPool, \Magento\Framework\App\ResourceConnection $resource, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Helper\Product\Flat\Indexer $productHelper, @@ -47,7 +44,6 @@ public function __construct( Eraser $flatItemEraser ) { parent::__construct( - $metadataPool, $resource, $storeManager, $productHelper, diff --git a/app/code/Magento/Catalog/Model/Indexer/Product/Flat/Action/Rows.php b/app/code/Magento/Catalog/Model/Indexer/Product/Flat/Action/Rows.php index eec7495f5566e..6be815952f118 100644 --- a/app/code/Magento/Catalog/Model/Indexer/Product/Flat/Action/Rows.php +++ b/app/code/Magento/Catalog/Model/Indexer/Product/Flat/Action/Rows.php @@ -7,7 +7,6 @@ use Magento\Catalog\Model\Indexer\Product\Flat\FlatTableBuilder; use Magento\Catalog\Model\Indexer\Product\Flat\TableBuilder; -use Magento\Framework\Model\Entity\MetadataPool; /** * Class Rows reindex action for mass actions @@ -21,7 +20,6 @@ class Rows extends \Magento\Catalog\Model\Indexer\Product\Flat\AbstractAction protected $flatItemEraser; /** - * @param MetadataPool $metadataPool * @param \Magento\Framework\App\ResourceConnection $resource * @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param \Magento\Catalog\Helper\Product\Flat\Indexer $productHelper @@ -31,7 +29,6 @@ class Rows extends \Magento\Catalog\Model\Indexer\Product\Flat\AbstractAction * @param Eraser $flatItemEraser */ public function __construct( - MetadataPool $metadataPool, \Magento\Framework\App\ResourceConnection $resource, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Helper\Product\Flat\Indexer $productHelper, @@ -41,7 +38,6 @@ public function __construct( Eraser $flatItemEraser ) { parent::__construct( - $metadataPool, $resource, $storeManager, $productHelper, diff --git a/app/code/Magento/Catalog/Model/Indexer/Product/Flat/FlatTableBuilder.php b/app/code/Magento/Catalog/Model/Indexer/Product/Flat/FlatTableBuilder.php index e8f297aae655d..621c1f11c81ce 100644 --- a/app/code/Magento/Catalog/Model/Indexer/Product/Flat/FlatTableBuilder.php +++ b/app/code/Magento/Catalog/Model/Indexer/Product/Flat/FlatTableBuilder.php @@ -61,15 +61,13 @@ class FlatTableBuilder * @param \Magento\Framework\App\Config\ScopeConfigInterface $config * @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param TableDataInterface $tableData - * @param MetadataPool $metadataPool */ public function __construct( \Magento\Catalog\Helper\Product\Flat\Indexer $productIndexerHelper, \Magento\Framework\App\ResourceConnection $resource, \Magento\Framework\App\Config\ScopeConfigInterface $config, \Magento\Store\Model\StoreManagerInterface $storeManager, - \Magento\Catalog\Model\Indexer\Product\Flat\TableDataInterface $tableData, - MetadataPool $metadataPool + \Magento\Catalog\Model\Indexer\Product\Flat\TableDataInterface $tableData ) { $this->_productIndexerHelper = $productIndexerHelper; $this->resource = $resource; @@ -77,7 +75,6 @@ public function __construct( $this->_config = $config; $this->_storeManager = $storeManager; $this->_tableData = $tableData; - $this->metadataPool = $metadataPool; } /** @@ -211,7 +208,7 @@ protected function _createTemporaryFlatTable($storeId) */ protected function _fillTemporaryFlatTable(array $tables, $storeId, $valueFieldSuffix) { - $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(); + $linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(); $select = $this->_connection->select(); $temporaryFlatTableName = $this->_getTemporaryTableName( $this->_productIndexerHelper->getFlatTableName($storeId) @@ -309,7 +306,7 @@ protected function _updateTemporaryTableByStoreValues( $temporaryFlatTableName = $this->_getTemporaryTableName( $this->_productIndexerHelper->getFlatTableName($storeId) ); - $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(); + $linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(); foreach ($tables as $tableName => $columns) { foreach ($columns as $attribute) { /* @var $attribute \Magento\Eav\Model\Entity\Attribute */ @@ -366,4 +363,16 @@ protected function _getTemporaryTableName($tableName) { return sprintf('%s_tmp_indexer', $tableName); } + + /** + * @return \Magento\Framework\Model\Entity\MetadataPool + */ + private function getMetadataPool() + { + if (null === $this->metadataPool) { + $this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Framework\Model\Entity\MetadataPool'); + } + return $this->metadataPool; + } } diff --git a/app/code/Magento/Catalog/Model/Indexer/Product/Flat/TableBuilder.php b/app/code/Magento/Catalog/Model/Indexer/Product/Flat/TableBuilder.php index a98b39c15fe1d..588bc742e1eb2 100644 --- a/app/code/Magento/Catalog/Model/Indexer/Product/Flat/TableBuilder.php +++ b/app/code/Magento/Catalog/Model/Indexer/Product/Flat/TableBuilder.php @@ -42,17 +42,14 @@ class TableBuilder /** * @param \Magento\Catalog\Helper\Product\Flat\Indexer $productIndexerHelper * @param \Magento\Framework\App\ResourceConnection $resource - * @param \Magento\Framework\Model\Entity\MetadataPool $metadataPool */ public function __construct( \Magento\Catalog\Helper\Product\Flat\Indexer $productIndexerHelper, - \Magento\Framework\App\ResourceConnection $resource, - \Magento\Framework\Model\Entity\MetadataPool $metadataPool + \Magento\Framework\App\ResourceConnection $resource ) { $this->_productIndexerHelper = $productIndexerHelper; $this->resource = $resource; $this->_connection = $resource->getConnection(); - $this->metadataPool = $metadataPool; } /** @@ -249,7 +246,7 @@ protected function _fillTemporaryTable( $valueFieldSuffix, $storeId ) { - $metadata = $this->metadataPool->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class); + $metadata = $this->getMetadataPool()->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class); if (!empty($tableColumns)) { $columnsChunks = array_chunk( $tableColumns, @@ -335,4 +332,16 @@ protected function _fillTemporaryTable( } } } + + /** + * @return \Magento\Framework\Model\Entity\MetadataPool + */ + private function getMetadataPool() + { + if (null === $this->metadataPool) { + $this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Framework\Model\Entity\MetadataPool'); + } + return $this->metadataPool; + } } diff --git a/app/code/Magento/Catalog/Model/Product.php b/app/code/Magento/Catalog/Model/Product.php index 2a2150c79e29a..d1f91a90e105c 100644 --- a/app/code/Magento/Catalog/Model/Product.php +++ b/app/code/Magento/Catalog/Model/Product.php @@ -349,6 +349,12 @@ class Product extends \Magento\Catalog\Model\AbstractModel implements protected $mediaGalleryProcessor; /** + * @var Product\LinkTypeProvider + */ + protected $linkTypeProvider; + + /** + * Product constructor. * @param \Magento\Framework\Model\Context $context * @param \Magento\Framework\Registry $registry * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory @@ -366,8 +372,8 @@ class Product extends \Magento\Catalog\Model\AbstractModel implements * @param Product\Type $catalogProductType * @param \Magento\Framework\Module\Manager $moduleManager * @param \Magento\Catalog\Helper\Product $catalogProduct - * @param \Magento\Catalog\Model\ResourceModel\Product $resource - * @param \Magento\Catalog\Model\ResourceModel\Product\Collection $resourceCollection + * @param ResourceModel\Product $resource + * @param ResourceModel\Product\Collection $resourceCollection * @param \Magento\Framework\Data\CollectionFactory $collectionFactory * @param \Magento\Framework\Filesystem $filesystem * @param \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry @@ -376,14 +382,17 @@ class Product extends \Magento\Catalog\Model\AbstractModel implements * @param Indexer\Product\Eav\Processor $productEavIndexerProcessor * @param CategoryRepositoryInterface $categoryRepository * @param Product\Image\CacheFactory $imageCacheFactory + * @param ProductLink\CollectionProvider $entityCollectionProvider + * @param Product\LinkTypeProvider $linkTypeProvider + * @param \Magento\Catalog\Api\Data\ProductLinkInterfaceFactory $productLinkFactory + * @param \Magento\Catalog\Api\Data\ProductLinkExtensionFactory $productLinkExtensionFactory * @param EntryConverterPool $mediaGalleryEntryConverterPool * @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper * @param \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor - * @param ProductLinkRepositoryInterface $linkRepository - * @param \Magento\Catalog\Model\Product\Gallery\Processor $mediaGalleryProcessor * @param array $data * * @SuppressWarnings(PHPMD.ExcessiveParameterList) + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function __construct( \Magento\Framework\Model\Context $context, @@ -413,11 +422,13 @@ public function __construct( \Magento\Catalog\Model\Indexer\Product\Eav\Processor $productEavIndexerProcessor, CategoryRepositoryInterface $categoryRepository, Product\Image\CacheFactory $imageCacheFactory, + \Magento\Catalog\Model\ProductLink\CollectionProvider $entityCollectionProvider, + \Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider, + \Magento\Catalog\Api\Data\ProductLinkInterfaceFactory $productLinkFactory, + \Magento\Catalog\Api\Data\ProductLinkExtensionFactory $productLinkExtensionFactory, EntryConverterPool $mediaGalleryEntryConverterPool, \Magento\Framework\Api\DataObjectHelper $dataObjectHelper, \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor, - ProductLinkRepositoryInterface $linkRepository, - \Magento\Catalog\Model\Product\Gallery\Processor $mediaGalleryProcessor, array $data = [] ) { $this->metadataService = $metadataService; @@ -440,11 +451,13 @@ public function __construct( $this->_productEavIndexerProcessor = $productEavIndexerProcessor; $this->categoryRepository = $categoryRepository; $this->imageCacheFactory = $imageCacheFactory; + $this->entityCollectionProvider = $entityCollectionProvider; + $this->linkTypeProvider = $linkTypeProvider; + $this->productLinkFactory = $productLinkFactory; + $this->productLinkExtensionFactory = $productLinkExtensionFactory; $this->mediaGalleryEntryConverterPool = $mediaGalleryEntryConverterPool; $this->dataObjectHelper = $dataObjectHelper; $this->joinProcessor = $joinProcessor; - $this->linkRepository = $linkRepository; - $this->mediaGalleryProcessor = $mediaGalleryProcessor; parent::__construct( $context, $registry, @@ -1396,7 +1409,7 @@ public function getCrossSellLinkCollection() public function getProductLinks() { if ($this->_links === null) { - $this->_links = $this->linkRepository->getList($this); + $this->_links = $this->getLinkRepository()->getList($this); } return $this->_links; } @@ -1508,7 +1521,7 @@ public function hasGalleryAttribute() public function addImageToMediaGallery($file, $mediaAttribute = null, $move = false, $exclude = true) { if ($this->hasGalleryAttribute()) { - $this->mediaGalleryProcessor->addImage( + $this->getMediaGalleryProcessor()->addImage( $this, $file, $mediaAttribute, @@ -2572,4 +2585,28 @@ private function getAppState() } return $this->appState; } + + /** + * @return ProductLinkRepositoryInterface + */ + private function getLinkRepository() + { + if (null === $this->linkRepository) { + $this->linkRepository = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Catalog\Api\ProductLinkRepositoryInterface'); + } + return $this->linkRepository; + } + + /** + * @return Product\Gallery\Processor + */ + private function getMediaGalleryProcessor() + { + if (null === $this->mediaGalleryProcessor) { + $this->mediaGalleryProcessor = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Catalog\Model\Product\Gallery\Processor'); + } + return $this->mediaGalleryProcessor; + } } diff --git a/app/code/Magento/Catalog/Model/Product/Attribute/Backend/GroupPrice/AbstractGroupPrice.php b/app/code/Magento/Catalog/Model/Product/Attribute/Backend/GroupPrice/AbstractGroupPrice.php index ee2f2a372fdf9..b8d6dfb745cc3 100644 --- a/app/code/Magento/Catalog/Model/Product/Attribute/Backend/GroupPrice/AbstractGroupPrice.php +++ b/app/code/Magento/Catalog/Model/Product/Attribute/Backend/GroupPrice/AbstractGroupPrice.php @@ -59,7 +59,6 @@ abstract protected function _getDuplicateErrorMessage(); * @param \Magento\Framework\Locale\FormatInterface $localeFormat * @param \Magento\Catalog\Model\Product\Type $catalogProductType * @param GroupManagementInterface $groupManagement - * @param \Magento\Framework\Model\Entity\MetadataPool $metadataPool */ public function __construct( \Magento\Directory\Model\CurrencyFactory $currencyFactory, @@ -68,12 +67,10 @@ public function __construct( \Magento\Framework\App\Config\ScopeConfigInterface $config, \Magento\Framework\Locale\FormatInterface $localeFormat, \Magento\Catalog\Model\Product\Type $catalogProductType, - GroupManagementInterface $groupManagement, - \Magento\Framework\Model\Entity\MetadataPool $metadataPool + GroupManagementInterface $groupManagement ) { $this->_catalogProductType = $catalogProductType; $this->_groupManagement = $groupManagement; - $this->metadataPool = $metadataPool; parent::__construct($currencyFactory, $storeManager, $catalogData, $config, $localeFormat); } @@ -282,7 +279,7 @@ public function afterLoad($object) } $data = $this->_getResource()->loadPriceData( - $object->getData($this->metadataPool->getMetadata(ProductInterface::class)->getLinkField()), + $object->getData($this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField()), $websiteId ); foreach ($data as $k => $v) { @@ -392,7 +389,7 @@ public function afterSave($object) $update = array_intersect_key($new, $old); $isChanged = false; - $productId = $object->getData($this->metadataPool->getMetadata(ProductInterface::class)->getLinkField()); + $productId = $object->getData($this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField()); if (!empty($delete)) { foreach ($delete as $data) { @@ -405,7 +402,7 @@ public function afterSave($object) foreach ($insert as $data) { $price = new \Magento\Framework\DataObject($data); $price->setData( - $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(), + $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(), $productId ); $this->_getResource()->savePriceData($price); @@ -464,4 +461,16 @@ public function getResource() { return $this->_getResource(); } + + /** + * @return \Magento\Framework\Model\Entity\MetadataPool + */ + private function getMetadataPool() + { + if (null === $this->metadataPool) { + $this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Framework\Model\Entity\MetadataPool'); + } + return $this->metadataPool; + } } diff --git a/app/code/Magento/Catalog/Model/Product/Attribute/Backend/Tierprice.php b/app/code/Magento/Catalog/Model/Product/Attribute/Backend/Tierprice.php index a2b7f41c62432..fc0fc07d4d524 100644 --- a/app/code/Magento/Catalog/Model/Product/Attribute/Backend/Tierprice.php +++ b/app/code/Magento/Catalog/Model/Product/Attribute/Backend/Tierprice.php @@ -28,7 +28,6 @@ class Tierprice extends \Magento\Catalog\Model\Product\Attribute\Backend\GroupPr * @param \Magento\Framework\Locale\FormatInterface $localeFormat * @param \Magento\Catalog\Model\Product\Type $catalogProductType * @param \Magento\Customer\Api\GroupManagementInterface $groupManagement - * @param \Magento\Framework\Model\Entity\MetadataPool $metadataPool * @param \Magento\Catalog\Model\ResourceModel\Product\Attribute\Backend\Tierprice $productAttributeTierprice */ public function __construct( @@ -39,7 +38,6 @@ public function __construct( \Magento\Framework\Locale\FormatInterface $localeFormat, \Magento\Catalog\Model\Product\Type $catalogProductType, \Magento\Customer\Api\GroupManagementInterface $groupManagement, - \Magento\Framework\Model\Entity\MetadataPool $metadataPool, \Magento\Catalog\Model\ResourceModel\Product\Attribute\Backend\Tierprice $productAttributeTierprice ) { $this->_productAttributeBackendTierprice = $productAttributeTierprice; @@ -50,8 +48,7 @@ public function __construct( $config, $localeFormat, $catalogProductType, - $groupManagement, - $metadataPool + $groupManagement ); } diff --git a/app/code/Magento/Catalog/Model/Product/Attribute/Repository.php b/app/code/Magento/Catalog/Model/Product/Attribute/Repository.php index c933d266e1c2a..3d5e49985d27b 100644 --- a/app/code/Magento/Catalog/Model/Product/Attribute/Repository.php +++ b/app/code/Magento/Catalog/Model/Product/Attribute/Repository.php @@ -62,7 +62,6 @@ class Repository implements \Magento\Catalog\Api\ProductAttributeRepositoryInter * @param \Magento\Eav\Model\Config $eavConfig * @param \Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\ValidatorFactory $validatorFactory * @param \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder - * @param \Magento\Catalog\Api\ProductAttributeOptionManagementInterface $optionManagement */ public function __construct( \Magento\Catalog\Model\ResourceModel\Attribute $attributeResource, @@ -71,8 +70,7 @@ public function __construct( \Magento\Eav\Api\AttributeRepositoryInterface $eavAttributeRepository, \Magento\Eav\Model\Config $eavConfig, \Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\ValidatorFactory $validatorFactory, - \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder, - \Magento\Catalog\Api\ProductAttributeOptionManagementInterface $optionManagement + \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder ) { $this->attributeResource = $attributeResource; $this->productHelper = $productHelper; @@ -81,7 +79,6 @@ public function __construct( $this->eavConfig = $eavConfig; $this->inputtypeValidatorFactory = $validatorFactory; $this->searchCriteriaBuilder = $searchCriteriaBuilder; - $this->optionManagement = $optionManagement; } /** @@ -180,7 +177,7 @@ public function save(\Magento\Catalog\Api\Data\ProductAttributeInterface $attrib } $this->attributeResource->save($attribute); foreach ($attribute->getOptions() as $option) { - $this->optionManagement->add($attribute->getAttributeCode(), $option); + $this->getOptionManagement()->add($attribute->getAttributeCode(), $option); } return $this->get($attribute->getAttributeCode()); } @@ -260,4 +257,16 @@ protected function validateFrontendInput($frontendInput) throw InputException::invalidFieldValue('frontend_input', $frontendInput); } } + + /** + * @return \Magento\Catalog\Api\ProductAttributeOptionManagementInterface + */ + private function getOptionManagement() + { + if (null === $this->optionManagement) { + $this->optionManagement = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Catalog\Api\ProductAttributeOptionManagementInterface'); + } + return $this->optionManagement; + } } diff --git a/app/code/Magento/Catalog/Model/Product/Copier.php b/app/code/Magento/Catalog/Model/Product/Copier.php index 4d58828b33fd9..fb40cc75cdb35 100644 --- a/app/code/Magento/Catalog/Model/Product/Copier.php +++ b/app/code/Magento/Catalog/Model/Product/Copier.php @@ -27,16 +27,13 @@ class Copier /** * @param CopyConstructorInterface $copyConstructor * @param \Magento\Catalog\Model\ProductFactory $productFactory - * @param Option\Repository $optionRepository */ public function __construct( CopyConstructorInterface $copyConstructor, - \Magento\Catalog\Model\ProductFactory $productFactory, - \Magento\Catalog\Model\Product\Option\Repository $optionRepository + \Magento\Catalog\Model\ProductFactory $productFactory ) { $this->productFactory = $productFactory; $this->copyConstructor = $copyConstructor; - $this->optionRepository = $optionRepository; } /** @@ -75,8 +72,20 @@ public function copy(\Magento\Catalog\Model\Product $product) } } while (!$isDuplicateSaved); - $this->optionRepository->duplicate($product, $duplicate); + $this->getOptionRepository()->duplicate($product, $duplicate); $product->getResource()->duplicate($product->getEntityId(), $duplicate->getEntityId()); return $duplicate; } + + /** + * @return Option\Repository + */ + private function getOptionRepository() + { + if (null === $this->optionRepository) { + $this->optionRepository = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Catalog\Model\Product\Option\Repository'); + } + return $this->optionRepository; + } } diff --git a/app/code/Magento/Catalog/Model/Product/Link.php b/app/code/Magento/Catalog/Model/Product/Link.php index 35c11dc08f547..00fe7765a1216 100644 --- a/app/code/Magento/Catalog/Model/Product/Link.php +++ b/app/code/Magento/Catalog/Model/Product/Link.php @@ -21,6 +21,7 @@ * @method \Magento\Catalog\Model\Product\Link setLinkTypeId(int $value) * * @author Magento Core Team + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class Link extends \Magento\Framework\Model\AbstractModel { @@ -54,12 +55,17 @@ class Link extends \Magento\Framework\Model\AbstractModel */ protected $saveProductLinks; + /** + * @var \Magento\CatalogInventory\Helper\Stock + */ + protected $stockHelper; + /** * @param \Magento\Framework\Model\Context $context * @param \Magento\Framework\Registry $registry * @param \Magento\Catalog\Model\ResourceModel\Product\Link\CollectionFactory $linkCollectionFactory * @param \Magento\Catalog\Model\ResourceModel\Product\Link\Product\CollectionFactory $productCollectionFactory - * @param \Magento\Catalog\Model\Product\Link\SaveHandler $saveProductLinks + * @param \Magento\CatalogInventory\Helper\Stock $stockHelper * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection * @param array $data @@ -69,14 +75,14 @@ public function __construct( \Magento\Framework\Registry $registry, \Magento\Catalog\Model\ResourceModel\Product\Link\CollectionFactory $linkCollectionFactory, \Magento\Catalog\Model\ResourceModel\Product\Link\Product\CollectionFactory $productCollectionFactory, - \Magento\Catalog\Model\Product\Link\SaveHandler $saveProductLinks, + \Magento\CatalogInventory\Helper\Stock $stockHelper, \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null, array $data = [] ) { $this->_linkCollectionFactory = $linkCollectionFactory; $this->_productCollectionFactory = $productCollectionFactory; - $this->saveProductLinks = $saveProductLinks; + $this->stockHelper = $stockHelper; parent::__construct($context, $registry, $resource, $resourceCollection, $data); } @@ -174,7 +180,19 @@ public function getAttributes($type = null) */ public function saveProductRelations($product) { - $this->saveProductLinks->execute(\Magento\Catalog\Api\Data\ProductInterface::class, $product); + $this->getProductLinkSaveHandler()->execute(\Magento\Catalog\Api\Data\ProductInterface::class, $product); return $this; } + + /** + * @return Link\SaveHandler + */ + private function getProductLinkSaveHandler() + { + if (null === $this->saveProductLinks) { + $this->saveProductLinks = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Catalog\Model\Product\Link\SaveHandler'); + } + return $this->saveProductLinks; + } } diff --git a/app/code/Magento/Catalog/Model/Product/Media/Config.php b/app/code/Magento/Catalog/Model/Product/Media/Config.php index 19ed7c7e997f7..273a3a53cfc42 100644 --- a/app/code/Magento/Catalog/Model/Product/Media/Config.php +++ b/app/code/Magento/Catalog/Model/Product/Media/Config.php @@ -32,15 +32,10 @@ class Config implements ConfigInterface /** * @param StoreManagerInterface $storeManager - * @param Attribute $attributeHelper */ - public function __construct( - StoreManagerInterface $storeManager, - Attribute $attributeHelper - ) + public function __construct(StoreManagerInterface $storeManager) { $this->storeManager = $storeManager; - $this->attributeHelper = $attributeHelper; } /** @@ -175,6 +170,18 @@ protected function _prepareFile($file) */ public function getMediaAttributeCodes() { - return $this->attributeHelper->getAttributeCodesByFrontendType('media_image'); + return $this->getAttributeHelper()->getAttributeCodesByFrontendType('media_image'); + } + + /** + * @return Attribute + */ + private function getAttributeHelper() + { + if (null === $this->attributeHelper) { + $this->attributeHelper = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Eav\Model\Entity\Attribute'); + } + return $this->attributeHelper; } } diff --git a/app/code/Magento/Catalog/Model/Product/Option.php b/app/code/Magento/Catalog/Model/Product/Option.php index 6b7eac3a18408..0733f2ba5380a 100644 --- a/app/code/Magento/Catalog/Model/Product/Option.php +++ b/app/code/Magento/Catalog/Model/Product/Option.php @@ -119,6 +119,7 @@ class Option extends AbstractExtensibleModel implements ProductCustomOptionInter * @var Option\Validator\Pool */ protected $validatorPool; + /** * @var MetadataPool */ @@ -133,8 +134,6 @@ class Option extends AbstractExtensibleModel implements ProductCustomOptionInter * @param Option\Type\Factory $optionFactory * @param \Magento\Framework\Stdlib\StringUtils $string * @param Option\Validator\Pool $validatorPool - * @param \Magento\Catalog\Model\Product\Option\Repository $optionRepository , - * @param MetadataPool $metadataPool * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection * @param array $data @@ -149,8 +148,6 @@ public function __construct( \Magento\Catalog\Model\Product\Option\Type\Factory $optionFactory, \Magento\Framework\Stdlib\StringUtils $string, Option\Validator\Pool $validatorPool, - \Magento\Catalog\Model\Product\Option\Repository $optionRepository, - MetadataPool $metadataPool, \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null, array $data = [] @@ -159,8 +156,6 @@ public function __construct( $this->optionTypeFactory = $optionFactory; $this->validatorPool = $validatorPool; $this->string = $string; - $this->optionRepository = $optionRepository; - $this->metadataPool = $metadataPool; parent::__construct( $context, $registry, @@ -469,7 +464,7 @@ public function deleteTitles($optionId) */ public function getProductOptions(Product $product) { - return $this->optionRepository->getProductOptions($product, $this->getAddRequiredFilter()); + return $this->getOptionRepository()->getProductOptions($product, $this->getAddRequiredFilter()); } /** @@ -676,6 +671,7 @@ public function getImageSizeY() { return $this->getData(self::KEY_IMAGE_SIZE_Y); } + /** * Set product SKU * @@ -840,7 +836,7 @@ public function getProductOptionCollection(Product $product) $collection = clone $this->getCollection(); $collection->addFieldToFilter( 'product_id', - $product->getData($this->metadataPool->getMetadata(ProductInterface::class)->getLinkField()) + $product->getData($this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField()) )->addTitleToResult( $product->getStoreId() )->addPriceToResult( @@ -872,5 +868,29 @@ public function setExtensionAttributes( ) { return $this->_setExtensionAttributes($extensionAttributes); } + + /** + * @return Option\Repository + */ + private function getOptionRepository() + { + if (null === $this->optionRepository) { + $this->optionRepository = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Catalog\Model\Product\Option\Repository'); + } + return $this->optionRepository; + } + + /** + * @return \Magento\Framework\Model\Entity\MetadataPool + */ + private function getMetadataPool() + { + if (null === $this->metadataPool) { + $this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Framework\Model\Entity\MetadataPool'); + } + return $this->metadataPool; + } //@codeCoverageIgnoreEnd } diff --git a/app/code/Magento/Catalog/Model/Product/Option/Converter.php b/app/code/Magento/Catalog/Model/Product/Option/Converter.php new file mode 100644 index 0000000000000..a21f404a543cf --- /dev/null +++ b/app/code/Magento/Catalog/Model/Product/Option/Converter.php @@ -0,0 +1,30 @@ +getData(); + $values = $option->getValues(); + $valuesData = []; + if (!empty($values)) { + foreach ($values as $key => $value) { + $valuesData[$key] = $value->getData(); + } + } + $optionData['values'] = $valuesData; + return $optionData; + } +} diff --git a/app/code/Magento/Catalog/Model/Product/Option/Repository.php b/app/code/Magento/Catalog/Model/Product/Option/Repository.php index 6dbae4464233c..1fe6966c1a51a 100644 --- a/app/code/Magento/Catalog/Model/Product/Option/Repository.php +++ b/app/code/Magento/Catalog/Model/Product/Option/Repository.php @@ -43,24 +43,24 @@ class Repository implements \Magento\Catalog\Api\ProductCustomOptionRepositoryIn protected $metadataPool; /** + * @var Converter + */ + protected $converter; + + /** + * Repository constructor. * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository * @param \Magento\Catalog\Model\ResourceModel\Product\Option $optionResource - * @param \Magento\Catalog\Model\Product\OptionFactory $optionFactory - * @param \Magento\Catalog\Model\ResourceModel\Product\Option\CollectionFactory $collectionFactory - * @param MetadataPool $metadataPool + * @param Converter $converter */ public function __construct( \Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Magento\Catalog\Model\ResourceModel\Product\Option $optionResource, - \Magento\Catalog\Model\Product\OptionFactory $optionFactory, - \Magento\Catalog\Model\ResourceModel\Product\Option\CollectionFactory $collectionFactory, - MetadataPool $metadataPool + \Magento\Catalog\Model\Product\Option\Converter $converter ) { $this->productRepository = $productRepository; $this->optionResource = $optionResource; - $this->optionFactory = $optionFactory; - $this->collectionFactory = $collectionFactory; - $this->metadataPool = $metadataPool; + $this->converter = $converter; } /** @@ -77,7 +77,7 @@ public function getList($sku) */ public function getProductOptions(ProductInterface $product, $requiredOnly = false) { - return $this->collectionFactory->create()->getProductOptions( + return $this->getCollectionFactory()->create()->getProductOptions( $product->getEntityId(), $product->getStoreId(), $requiredOnly @@ -114,7 +114,7 @@ public function duplicate( \Magento\Catalog\Api\Data\ProductInterface $duplicate ) { return $this->optionResource->duplicate( - $this->optionFactory->create([]), + $this->getOptionFactory()->create([]), $product->getId(), $duplicate->getId() ); @@ -126,7 +126,7 @@ public function duplicate( public function save(\Magento\Catalog\Api\Data\ProductCustomOptionInterface $option) { $product = $this->productRepository->get($option->getProductSku()); - $metadata = $this->metadataPool->getMetadata(ProductInterface::class); + $metadata = $this->getMetadataPool()->getMetadata(ProductInterface::class); $option->setData('product_id', $product->getData($metadata->getLinkField())); $option->setOptionId(null); $option->save(); @@ -182,4 +182,40 @@ protected function markRemovedValues($newValues, $originalValues) return $newValues; } + + /** + * @return \Magento\Catalog\Model\Product\OptionFactory + */ + private function getOptionFactory() + { + if (null === $this->optionFactory) { + $this->optionFactory = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Catalog\Model\Product\OptionFactory'); + } + return $this->optionFactory; + } + + /** + * @return \Magento\Catalog\Model\ResourceModel\Product\Option\CollectionFactory + */ + private function getCollectionFactory() + { + if (null === $this->collectionFactory) { + $this->collectionFactory = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Catalog\Model\ResourceModel\Product\Option\CollectionFactory'); + } + return $this->collectionFactory; + } + + /** + * @return \Magento\Framework\Model\Entity\MetadataPool + */ + private function getMetadataPool() + { + if (null === $this->metadataPool) { + $this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Framework\Model\Entity\MetadataPool'); + } + return $this->metadataPool; + } } diff --git a/app/code/Magento/Catalog/Model/ProductLink/Repository.php b/app/code/Magento/Catalog/Model/ProductLink/Repository.php index 7cc5895dabb7d..ed7fff6743f3b 100644 --- a/app/code/Magento/Catalog/Model/ProductLink/Repository.php +++ b/app/code/Magento/Catalog/Model/ProductLink/Repository.php @@ -7,7 +7,6 @@ namespace Magento\Catalog\Model\ProductLink; use Magento\Catalog\Api\Data\ProductInterface; -use Magento\Catalog\Api\Data\ProductLinkExtension; use Magento\Catalog\Api\Data\ProductLinkInterfaceFactory; use Magento\Catalog\Api\Data\ProductLinkExtensionFactory; use Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks as LinksInitializer; @@ -85,12 +84,6 @@ class Repository implements \Magento\Catalog\Api\ProductLinkRepositoryInterface * @param LinksInitializer $linkInitializer * @param Management $linkManagement * @param \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor - * @param \Magento\Catalog\Model\ResourceModel\Product\Relation $catalogProductRelation - * @param \Magento\Catalog\Model\ResourceModel\Product\Link $linkResource - * @param LinkTypeProvider $linkTypeProvider - * @param ProductLinkInterfaceFactory $productLinkFactory - * @param ProductLinkExtensionFactory $productLinkExtensionFactory - * @param MetadataPool $metadataPool * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct( @@ -98,25 +91,13 @@ public function __construct( \Magento\Catalog\Model\ProductLink\CollectionProvider $entityCollectionProvider, \Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks $linkInitializer, \Magento\Catalog\Model\ProductLink\Management $linkManagement, - \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor, - \Magento\Catalog\Model\ResourceModel\Product\Relation $catalogProductRelation, - \Magento\Catalog\Model\ResourceModel\Product\Link $linkResource, - LinkTypeProvider $linkTypeProvider, - ProductLinkInterfaceFactory $productLinkFactory, - ProductLinkExtensionFactory $productLinkExtensionFactory, - MetadataPool $metadataPool + \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor ) { $this->productRepository = $productRepository; $this->entityCollectionProvider = $entityCollectionProvider; $this->linkInitializer = $linkInitializer; $this->linkManagement = $linkManagement; $this->dataObjectProcessor = $dataObjectProcessor; - $this->catalogProductRelation = $catalogProductRelation; - $this->linkResource = $linkResource; - $this->linkTypeProvider = $linkTypeProvider; - $this->productLinkFactory = $productLinkFactory; - $this->productLinkExtensionFactory = $productLinkExtensionFactory; - $this->metadataPool = $metadataPool; } /** @@ -141,10 +122,10 @@ public function save(\Magento\Catalog\Api\Data\ProductLinkInterface $entity) $links[$linkedProduct->getId()] = $data; try { - $linkTypesToId = $this->linkTypeProvider->getLinkTypes(); - $productData = $this->metadataPool->getHydrator(ProductInterface::class)->extract($product); - $this->linkResource->saveProductLinks( - $productData[$this->metadataPool->getMetadata(ProductInterface::class)->getLinkField()], + $linkTypesToId = $this->getLinkTypeProvider()->getLinkTypes(); + $productData = $this->getMetadataPool()->getHydrator(ProductInterface::class)->extract($product); + $this->getLinkResource()->saveProductLinks( + $productData[$this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField()], $links, $linkTypesToId[$entity->getLinkType()] ); @@ -163,12 +144,12 @@ public function save(\Magento\Catalog\Api\Data\ProductLinkInterface $entity) public function getList(\Magento\Catalog\Api\Data\ProductInterface $product) { $output = []; - $linkTypes = $this->linkTypeProvider->getLinkTypes(); + $linkTypes = $this->getLinkTypeProvider()->getLinkTypes(); foreach (array_keys($linkTypes) as $linkTypeName) { $collection = $this->entityCollectionProvider->getCollection($product, $linkTypeName); foreach ($collection as $item) { /** @var \Magento\Catalog\Api\Data\ProductLinkInterface $productLink */ - $productLink = $this->productLinkFactory->create(); + $productLink = $this->getProductLinkFactory()->create(); $productLink->setSku($product->getSku()) ->setLinkType($linkTypeName) ->setLinkedProductSku($item['sku']) @@ -177,7 +158,7 @@ public function getList(\Magento\Catalog\Api\Data\ProductInterface $product) if (isset($item['custom_attributes'])) { $productLinkExtension = $productLink->getExtensionAttributes(); if ($productLinkExtension === null) { - $productLinkExtension = $this->productLinkExtensionFactory->create(); + $productLinkExtension = $this->getProductLinkExtensionFactory()->create(); } foreach ($item['custom_attributes'] as $option) { $name = $option['attribute_code']; @@ -203,10 +184,10 @@ public function delete(\Magento\Catalog\Api\Data\ProductLinkInterface $entity) { $linkedProduct = $this->productRepository->get($entity->getLinkedProductSku()); $product = $this->productRepository->get($entity->getSku()); - $linkTypesToId = $this->linkTypeProvider->getLinkTypes(); - $productData = $this->metadataPool->getHydrator(ProductInterface::class)->extract($product); - $linkId = $this->linkResource->getProductLinkId( - $productData[$this->metadataPool->getMetadata(ProductInterface::class)->getLinkField()], + $linkTypesToId = $this->getLinkTypeProvider()->getLinkTypes(); + $productData = $this->getMetadataPool()->getHydrator(ProductInterface::class)->extract($product); + $linkId = $this->getLinkResource()->getProductLinkId( + $productData[$this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField()], $linkedProduct->getId(), $linkTypesToId[$entity->getLinkType()] ); @@ -222,7 +203,7 @@ public function delete(\Magento\Catalog\Api\Data\ProductLinkInterface $entity) } try { - $this->linkResource->deleteProductLink($linkId); + $this->getLinkResource()->deleteProductLink($linkId); } catch (\Exception $exception) { throw new CouldNotSaveException(__('Invalid data provided for linked products')); } @@ -252,4 +233,64 @@ public function deleteById($sku, $type, $linkedProductSku) ) ); } + + /** + * @return \Magento\Catalog\Model\ResourceModel\Product\Link + */ + private function getLinkResource() + { + if (null === $this->linkResource) { + $this->linkResource = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Catalog\Model\ResourceModel\Product\Link'); + } + return $this->linkResource; + } + + /** + * @return LinkTypeProvider + */ + private function getLinkTypeProvider() + { + if (null === $this->linkTypeProvider) { + $this->linkTypeProvider = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Catalog\Model\Product\LinkTypeProvider'); + } + return $this->linkTypeProvider; + } + + /** + * @return ProductLinkInterfaceFactory + */ + private function getProductLinkFactory() + { + if (null === $this->productLinkFactory) { + $this->productLinkFactory = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Catalog\Api\Data\ProductLinkInterfaceFactory'); + } + return $this->productLinkFactory; + } + + /** + * @return ProductLinkExtensionFactory + */ + private function getProductLinkExtensionFactory() + { + if (null === $this->productLinkExtensionFactory) { + $this->productLinkExtensionFactory = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Catalog\Api\Data\ProductLinkExtensionFactory'); + } + return $this->productLinkExtensionFactory; + } + + /** + * @return \Magento\Framework\Model\Entity\MetadataPool + */ + private function getMetadataPool() + { + if (null === $this->metadataPool) { + $this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Framework\Model\Entity\MetadataPool'); + } + return $this->metadataPool; + } } diff --git a/app/code/Magento/Catalog/Model/ProductRepository.php b/app/code/Magento/Catalog/Model/ProductRepository.php index 3fd83b321ad0a..e8b5dea903b9e 100644 --- a/app/code/Magento/Catalog/Model/ProductRepository.php +++ b/app/code/Magento/Catalog/Model/ProductRepository.php @@ -8,8 +8,10 @@ use Magento\Catalog\Api\Data\ProductInterface; use Magento\Catalog\Model\ResourceModel\Product\Collection; +use Magento\Catalog\Model\Product\Gallery\MimeTypeExtensionMap; use Magento\Framework\Api\Data\ImageContentInterface; use Magento\Framework\Api\Data\ImageContentInterfaceFactory; +use Magento\Framework\Api\ImageContentValidatorInterface; use Magento\Framework\Api\ImageProcessorInterface; use Magento\Framework\Api\SortOrder; use Magento\Framework\Exception\InputException; @@ -74,7 +76,17 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa */ protected $resourceModel; - /* + /** + * @var Product\Initialization\Helper\ProductLinks + */ + protected $linkInitializer; + + /** + * @var Product\LinkTypeProvider + */ + protected $linkTypeProvider; + + /** * @var \Magento\Store\Model\StoreManagerInterface */ protected $storeManager; @@ -89,6 +101,11 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa */ protected $metadataService; + /** + * @var \Magento\Framework\Api\ExtensibleDataObjectConverter + */ + protected $extensibleDataObjectConverter; + /** * @var \Magento\Framework\Filesystem */ @@ -115,6 +132,7 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa protected $mediaGalleryProcessor; /** + * ProductRepository constructor. * @param ProductFactory $productFactory * @param \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper $initializationHelper * @param \Magento\Catalog\Api\Data\ProductSearchResultsInterfaceFactory $searchResultsFactory @@ -128,12 +146,15 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa * @param \Magento\Framework\Api\FilterBuilder $filterBuilder * @param \Magento\Catalog\Api\ProductAttributeRepositoryInterface $metadataServiceInterface * @param \Magento\Framework\Api\ExtensibleDataObjectConverter $extensibleDataObjectConverter + * @param Product\Option\Converter $optionConverter * @param \Magento\Framework\Filesystem $fileSystem + * @param ImageContentValidatorInterface $contentValidator * @param ImageContentInterfaceFactory $contentFactory + * @param MimeTypeExtensionMap $mimeTypeExtensionMap * @param ImageProcessorInterface $imageProcessor * @param \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor - * @param Product\Gallery\Processor $mediaGalleryProcessor * @SuppressWarnings(PHPMD.ExcessiveParameterList) + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function __construct( ProductFactory $productFactory, @@ -149,11 +170,13 @@ public function __construct( \Magento\Framework\Api\FilterBuilder $filterBuilder, \Magento\Catalog\Api\ProductAttributeRepositoryInterface $metadataServiceInterface, \Magento\Framework\Api\ExtensibleDataObjectConverter $extensibleDataObjectConverter, + \Magento\Catalog\Model\Product\Option\Converter $optionConverter, \Magento\Framework\Filesystem $fileSystem, + ImageContentValidatorInterface $contentValidator, ImageContentInterfaceFactory $contentFactory, + MimeTypeExtensionMap $mimeTypeExtensionMap, ImageProcessorInterface $imageProcessor, - \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor, - \Magento\Catalog\Model\Product\Gallery\Processor $mediaGalleryProcessor + \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor ) { $this->productFactory = $productFactory; $this->collectionFactory = $collectionFactory; @@ -172,7 +195,6 @@ public function __construct( $this->contentFactory = $contentFactory; $this->imageProcessor = $imageProcessor; $this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor; - $this->mediaGalleryProcessor = $mediaGalleryProcessor; } /** @@ -317,7 +339,7 @@ protected function processNewMediaGalleryEntry( throw new StateException(__('Requested product does not support images.')); } - $imageFileUri = $this->mediaGalleryProcessor->addImage( + $imageFileUri = $this->getMediaGalleryProcessor()->addImage( $product, $tmpFilePath, isset($newEntry['types']) ? $newEntry['types'] : [], @@ -325,7 +347,7 @@ protected function processNewMediaGalleryEntry( isset($newEntry['disabled']) ? $newEntry['disabled'] : true ); // Update additional fields that are still empty after addImage call - $this->mediaGalleryProcessor->updateImage( + $this->getMediaGalleryProcessor()->updateImage( $product, $imageFileUri, [ @@ -432,12 +454,12 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE $newEntries = $mediaGalleryEntries; } - $this->mediaGalleryProcessor->clearMediaAttribute($product, array_keys($product->getMediaAttributes())); + $this->getMediaGalleryProcessor()->clearMediaAttribute($product, array_keys($product->getMediaAttributes())); $images = $product->getMediaGallery('images'); if ($images) { foreach ($images as $image) { if (!isset($image['removed']) && !empty($image['types'])) { - $this->mediaGalleryProcessor->setMediaAttribute($product, $image['types'], $image['file']); + $this->getMediaGalleryProcessor()->setMediaAttribute($product, $image['types'], $image['file']); } } } @@ -644,4 +666,16 @@ public function cleanCache() $this->instances = null; $this->instancesById = null; } + + /** + * @return Product\Gallery\Processor + */ + private function getMediaGalleryProcessor() + { + if (null === $this->mediaGalleryProcessor) { + $this->mediaGalleryProcessor = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Catalog\Model\Product\Gallery\Processor'); + } + return $this->mediaGalleryProcessor; + } } diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Attribute.php b/app/code/Magento/Catalog/Model/ResourceModel/Attribute.php index 64eb4e5427160..400b796080313 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Attribute.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Attribute.php @@ -38,7 +38,6 @@ class Attribute extends \Magento\Eav\Model\ResourceModel\Entity\Attribute * @param \Magento\Eav\Model\ResourceModel\Entity\Type $eavEntityType * @param \Magento\Eav\Model\Config $eavConfig * @param LockValidatorInterface $lockValidator - * @param \Magento\Framework\Model\Entity\MetadataPool $metadataPool * @param string $connectionName */ public function __construct( @@ -47,12 +46,10 @@ public function __construct( \Magento\Eav\Model\ResourceModel\Entity\Type $eavEntityType, \Magento\Eav\Model\Config $eavConfig, LockValidatorInterface $lockValidator, - \Magento\Framework\Model\Entity\MetadataPool $metadataPool, $connectionName = null ) { $this->attrLockValidator = $lockValidator; $this->_eavConfig = $eavConfig; - $this->metadataPool = $metadataPool; parent::__construct($context, $storeManager, $eavEntityType, $connectionName); } @@ -113,11 +110,11 @@ protected function _clearUselessAttributeValues(\Magento\Framework\Model\Abstrac /** * Delete entity * - * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $object + * @param \Magento\Framework\Model\AbstractModel $object * @return $this * @throws \Magento\Framework\Exception\LocalizedException */ - public function deleteEntity(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $object) + public function deleteEntity(\Magento\Framework\Model\AbstractModel $object) { if (!$object->getEntityAttributeId()) { return $this; @@ -140,7 +137,7 @@ public function deleteEntity(\Magento\Eav\Model\Entity\Attribute\AbstractAttribu $backendTable = $attribute->getBackend()->getTable(); if ($backendTable) { - $linkField = $this->metadataPool + $linkField = $this->getMetadataPool() ->getMetadata(ProductInterface::class) ->getLinkField(); @@ -165,4 +162,16 @@ public function deleteEntity(\Magento\Eav\Model\Entity\Attribute\AbstractAttribu return $this; } + + /** + * @return \Magento\Framework\Model\Entity\MetadataPool + */ + private function getMetadataPool() + { + if (null === $this->metadataPool) { + $this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Framework\Model\Entity\MetadataPool'); + } + return $this->metadataPool; + } } diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Category.php b/app/code/Magento/Catalog/Model/ResourceModel/Category.php index e8bc1d1e11b45..56da0aaa9199e 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Category.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Category.php @@ -86,8 +86,6 @@ class Category extends AbstractResource * @param \Magento\Framework\Event\ManagerInterface $eventManager * @param Category\TreeFactory $categoryTreeFactory * @param Category\CollectionFactory $categoryCollectionFactory - * @param EntityManager $entityManager - * @param Category\AggregateCount $aggregateCount * @param array $data */ public function __construct( @@ -97,8 +95,6 @@ public function __construct( \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Catalog\Model\ResourceModel\Category\TreeFactory $categoryTreeFactory, \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory, - Category\AggregateCount $aggregateCount, - EntityManager $entityManager, $data = [] ) { parent::__construct( @@ -110,9 +106,7 @@ public function __construct( $this->_categoryTreeFactory = $categoryTreeFactory; $this->_categoryCollectionFactory = $categoryCollectionFactory; $this->_eventManager = $eventManager; - $this->entityManager = $entityManager; $this->connectionName = 'catalog'; - $this->aggregateCount = $aggregateCount; } /** @@ -191,7 +185,7 @@ protected function _getTree() protected function _beforeDelete(\Magento\Framework\DataObject $object) { parent::_beforeDelete($object); - $this->aggregateCount->processDelete($object); + $this->getAggregateCount()->processDelete($object); $this->deleteChildren($object); } @@ -999,8 +993,8 @@ public function load($object, $entityId, $attributes = []) { $this->_attributes = []; $this->loadAttributesMetadata($attributes); - $object = $this->entityManager->load(CategoryInterface::class, $object, $entityId); - if (!$this->entityManager->has(\Magento\Catalog\Api\Data\CategoryInterface::class, $entityId)) { + $object = $this->getEntityManager()->load(CategoryInterface::class, $object, $entityId); + if (!$this->getEntityManager()->has(\Magento\Catalog\Api\Data\CategoryInterface::class, $entityId)) { $object->isObjectNew(true); } return $this; @@ -1011,7 +1005,7 @@ public function load($object, $entityId, $attributes = []) */ public function delete($object) { - $this->entityManager->delete(CategoryInterface::class, $object); + $this->getEntityManager()->delete(CategoryInterface::class, $object); $this->_eventManager->dispatch( 'catalog_category_delete_after_done', ['product' => $object] @@ -1028,7 +1022,31 @@ public function delete($object) */ public function save(\Magento\Framework\Model\AbstractModel $object) { - $this->entityManager->save(CategoryInterface::class, $object); + $this->getEntityManager()->save(CategoryInterface::class, $object); return $this; } + + /** + * @return EntityManager + */ + private function getEntityManager() + { + if (null === $this->entityManager) { + $this->entityManager = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Framework\Model\EntityManager'); + } + return $this->entityManager; + } + + /** + * @return Category\AggregateCount + */ + private function getAggregateCount() + { + if (null === $this->aggregateCount) { + $this->aggregateCount = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Catalog\Model\ResourceModel\Category\AggregateCount'); + } + return $this->aggregateCount; + } } diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php b/app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php index d537d61e9d522..0445d442e7158 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php @@ -105,7 +105,6 @@ class Tree extends Dbp * @param \Magento\Framework\Event\ManagerInterface $eventManager * @param \Magento\Catalog\Model\Attribute\Config $attributeConfig * @param Collection\Factory $collectionFactory - * @param MetadataPool $metadataPool */ public function __construct( \Magento\Catalog\Model\ResourceModel\Category $catalogCategory, @@ -114,8 +113,7 @@ public function __construct( \Magento\Framework\App\ResourceConnection $resource, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Catalog\Model\Attribute\Config $attributeConfig, - \Magento\Catalog\Model\ResourceModel\Category\Collection\Factory $collectionFactory, - MetadataPool $metadataPool + \Magento\Catalog\Model\ResourceModel\Category\Collection\Factory $collectionFactory ) { $this->_catalogCategory = $catalogCategory; $this->_cache = $cache; @@ -134,7 +132,6 @@ public function __construct( $this->_eventManager = $eventManager; $this->_attributeConfig = $attributeConfig; $this->_collectionFactory = $collectionFactory; - $this->metadataPool = $metadataPool; } /** @@ -308,7 +305,7 @@ protected function _getDisabledIds($collection, $allIds) */ protected function _getInactiveItemIds($collection, $storeId) { - $linkField = $this->metadataPool->getMetadata(CategoryInterface::class)->getLinkField(); + $linkField = $this->getMetadataPool()->getMetadata(CategoryInterface::class)->getLinkField(); $intTable = $this->_coreResource->getTableName('catalog_category_entity_int'); $select = $collection->getAllIdsSql() @@ -582,7 +579,7 @@ protected function _updateAnchorProductCount(&$data) */ protected function _createCollectionDataSelect($sorted = true, $optionalAttributes = []) { - $meta = $this->metadataPool->getMetadata(CategoryInterface::class); + $meta = $this->getMetadataPool()->getMetadata(CategoryInterface::class); $linkField = $meta->getLinkField(); $select = $this->_getDefaultCollection($sorted ? $this->_orderField : false)->getSelect(); @@ -678,4 +675,16 @@ public function getExistingCategoryIdsBySpecifiedIds($ids) $select = $this->_conn->select()->from($this->_table, ['entity_id'])->where('entity_id IN (?)', $ids); return $this->_conn->fetchCol($select); } + + /** + * @return \Magento\Framework\Model\Entity\MetadataPool + */ + private function getMetadataPool() + { + if (null === $this->metadataPool) { + $this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Framework\Model\Entity\MetadataPool'); + } + return $this->metadataPool; + } } diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product.php b/app/code/Magento/Catalog/Model/ResourceModel/Product.php index 205e391ef9987..995a79ddb0583 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product.php @@ -83,7 +83,6 @@ class Product extends AbstractResource * @param \Magento\Eav\Model\Entity\Attribute\SetFactory $setFactory * @param \Magento\Eav\Model\Entity\TypeFactory $typeFactory * @param \Magento\Catalog\Model\Product\Attribute\DefaultAttributes $defaultAttributes - * @param \Magento\Framework\Model\EntityManager $entityManager * @param array $data * * @SuppressWarnings(PHPMD.ExcessiveParameterList) @@ -98,7 +97,6 @@ public function __construct( \Magento\Eav\Model\Entity\Attribute\SetFactory $setFactory, \Magento\Eav\Model\Entity\TypeFactory $typeFactory, \Magento\Catalog\Model\Product\Attribute\DefaultAttributes $defaultAttributes, - \Magento\Framework\Model\EntityManager $entityManager, $data = [] ) { $this->_categoryCollectionFactory = $categoryCollectionFactory; @@ -106,7 +104,6 @@ public function __construct( $this->eventManager = $eventManager; $this->setFactory = $setFactory; $this->typeFactory = $typeFactory; - $this->entityManager = $entityManager; $this->defaultAttributes = $defaultAttributes; parent::__construct( $context, @@ -302,7 +299,7 @@ protected function _afterSave(\Magento\Framework\DataObject $product) */ public function delete($object) { - $this->entityManager->delete(\Magento\Catalog\Api\Data\ProductInterface::class, $object); + $this->getEntityManager()->delete(\Magento\Catalog\Api\Data\ProductInterface::class, $object); $this->eventManager->dispatch( 'catalog_product_delete_after_done', ['product' => $object] @@ -654,7 +651,7 @@ public function validate($object) public function load($object, $entityId, $attributes = []) { $this->loadAttributesMetadata($attributes); - $this->entityManager->load(\Magento\Catalog\Api\Data\ProductInterface::class, $object, $entityId); + $this->getEntityManager()->load(\Magento\Catalog\Api\Data\ProductInterface::class, $object, $entityId); return $this; } @@ -694,7 +691,19 @@ protected function evaluateDelete($object, $id, $connection) */ public function save(\Magento\Framework\Model\AbstractModel $object) { - $this->entityManager->save(ProductInterface::class, $object); + $this->getEntityManager()->save(ProductInterface::class, $object); return $this; } + + /** + * @return \Magento\Framework\Model\EntityManager + */ + private function getEntityManager() + { + if (null === $this->entityManager) { + $this->entityManager = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Framework\Model\EntityManager'); + } + return $this->entityManager; + } } diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php index 98a4eb86ec4b6..5f1fc6d0e0202 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php @@ -263,7 +263,6 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac * @param \Magento\Customer\Model\Session $customerSession * @param \Magento\Framework\Stdlib\DateTime $dateTime * @param GroupManagementInterface $groupManagement - * @param \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation $productLimitation * @param \Magento\Framework\DB\Adapter\AdapterInterface $connection * * @SuppressWarnings(PHPMD.ExcessiveParameterList) @@ -288,7 +287,6 @@ public function __construct( \Magento\Customer\Model\Session $customerSession, \Magento\Framework\Stdlib\DateTime $dateTime, GroupManagementInterface $groupManagement, - \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation $productLimitation, \Magento\Framework\DB\Adapter\AdapterInterface $connection = null ) { $this->moduleManager = $moduleManager; @@ -301,7 +299,7 @@ public function __construct( $this->_resourceHelper = $resourceHelper; $this->dateTime = $dateTime; $this->_groupManagement = $groupManagement; - $this->_productLimitationFilters = $productLimitation; + $this->_productLimitationFilters = $this->createLimitationFilters(); parent::__construct( $entityFactory, $logger, @@ -2264,4 +2262,13 @@ public function getPricesCount() return $this->_pricesCount; } + + /** + * @return Collection\ProductLimitation + */ + private function createLimitationFilters() + { + return \Magento\Framework\App\ObjectManager::getInstance() + ->create('Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation'); + } } diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php index 9edec39353fd9..49d32cb99a5f2 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php @@ -70,7 +70,6 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection * @param \Magento\Customer\Model\Session $customerSession * @param \Magento\Framework\Stdlib\DateTime $dateTime * @param \Magento\Customer\Api\GroupManagementInterface $groupManagement - * @param \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation $productLimitation * @param \Magento\Catalog\Model\ResourceModel\Product\Compare\Item $catalogProductCompareItem * @param \Magento\Catalog\Helper\Product\Compare $catalogProductCompare * @param \Magento\Framework\DB\Adapter\AdapterInterface $connection @@ -97,7 +96,6 @@ public function __construct( \Magento\Customer\Model\Session $customerSession, \Magento\Framework\Stdlib\DateTime $dateTime, \Magento\Customer\Api\GroupManagementInterface $groupManagement, - \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation $productLimitation, \Magento\Catalog\Model\ResourceModel\Product\Compare\Item $catalogProductCompareItem, \Magento\Catalog\Helper\Product\Compare $catalogProductCompare, \Magento\Framework\DB\Adapter\AdapterInterface $connection = null @@ -124,7 +122,6 @@ public function __construct( $customerSession, $dateTime, $groupManagement, - $productLimitation, $connection ); } diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/AbstractIndexer.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/AbstractIndexer.php index 230faaff89db8..b52ac661da560 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/AbstractIndexer.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/AbstractIndexer.php @@ -32,18 +32,15 @@ abstract class AbstractIndexer extends \Magento\Indexer\Model\ResourceModel\Abst * @param \Magento\Framework\Model\ResourceModel\Db\Context $context * @param \Magento\Framework\Indexer\Table\StrategyInterface $tableStrategy * @param \Magento\Eav\Model\Config $eavConfig - * @param \Magento\Framework\Model\Entity\MetadataPool $metadataPool * @param string $connectionName */ public function __construct( \Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Framework\Indexer\Table\StrategyInterface $tableStrategy, \Magento\Eav\Model\Config $eavConfig, - \Magento\Framework\Model\Entity\MetadataPool $metadataPool, $connectionName = null ) { $this->_eavConfig = $eavConfig; - $this->metadataPool = $metadataPool; parent::__construct($context, $tableStrategy, $connectionName); } @@ -78,7 +75,7 @@ protected function _addAttributeToSelect($select, $attrCode, $entity, $store, $c $attributeTable = $attribute->getBackend()->getTable(); $connection = $this->getConnection(); $joinType = $condition !== null || $required ? 'join' : 'joinLeft'; - $productIdField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(); + $productIdField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(); if ($attribute->isScopeGlobal()) { $alias = 'ta_' . $attrCode; @@ -184,7 +181,7 @@ protected function _addProductWebsiteJoinToSelect($select, $website, $product) public function getRelationsByChild($childIds) { $connection = $this->getConnection(); - $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(); + $linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(); $select = $connection->select()->from( ['relation' => $this->getTable('catalog_product_relation')], [] @@ -215,7 +212,7 @@ public function getRelationsByParent($parentIds) $result = []; if (!empty($parentIds)) { $connection = $this->getConnection(); - $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(); + $linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(); $select = $connection->select()->from( ['cpr' => $this->getTable('catalog_product_relation')], 'child_id' @@ -231,4 +228,16 @@ public function getRelationsByParent($parentIds) return $result; } + + /** + * @return \Magento\Framework\Model\Entity\MetadataPool + */ + protected function getMetadataPool() + { + if (null === $this->metadataPool) { + $this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Framework\Model\Entity\MetadataPool'); + } + return $this->metadataPool; + } } diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/Eav/AbstractEav.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/Eav/AbstractEav.php index c1588764299f2..3cc635a3c1b86 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/Eav/AbstractEav.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/Eav/AbstractEav.php @@ -27,7 +27,6 @@ abstract class AbstractEav extends \Magento\Catalog\Model\ResourceModel\Product\ * @param \Magento\Framework\Model\ResourceModel\Db\Context $context * @param \Magento\Framework\Indexer\Table\StrategyInterface $tableStrategy * @param \Magento\Eav\Model\Config $eavConfig - * @param \Magento\Framework\Model\Entity\MetadataPool $metadataPool * @param \Magento\Framework\Event\ManagerInterface $eventManager * @param string $connectionName */ @@ -35,12 +34,11 @@ public function __construct( \Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Framework\Indexer\Table\StrategyInterface $tableStrategy, \Magento\Eav\Model\Config $eavConfig, - \Magento\Framework\Model\Entity\MetadataPool $metadataPool, \Magento\Framework\Event\ManagerInterface $eventManager, $connectionName = null ) { $this->_eventManager = $eventManager; - parent::__construct($context, $tableStrategy, $eavConfig, $metadataPool, $connectionName); + parent::__construct($context, $tableStrategy, $eavConfig, $connectionName); } /** @@ -166,7 +164,7 @@ protected function _removeNotVisibleEntityFromIndex() "cpe.entity_id = {$idxTable}.entity_id", [] ); - $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(); + $linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(); $condition = $connection->quoteInto('=?', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_NOT_VISIBLE); $this->_addAttributeToSelect( $select, @@ -192,7 +190,7 @@ protected function _prepareRelationIndexSelect($parentIds = null) { $connection = $this->getConnection(); $idxTable = $this->getIdxTable(); - $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(); + $linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(); $select = $connection->select()->from( ['l' => $this->getTable('catalog_product_relation')], [] diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/Eav/Decimal.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/Eav/Decimal.php index 07bb3925f200f..e8d9889e68d59 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/Eav/Decimal.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/Eav/Decimal.php @@ -46,7 +46,7 @@ protected function _prepareIndex($entityIds = null, $attributeId = null) return $this; } - $productIdField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(); + $productIdField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(); $productValueExpression = $connection->getCheckSql('pds.value_id > 0', 'pds.value', 'pdd.value'); $select = $connection->select()->from( diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/Eav/Source.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/Eav/Source.php index a9830eb480591..e0b4976193481 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/Eav/Source.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/Eav/Source.php @@ -29,7 +29,6 @@ class Source extends AbstractEav * @param \Magento\Framework\Indexer\Table\StrategyInterface $tableStrategy * @param \Magento\Eav\Model\Config $eavConfig * @param \Magento\Framework\Event\ManagerInterface $eventManager - * @param \Magento\Framework\Model\Entity\MetadataPool $metadataPool * @param \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper * @param string $connectionName */ @@ -37,13 +36,12 @@ public function __construct( \Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Framework\Indexer\Table\StrategyInterface $tableStrategy, \Magento\Eav\Model\Config $eavConfig, - \Magento\Framework\Model\Entity\MetadataPool $metadataPool, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper, $connectionName = null ) { $this->_resourceHelper = $resourceHelper; - parent::__construct($context, $tableStrategy, $eavConfig, $metadataPool, $eventManager, $connectionName); + parent::__construct($context, $tableStrategy, $eavConfig, $eventManager, $connectionName); } /** @@ -120,7 +118,7 @@ protected function _prepareSelectIndex($entityIds = null, $attributeId = null) if (!$attrIds) { return $this; } - $productIdField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(); + $productIdField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(); /**@var $subSelect \Magento\Framework\DB\Select*/ $subSelect = $connection->select()->from( @@ -225,7 +223,7 @@ protected function _prepareMultiselectIndex($entityIds = null, $attributeId = nu if (!$attrIds) { return $this; } - $productIdField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(); + $productIdField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(); // load attribute options $options = []; diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/Price/DefaultPrice.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/Price/DefaultPrice.php index f47eb59687f9e..289445ae2daf0 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/Price/DefaultPrice.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/Price/DefaultPrice.php @@ -49,7 +49,6 @@ class DefaultPrice extends AbstractIndexer implements PriceInterface * @param \Magento\Framework\Model\ResourceModel\Db\Context $context * @param \Magento\Framework\Indexer\Table\StrategyInterface $tableStrategy * @param \Magento\Eav\Model\Config $eavConfig - * @param \Magento\Framework\Model\Entity\MetadataPool $metadataPool * @param \Magento\Framework\Event\ManagerInterface $eventManager * @param \Magento\Framework\Module\Manager $moduleManager * @param string $connectionName @@ -58,14 +57,13 @@ public function __construct( \Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Framework\Indexer\Table\StrategyInterface $tableStrategy, \Magento\Eav\Model\Config $eavConfig, - \Magento\Framework\Model\Entity\MetadataPool $metadataPool, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Framework\Module\Manager $moduleManager, $connectionName = null ) { $this->_eventManager = $eventManager; $this->moduleManager = $moduleManager; - parent::__construct($context, $tableStrategy, $eavConfig, $metadataPool, $connectionName); + parent::__construct($context, $tableStrategy, $eavConfig, $connectionName); } /** @@ -242,7 +240,7 @@ protected function _prepareFinalPriceData($entityIds = null) protected function prepareFinalPriceDataForType($entityIds, $type) { $this->_prepareDefaultFinalPriceTable(); - $metadata = $this->metadataPool->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class); + $metadata = $this->getMetadataPool()->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class); $connection = $this->getConnection(); $select = $connection->select()->from( ['e' => $this->getTable('catalog_product_entity')], diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Link.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Link.php index 958e77d70a255..bbccabe5a61c9 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Link.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Link.php @@ -27,11 +27,15 @@ class Link extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb protected $_catalogProductRelation; /** + * Link constructor. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context + * @param Relation $catalogProductRelation * @param string|null $connectionName + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function __construct( \Magento\Framework\Model\ResourceModel\Db\Context $context, + Relation $catalogProductRelation, $connectionName = null ) { parent::__construct($context, $connectionName); diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Link/Product/Collection.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Link/Product/Collection.php index 97b30dc20e65a..6cb87e8598714 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Link/Product/Collection.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Link/Product/Collection.php @@ -78,7 +78,6 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection * @param \Magento\Customer\Model\Session $customerSession * @param \Magento\Framework\Stdlib\DateTime $dateTime * @param GroupManagementInterface $groupManagement - * @param \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation $productLimitation * @param \Magento\Framework\DB\Adapter\AdapterInterface|null $connection * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ @@ -102,7 +101,6 @@ public function __construct( \Magento\Customer\Model\Session $customerSession, \Magento\Framework\Stdlib\DateTime $dateTime, GroupManagementInterface $groupManagement, - \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation $productLimitation, \Magento\Framework\DB\Adapter\AdapterInterface $connection = null ) { parent::__construct( @@ -125,7 +123,6 @@ public function __construct( $customerSession, $dateTime, $groupManagement, - $productLimitation, $connection ); } diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Option.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Option.php index ec3c57fe81bac..b14be38585462 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Option.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Option.php @@ -48,7 +48,6 @@ class Option extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb * @param \Magento\Directory\Model\CurrencyFactory $currencyFactory * @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param \Magento\Framework\App\Config\ScopeConfigInterface $config - * @param \Magento\Framework\Model\Entity\MetadataPool $metadataPool * @param string $connectionName */ public function __construct( @@ -56,13 +55,11 @@ public function __construct( \Magento\Directory\Model\CurrencyFactory $currencyFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\App\Config\ScopeConfigInterface $config, - \Magento\Framework\Model\Entity\MetadataPool $metadataPool, $connectionName = null ) { $this->_currencyFactory = $currencyFactory; $this->_storeManager = $storeManager; $this->_config = $config; - $this->metadataPool = $metadataPool; parent::__construct($context, $connectionName); } @@ -493,7 +490,7 @@ public function getSearchableData($productId, $storeId) ['cpe' => $this->getTable('catalog_product_entity')], sprintf( 'cpe.%s = product_option.product_id', - $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField() + $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField() ), [] )->joinLeft( @@ -540,7 +537,7 @@ public function getSearchableData($productId, $storeId) ['cpe' => $this->getTable('catalog_product_entity')], sprintf( 'cpe.%s = product_option.product_id', - $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField() + $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField() ), [] )->join( @@ -562,4 +559,16 @@ public function getSearchableData($productId, $storeId) return $searchData; } + + /** + * @return \Magento\Framework\Model\Entity\MetadataPool + */ + private function getMetadataPool() + { + if (null === $this->metadataPool) { + $this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Framework\Model\Entity\MetadataPool'); + } + return $this->metadataPool; + } } diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Option/Collection.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Option/Collection.php index 0fdcab3f351c7..23dbe2498843f 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Option/Collection.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Option/Collection.php @@ -47,8 +47,6 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab * @param \Magento\Framework\Event\ManagerInterface $eventManager * @param \Magento\Catalog\Model\ResourceModel\Product\Option\Value\CollectionFactory $optionValueCollectionFactory * @param \Magento\Store\Model\StoreManagerInterface $storeManager - * @param \Magento\Framework\Model\Entity\MetadataPool $metadataPool - * @param JoinProcessorInterface $joinProcessor * @param \Magento\Framework\DB\Adapter\AdapterInterface $connection * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource * @SuppressWarnings(PHPMD.ExcessiveParameterList) @@ -60,15 +58,11 @@ public function __construct( \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Catalog\Model\ResourceModel\Product\Option\Value\CollectionFactory $optionValueCollectionFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, - \Magento\Framework\Model\Entity\MetadataPool $metadataPool, - JoinProcessorInterface $joinProcessor, \Magento\Framework\DB\Adapter\AdapterInterface $connection = null, \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null ) { $this->_optionValueCollectionFactory = $optionValueCollectionFactory; $this->_storeManager = $storeManager; - $this->metadataPool = $metadataPool; - $this->joinProcessor = $joinProcessor; parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource); } @@ -251,7 +245,7 @@ protected function _initSelect() ['cpe' => $this->getTable('catalog_product_entity')], sprintf( 'cpe.%s = main_table.product_id', - $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField() + $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField() ), [] ); @@ -283,7 +277,7 @@ public function getProductOptions($productId, $storeId, $requiredOnly = false) $collection->addRequiredFilter(); } $collection->addValuesToResult($storeId); - $this->joinProcessor->process($collection); + $this->getJoinProcessor()->process($collection); return $collection->getItems(); } @@ -320,4 +314,28 @@ public function reset() { return $this->_reset(); } + + /** + * @return \Magento\Framework\Model\Entity\MetadataPool + */ + private function getMetadataPool() + { + if (null === $this->metadataPool) { + $this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Framework\Model\Entity\MetadataPool'); + } + return $this->metadataPool; + } + + /** + * @return JoinProcessorInterface + */ + private function getJoinProcessor() + { + if (null === $this->joinProcessor) { + $this->joinProcessor = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface'); + } + return $this->joinProcessor; + } } diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Url.php b/app/code/Magento/Catalog/Model/ResourceModel/Url.php index 5cbad064d258f..0fd6ad826b5fd 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Url.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Url.php @@ -108,7 +108,6 @@ class Url extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb * @param Product $productResource * @param \Magento\Catalog\Model\Category $catalogCategory * @param \Psr\Log\LoggerInterface $logger - * @param MetadataPool $metadataPool * @param null $connectionName */ public function __construct( @@ -118,7 +117,6 @@ public function __construct( Product $productResource, \Magento\Catalog\Model\Category $catalogCategory, \Psr\Log\LoggerInterface $logger, - MetadataPool $metadataPool, $connectionName = null ) { $this->_storeManager = $storeManager; @@ -126,7 +124,6 @@ public function __construct( $this->productResource = $productResource; $this->_catalogCategory = $catalogCategory; $this->_logger = $logger; - $this->metadataPool = $metadataPool; parent::__construct($context, $connectionName); } @@ -167,8 +164,8 @@ public function getStores($storeId = null) */ protected function _getCategoryAttribute($attributeCode, $categoryIds, $storeId) { - $linkField = $this->metadataPool->getMetadata(CategoryInterface::class)->getLinkField(); - $identifierFiled = $this->metadataPool->getMetadata(CategoryInterface::class)->getIdentifierField(); + $linkField = $this->getMetadataPool()->getMetadata(CategoryInterface::class)->getLinkField(); + $identifierFiled = $this->getMetadataPool()->getMetadata(CategoryInterface::class)->getIdentifierField(); $connection = $this->getConnection(); if (!isset($this->_categoryAttributes[$attributeCode])) { @@ -403,7 +400,7 @@ protected function _getCategories($categoryIds, $storeId = null, $path = null) $categories = []; $connection = $this->getConnection(); - $meta = $this->metadataPool->getMetadata(CategoryInterface::class); + $meta = $this->getMetadataPool()->getMetadata(CategoryInterface::class); $linkField = $meta->getLinkField(); if (!is_array($categoryIds)) { @@ -698,4 +695,16 @@ public function getRewriteByProductStore(array $products) return $result; } + + /** + * @return \Magento\Framework\Model\Entity\MetadataPool + */ + private function getMetadataPool() + { + if (null === $this->metadataPool) { + $this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Framework\Model\Entity\MetadataPool'); + } + return $this->metadataPool; + } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/CategoryLinkManagementTest.php b/app/code/Magento/Catalog/Test/Unit/Model/CategoryLinkManagementTest.php index e97c0c4a29a76..38061c6fc2f65 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/CategoryLinkManagementTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/CategoryLinkManagementTest.php @@ -28,9 +28,6 @@ protected function setUp() { $this->categoryRepositoryMock = $this->getMock('\Magento\Catalog\Model\CategoryRepository', [], [], '', false); $productResource = $this->getMock('Magento\Catalog\Model\ResourceModel\Product', [], [], '', false); - $productRepositoryMock = $this->getMockBuilder('Magento\Catalog\Api\ProductRepositoryInterface') - ->disableOriginalConstructor() - ->getMockForAbstractClass(); $categoryLinkRepository = $this->getMockBuilder('Magento\Catalog\Api\CategoryLinkRepositoryInterface') ->disableOriginalConstructor() ->getMockForAbstractClass(); @@ -44,14 +41,18 @@ protected function setUp() '', false ); + $this->model = new \Magento\Catalog\Model\CategoryLinkManagement( $this->categoryRepositoryMock, - $productRepositoryMock, - $productResource, - $categoryLinkRepository, - $this->productLinkFactoryMock, - $indexerRegistry + $this->productLinkFactoryMock ); + + $this->setProperties($this->model, [ + 'productResource' => $productResource, + 'categoryLinkRepository' => $categoryLinkRepository, + 'productLinkFactory' => $this->productLinkFactoryMock, + 'indexerRegistry' => $indexerRegistry + ]); } public function testGetAssignedProducts() @@ -94,4 +95,20 @@ public function testGetAssignedProducts() ->willReturnSelf(); $this->assertEquals([$categoryProductLinkMock], $this->model->getAssignedProducts($categoryId)); } + + /** + * @param $object + * @param array $properties + */ + private function setProperties($object, $properties = []) + { + $reflectionClass = new \ReflectionClass(get_class($object)); + foreach ($properties as $key => $value) { + if ($reflectionClass->hasProperty($key)) { + $reflectionProperty = $reflectionClass->getProperty($key); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue($object, $value); + } + } + } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/CategoryRepositoryTest.php b/app/code/Magento/Catalog/Test/Unit/Model/CategoryRepositoryTest.php index 69317fb768889..55779d1779e25 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/CategoryRepositoryTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/CategoryRepositoryTest.php @@ -91,9 +91,13 @@ protected function setUp() $this->model = new \Magento\Catalog\Model\CategoryRepository( $this->categoryFactoryMock, $this->categoryResourceMock, - $this->storeManagerMock, - $this->metadataPoolMock + $this->storeManagerMock ); + + $this->setProperties($this->model, [ + 'metadataPool' => $this->metadataPoolMock + ]); + // Todo: \Magento\Framework\TestFramework\Unit\Helper\ObjectManager to do this automatically (MAGETWO-49793) $reflection = new \ReflectionClass(get_class($this->model)); $reflectionProperty = $reflection->getProperty('extensibleDataObjectConverter'); @@ -367,4 +371,20 @@ public function testDeleteByIdentifierWithException() ); $this->model->deleteByIdentifier($categoryId); } + + /** + * @param $object + * @param array $properties + */ + private function setProperties($object, $properties = []) + { + $reflectionClass = new \ReflectionClass(get_class($object)); + foreach ($properties as $key => $value) { + if ($reflectionClass->hasProperty($key)) { + $reflectionProperty = $reflectionClass->getProperty($key); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue($object, $value); + } + } + } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Product/CopierTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Product/CopierTest.php index b3c533aa8ba6f..09149788022db 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Product/CopierTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Product/CopierTest.php @@ -58,9 +58,12 @@ protected function setUp() $this->_model = new Copier( $this->copyConstructorMock, - $this->productFactoryMock, - $this->optionRepositoryMock + $this->productFactoryMock ); + + $this->setProperties($this->_model, [ + 'optionRepository' => $this->optionRepositoryMock + ]); } public function testCopy() @@ -126,4 +129,20 @@ public function testCopy() $this->assertEquals($duplicateMock, $this->_model->copy($this->productMock)); } + + /** + * @param $object + * @param array $properties + */ + private function setProperties($object, $properties = []) + { + $reflectionClass = new \ReflectionClass(get_class($object)); + foreach ($properties as $key => $value) { + if ($reflectionClass->hasProperty($key)) { + $reflectionProperty = $reflectionClass->getProperty($key); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue($object, $value); + } + } + } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Product/Option/RepositoryTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Product/Option/RepositoryTest.php index 76c9d1cdb2d79..ac08093a9df4b 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Product/Option/RepositoryTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Product/Option/RepositoryTest.php @@ -46,6 +46,7 @@ protected function setUp() '', false ); + $this->converterMock = $this->getMock('\Magento\Catalog\Model\Product\Option\Converter', [], [], '', false); $this->optionMock = $this->getMock('\Magento\Catalog\Model\Product\Option', [], [], '', false); $this->productMock = $this->getMock('Magento\Catalog\Model\Product', [], [], '', false); $optionFactory = $this->getMock( @@ -69,12 +70,20 @@ protected function setUp() ->disableOriginalConstructor() ->getMock(); $metadataPool->expects($this->any())->method('getMetadata')->willReturn($metadata); + $this->optionRepository = new Repository( $this->productRepositoryMock, $this->optionResourceMock, - $optionFactory, - $optionCollectionFactory, - $metadataPool + $this->converterMock + ); + + $this->setProperties( + $this->optionRepository, + [ + 'optionFactory' => $optionFactory, + 'optionCollectionFactory' => $optionCollectionFactory, + 'metadataPool' => $metadataPool + ] ); } @@ -200,4 +209,20 @@ public function testDeleteByIdentifierWhenCannotRemoveOption() ->willThrowException(new \Exception()); $this->assertTrue($this->optionRepository->deleteByIdentifier($productSku, $optionId)); } + + /** + * @param $object + * @param array $properties + */ + private function setProperties($object, $properties = []) + { + $reflectionClass = new \ReflectionClass(get_class($object)); + foreach ($properties as $key => $value) { + if ($reflectionClass->hasProperty($key)) { + $reflectionProperty = $reflectionClass->getProperty($key); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue($object, $value); + } + } + } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ProductTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ProductTest.php index 2e4a525f12765..0bee69952c4be 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ProductTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ProductTest.php @@ -342,6 +342,7 @@ protected function setUp() $this->mediaConfig = $this->getMock('Magento\Catalog\Model\Product\Media\Config', [], [], '', false); $this->objectManagerHelper = new ObjectManagerHelper($this); + $this->model = $this->objectManagerHelper->getObject( 'Magento\Catalog\Model\Product', [ diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/TreeTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/TreeTest.php index 13e8b99c47699..e72ded1d512d7 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/TreeTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/TreeTest.php @@ -214,6 +214,7 @@ public function testAddCollectionData() ->with(CategoryInterface::class) ->willReturn($categoryMetadataMock); + $model = $objectHelper->getObject( 'Magento\Catalog\Model\ResourceModel\Category\Tree', [ diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/CollectionTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/CollectionTest.php index a71d0c95fab1c..f61888f8df93f 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/CollectionTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/CollectionTest.php @@ -112,6 +112,13 @@ protected function setUp() $entityMock->expects($this->any())->method('getTable')->willReturnArgument(0); $this->connectionMock->expects($this->atLeastOnce())->method('select')->willReturn($this->selectMock); $helper = new ObjectManager($this); + + $this->prepareObjectManager([ + [ + 'Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation', + $this->getMock('Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation') + ] + ]); $this->collection = $helper->getObject( 'Magento\Catalog\Model\ResourceModel\Product\Collection', [ @@ -165,4 +172,20 @@ public function testAddProductCategoriesFilter() )->willReturnSelf(); $this->collection->addCategoriesFilter([$conditionType => $values]); } + + /** + * @param $map + */ + private function prepareObjectManager($map) + { + $objectManagerMock = $this->getMock('Magento\Framework\ObjectManagerInterface'); + $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($map)); + $reflectionClass = new \ReflectionClass('Magento\Framework\App\ObjectManager'); + $reflectionProperty = $reflectionClass->getProperty('_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue($objectManagerMock); + } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Link/Product/CollectionTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Link/Product/CollectionTest.php index 9089999be0466..429440a2a1983 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Link/Product/CollectionTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Link/Product/CollectionTest.php @@ -116,6 +116,13 @@ function ($store) { $this->sessionMock = $this->getMock('Magento\Customer\Model\Session', [], [], '', false); $this->dateTimeMock = $this->getMock('Magento\Framework\Stdlib\DateTime'); $this->objectManagerHelper = new ObjectManagerHelper($this); + $this->prepareObjectManager([ + [ + 'Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation', + $this->getMock('Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation') + ] + ]); + $this->collection = $this->objectManagerHelper->getObject( 'Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection', [ @@ -151,4 +158,20 @@ public function testSetProduct() $this->collection->setProduct($product); $this->assertEquals(33, $this->collection->getStoreId()); } + + /** + * @param $map + */ + public function prepareObjectManager($map) + { + $objectManagerMock = $this->getMock('Magento\Framework\ObjectManagerInterface'); + $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($map)); + $reflectionClass = new \ReflectionClass('Magento\Framework\App\ObjectManager'); + $reflectionProperty = $reflectionClass->getProperty('_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue($objectManagerMock); + } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Option/CollectionTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Option/CollectionTest.php index 07727f8b0de62..b1ecb4f5f46d0 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Option/CollectionTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Option/CollectionTest.php @@ -133,6 +133,12 @@ protected function setUp() $metadata->expects($this->any())->method('getLinkField')->willReturn('id'); $this->metadataPoolMock->expects($this->any())->method('getMetadata')->willReturn($metadata); $this->selectMock->expects($this->exactly(2))->method('join'); + + $this->prepareObjectManager([ + ['Magento\Framework\Model\Entity\MetadataPool', $this->metadataPoolMock], + ['Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface', $this->joinProcessor] + ]); + $this->collection = new Collection( $this->entityFactoryMock, $this->loggerMock, @@ -140,8 +146,6 @@ protected function setUp() $this->eventManagerMock, $this->optionsFactoryMock, $this->storeManagerMock, - $this->metadataPoolMock, - $this->joinProcessor, null, $this->resourceMock ); @@ -151,4 +155,20 @@ public function testReset() { $this->collection->reset(); } + + /** + * @param $map + */ + private function prepareObjectManager($map) + { + $objectManagerMock = $this->getMock('Magento\Framework\ObjectManagerInterface'); + $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($map)); + $reflectionClass = new \ReflectionClass('Magento\Framework\App\ObjectManager'); + $reflectionProperty = $reflectionClass->getProperty('_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue($objectManagerMock); + } } diff --git a/app/code/Magento/Catalog/etc/di.xml b/app/code/Magento/Catalog/etc/di.xml index 85ffa57e28e29..65e14be97edd1 100644 --- a/app/code/Magento/Catalog/etc/di.xml +++ b/app/code/Magento/Catalog/etc/di.xml @@ -612,7 +612,7 @@ - + entity_type_id diff --git a/app/code/Magento/CatalogInventory/Block/Stockqty/AbstractStockqty.php b/app/code/Magento/CatalogInventory/Block/Stockqty/AbstractStockqty.php index a22697626c824..9c080905165ae 100644 --- a/app/code/Magento/CatalogInventory/Block/Stockqty/AbstractStockqty.php +++ b/app/code/Magento/CatalogInventory/Block/Stockqty/AbstractStockqty.php @@ -25,6 +25,11 @@ abstract class AbstractStockqty extends \Magento\Framework\View\Element\Template */ protected $_coreRegistry; + /** + * @var \Magento\CatalogInventory\Api\StockStateInterface + */ + protected $stockState; + /** * @var \Magento\CatalogInventory\Api\StockRegistryInterface */ @@ -33,16 +38,19 @@ abstract class AbstractStockqty extends \Magento\Framework\View\Element\Template /** * @param \Magento\Framework\View\Element\Template\Context $context * @param \Magento\Framework\Registry $registry + * @param \Magento\CatalogInventory\Api\StockStateInterface $stockState * @param \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry * @param array $data */ public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Framework\Registry $registry, + \Magento\CatalogInventory\Api\StockStateInterface $stockState, \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry, array $data = [] ) { $this->_coreRegistry = $registry; + $this->stockState = $stockState; $this->stockRegistry = $stockRegistry; parent::__construct($context, $data); diff --git a/app/code/Magento/CatalogInventory/Model/ResourceModel/Indexer/Stock/DefaultStock.php b/app/code/Magento/CatalogInventory/Model/ResourceModel/Indexer/Stock/DefaultStock.php index 30ea482cce93d..c91170f629404 100644 --- a/app/code/Magento/CatalogInventory/Model/ResourceModel/Indexer/Stock/DefaultStock.php +++ b/app/code/Magento/CatalogInventory/Model/ResourceModel/Indexer/Stock/DefaultStock.php @@ -48,23 +48,18 @@ class DefaultStock extends AbstractIndexer implements StockInterface * @param \Magento\Framework\Model\ResourceModel\Db\Context $context * @param \Magento\Framework\Indexer\Table\StrategyInterface $tableStrategy * @param \Magento\Eav\Model\Config $eavConfig - * @param \Magento\Framework\Model\Entity\MetadataPool $metadataPool * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig - * @param QueryProcessorComposite $queryProcessorComposite * @param string $connectionName */ public function __construct( \Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Framework\Indexer\Table\StrategyInterface $tableStrategy, \Magento\Eav\Model\Config $eavConfig, - \Magento\Framework\Model\Entity\MetadataPool $metadataPool, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, - QueryProcessorComposite $queryProcessorComposite, $connectionName = null ) { $this->_scopeConfig = $scopeConfig; - $this->queryProcessorComposite = $queryProcessorComposite; - parent::__construct($context, $tableStrategy, $eavConfig, $metadataPool, $connectionName); + parent::__construct($context, $tableStrategy, $eavConfig, $connectionName); } /** @@ -181,7 +176,7 @@ protected function _isManageStock() */ protected function _getStockStatusSelect($entityIds = null, $usePrimaryTable = false) { - $metadata = $this->metadataPool->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class); + $metadata = $this->getMetadataPool()->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class); $connection = $this->getConnection(); $qtyExpr = $connection->getCheckSql('cisi.qty > 0', 'cisi.qty', 0); $select = $connection->select()->from( @@ -235,7 +230,7 @@ protected function _prepareIndexTable($entityIds = null) { $connection = $this->getConnection(); $select = $this->_getStockStatusSelect($entityIds); - $select = $this->queryProcessorComposite->processQuery($select, $entityIds); + $select = $this->getQueryProcessorComposite()->processQuery($select, $entityIds); $query = $select->insertFromSelect($this->getIdxTable()); $connection->query($query); @@ -252,7 +247,7 @@ protected function _updateIndex($entityIds) { $connection = $this->getConnection(); $select = $this->_getStockStatusSelect($entityIds, true); - $select = $this->queryProcessorComposite->processQuery($select, $entityIds, true); + $select = $this->getQueryProcessorComposite()->processQuery($select, $entityIds, true); $query = $connection->query($select); $i = 0; @@ -329,4 +324,16 @@ protected function getStatusExpression(AdapterInterface $connection, $isAggregat } return $statusExpr; } + + /** + * @return QueryProcessorComposite + */ + private function getQueryProcessorComposite() + { + if (null === $this->queryProcessorComposite) { + $this->queryProcessorComposite = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\CatalogInventory\Model\ResourceModel\Indexer\Stock\QueryProcessorComposite'); + } + return $this->queryProcessorComposite; + } } diff --git a/app/code/Magento/CatalogInventory/Model/Stock/StockItemRepository.php b/app/code/Magento/CatalogInventory/Model/Stock/StockItemRepository.php index 98faed465a74f..bf96f1aaee49e 100644 --- a/app/code/Magento/CatalogInventory/Model/Stock/StockItemRepository.php +++ b/app/code/Magento/CatalogInventory/Model/Stock/StockItemRepository.php @@ -101,7 +101,6 @@ class StockItemRepository implements StockItemRepositoryInterface * @param TimezoneInterface $localeDate * @param Processor $indexProcessor * @param DateTime $dateTime - * @param StockRegistryStorage $stockRegistryStorage * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct( @@ -115,8 +114,7 @@ public function __construct( MapperFactory $mapperFactory, TimezoneInterface $localeDate, Processor $indexProcessor, - DateTime $dateTime, - StockRegistryStorage $stockRegistryStorage + DateTime $dateTime ) { $this->stockConfiguration = $stockConfiguration; $this->stockStateProvider = $stockStateProvider; @@ -129,7 +127,6 @@ public function __construct( $this->localeDate = $localeDate; $this->indexProcessor = $indexProcessor; $this->dateTime = $dateTime; - $this->stockRegistryStorage = $stockRegistryStorage; } /** @@ -209,8 +206,8 @@ public function delete(StockItemInterface $stockItem) { try { $this->resource->delete($stockItem); - $this->stockRegistryStorage->removeStockItem($stockItem->getProductId()); - $this->stockRegistryStorage->removeStockStatus($stockItem->getProductId()); + $this->getStockRegistryStorage()->removeStockItem($stockItem->getProductId()); + $this->getStockRegistryStorage()->removeStockStatus($stockItem->getProductId()); } catch (\Exception $exception) { throw new CouldNotDeleteException( __('Unable to remove Stock Item with id "%1"', $stockItem->getItemId()), @@ -236,4 +233,16 @@ public function deleteById($id) } return true; } + + /** + * @return StockRegistryStorage + */ + private function getStockRegistryStorage() + { + if (null === $this->stockRegistryStorage) { + $this->stockRegistryStorage = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\CatalogInventory\Model\StockRegistryStorage'); + } + return $this->stockRegistryStorage; + } } diff --git a/app/code/Magento/CatalogInventory/Model/Stock/StockRepository.php b/app/code/Magento/CatalogInventory/Model/Stock/StockRepository.php index 0386143a9cdcc..015310fffa48d 100644 --- a/app/code/Magento/CatalogInventory/Model/Stock/StockRepository.php +++ b/app/code/Magento/CatalogInventory/Model/Stock/StockRepository.php @@ -59,22 +59,19 @@ class StockRepository implements StockRepositoryInterface * @param StockCollectionInterfaceFactory $collectionFactory * @param QueryBuilderFactory $queryBuilderFactory * @param MapperFactory $mapperFactory - * @param StockRegistryStorage $stockRegistryStorage */ public function __construct( StockResource $resource, StockFactory $stockFactory, StockCollectionInterfaceFactory $collectionFactory, QueryBuilderFactory $queryBuilderFactory, - MapperFactory $mapperFactory, - StockRegistryStorage $stockRegistryStorage + MapperFactory $mapperFactory ) { $this->resource = $resource; $this->stockFactory = $stockFactory; $this->stockCollectionFactory = $collectionFactory; $this->queryBuilderFactory = $queryBuilderFactory; $this->mapperFactory = $mapperFactory; - $this->stockRegistryStorage = $stockRegistryStorage; } /** @@ -130,7 +127,7 @@ public function delete(StockInterface $stock) { try { $this->resource->delete($stock); - $this->stockRegistryStorage->removeStock(); + $this->getStockRegistryStorage()->removeStock(); } catch (\Exception $exception) { throw new CouldNotDeleteException( __('Unable to remove Stock with id "%1"', $stock->getStockId()), @@ -158,4 +155,16 @@ public function deleteById($id) } return true; } + + /** + * @return StockRegistryStorage + */ + private function getStockRegistryStorage() + { + if (null === $this->stockRegistryStorage) { + $this->stockRegistryStorage = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\CatalogInventory\Model\StockRegistryStorage'); + } + return $this->stockRegistryStorage; + } } diff --git a/app/code/Magento/CatalogInventory/Model/Stock/StockStatusRepository.php b/app/code/Magento/CatalogInventory/Model/Stock/StockStatusRepository.php index 1a01bc8778967..921a28a3e58d9 100644 --- a/app/code/Magento/CatalogInventory/Model/Stock/StockStatusRepository.php +++ b/app/code/Magento/CatalogInventory/Model/Stock/StockStatusRepository.php @@ -57,22 +57,19 @@ class StockStatusRepository implements StockStatusRepositoryInterface * @param StockStatusCollectionInterfaceFactory $collectionFactory * @param QueryBuilderFactory $queryBuilderFactory * @param MapperFactory $mapperFactory - * @param StockRegistryStorage $stockRegistryStorage */ public function __construct( StockStatusResource $resource, StatusFactory $stockStatusFactory, StockStatusCollectionInterfaceFactory $collectionFactory, QueryBuilderFactory $queryBuilderFactory, - MapperFactory $mapperFactory, - StockRegistryStorage $stockRegistryStorage + MapperFactory $mapperFactory ) { $this->resource = $resource; $this->stockStatusFactory = $stockStatusFactory; $this->stockStatusCollectionFactory = $collectionFactory; $this->queryBuilderFactory = $queryBuilderFactory; $this->mapperFactory = $mapperFactory; - $this->stockRegistryStorage = $stockRegistryStorage; } /** @@ -124,7 +121,7 @@ public function delete(StockStatusInterface $stockStatus) { try { $this->resource->delete($stockStatus); - $this->stockRegistryStorage->removeStockStatus($stockStatus->getProductId()); + $this->getStockRegistryStorage()->removeStockStatus($stockStatus->getProductId()); } catch (\Exception $exception) { throw new CouldNotDeleteException( __('Unable to remove Stock Status for product %1', $stockStatus->getProductId()), @@ -152,4 +149,16 @@ public function deleteById($id) } return true; } + + /** + * @return StockRegistryStorage + */ + private function getStockRegistryStorage() + { + if (null === $this->stockRegistryStorage) { + $this->stockRegistryStorage = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\CatalogInventory\Model\StockRegistryStorage'); + } + return $this->stockRegistryStorage; + } } diff --git a/app/code/Magento/CatalogInventory/Model/StockRegistryProvider.php b/app/code/Magento/CatalogInventory/Model/StockRegistryProvider.php index b7ac13dd0d3ec..a71b8bbe2b7b5 100644 --- a/app/code/Magento/CatalogInventory/Model/StockRegistryProvider.php +++ b/app/code/Magento/CatalogInventory/Model/StockRegistryProvider.php @@ -83,7 +83,6 @@ class StockRegistryProvider implements StockRegistryProviderInterface * @param StockCriteriaInterfaceFactory $stockCriteriaFactory * @param StockItemCriteriaInterfaceFactory $stockItemCriteriaFactory * @param StockStatusCriteriaInterfaceFactory $stockStatusCriteriaFactory - * @param StockRegistryStorage $stockRegistryStorage * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct( @@ -95,8 +94,7 @@ public function __construct( StockStatusInterfaceFactory $stockStatusFactory, StockCriteriaInterfaceFactory $stockCriteriaFactory, StockItemCriteriaInterfaceFactory $stockItemCriteriaFactory, - StockStatusCriteriaInterfaceFactory $stockStatusCriteriaFactory, - StockRegistryStorage $stockRegistryStorage + StockStatusCriteriaInterfaceFactory $stockStatusCriteriaFactory ) { $this->stockRepository = $stockRepository; $this->stockFactory = $stockFactory; @@ -107,7 +105,6 @@ public function __construct( $this->stockCriteriaFactory = $stockCriteriaFactory; $this->stockItemCriteriaFactory = $stockItemCriteriaFactory; $this->stockStatusCriteriaFactory = $stockStatusCriteriaFactory; - $this->stockRegistryStorage = $stockRegistryStorage; } /** @@ -116,14 +113,14 @@ public function __construct( */ public function getStock($scopeId) { - $stock = $this->stockRegistryStorage->getStock($scopeId); + $stock = $this->getStockRegistryStorage()->getStock($scopeId); if (null === $stock) { $criteria = $this->stockCriteriaFactory->create(); $criteria->setScopeFilter($scopeId); $collection = $this->stockRepository->getList($criteria); $stock = current($collection->getItems()); if ($stock && $stock->getStockId()) { - $this->stockRegistryStorage->setStock($scopeId, $stock); + $this->getStockRegistryStorage()->setStock($scopeId, $stock); } else { $stock = $this->stockFactory->create(); } @@ -138,14 +135,14 @@ public function getStock($scopeId) */ public function getStockItem($productId, $scopeId) { - $stockItem = $this->stockRegistryStorage->getStockItem($productId, $scopeId); + $stockItem = $this->getStockRegistryStorage()->getStockItem($productId, $scopeId); if (null === $stockItem) { $criteria = $this->stockItemCriteriaFactory->create(); $criteria->setProductsFilter($productId); $collection = $this->stockItemRepository->getList($criteria); $stockItem = current($collection->getItems()); if ($stockItem && $stockItem->getItemId()) { - $this->stockRegistryStorage->setStockItem($productId, $scopeId, $stockItem); + $this->getStockRegistryStorage()->setStockItem($productId, $scopeId, $stockItem); } else { $stockItem = $this->stockItemFactory->create(); } @@ -160,7 +157,7 @@ public function getStockItem($productId, $scopeId) */ public function getStockStatus($productId, $scopeId) { - $stockStatus = $this->stockRegistryStorage->getStockStatus($productId, $scopeId); + $stockStatus = $this->getStockRegistryStorage()->getStockStatus($productId, $scopeId); if (null === $stockStatus) { $criteria = $this->stockStatusCriteriaFactory->create(); $criteria->setProductsFilter($productId); @@ -168,11 +165,23 @@ public function getStockStatus($productId, $scopeId) $collection = $this->stockStatusRepository->getList($criteria); $stockStatus = current($collection->getItems()); if ($stockStatus && $stockStatus->getProductId()) { - $this->stockRegistryStorage->setStockStatus($productId, $scopeId, $stockStatus); + $this->getStockRegistryStorage()->setStockStatus($productId, $scopeId, $stockStatus); } else { $stockStatus = $this->stockStatusFactory->create(); } } return $stockStatus; } + + /** + * @return StockRegistryStorage + */ + private function getStockRegistryStorage() + { + if (null === $this->stockRegistryStorage) { + $this->stockRegistryStorage = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\CatalogInventory\Model\StockRegistryStorage'); + } + return $this->stockRegistryStorage; + } } diff --git a/app/code/Magento/CatalogInventory/Test/Unit/Model/Spi/StockRegistryProviderTest.php b/app/code/Magento/CatalogInventory/Test/Unit/Model/Spi/StockRegistryProviderTest.php index 2d8a9eaa051f7..b4742f6454b8e 100644 --- a/app/code/Magento/CatalogInventory/Test/Unit/Model/Spi/StockRegistryProviderTest.php +++ b/app/code/Magento/CatalogInventory/Test/Unit/Model/Spi/StockRegistryProviderTest.php @@ -223,7 +223,8 @@ protected function setUp() 'stockCriteriaFactory' => $this->stockCriteriaFactory, 'stockItemCriteriaFactory' => $this->stockItemCriteriaFactory, - 'stockStatusCriteriaFactory' => $this->stockStatusCriteriaFactory + 'stockStatusCriteriaFactory' => $this->stockStatusCriteriaFactory, + 'stockRegistryStorage' => $this->getMock('Magento\CatalogInventory\Model\StockRegistryStorage') ] ); } diff --git a/app/code/Magento/CatalogRule/Model/Indexer/IndexBuilder.php b/app/code/Magento/CatalogRule/Model/Indexer/IndexBuilder.php index 1ba4ab648f126..b7e022e8087df 100644 --- a/app/code/Magento/CatalogRule/Model/Indexer/IndexBuilder.php +++ b/app/code/Magento/CatalogRule/Model/Indexer/IndexBuilder.php @@ -106,7 +106,6 @@ class IndexBuilder * @param \Magento\Framework\Stdlib\DateTime\DateTime $dateTime * @param \Magento\Catalog\Model\ProductFactory $productFactory * @param int $batchCount - * @param \Magento\Framework\Model\Entity\MetadataPool $metadataPool * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct( @@ -119,7 +118,6 @@ public function __construct( \Magento\Framework\Stdlib\DateTime $dateFormat, \Magento\Framework\Stdlib\DateTime\DateTime $dateTime, \Magento\Catalog\Model\ProductFactory $productFactory, - MetadataPool $metadataPool, $batchCount = 1000 ) { $this->resource = $resource; @@ -133,7 +131,6 @@ public function __construct( $this->dateTime = $dateTime; $this->productFactory = $productFactory; $this->batchCount = $batchCount; - $this->metadataPool = $metadataPool; } /** @@ -604,7 +601,7 @@ protected function getRuleProductsStmt($websiteId, Product $product = null) $priceTable = $priceAttr->getBackend()->getTable(); $attributeId = $priceAttr->getId(); - $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(); + $linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(); $select->join( ['e' => $this->getTable('catalog_product_entity')], sprintf('e.entity_id = rp.product_id'), @@ -732,4 +729,16 @@ private function roundTime($timeStamp) return $timeStamp; } + + /** + * @return MetadataPool + */ + private function getMetadataPool() + { + if (null === $this->metadataPool) { + $this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Framework\Model\Entity\MetadataPool'); + } + return $this->metadataPool; + } } diff --git a/app/code/Magento/CatalogRule/Model/Rule.php b/app/code/Magento/CatalogRule/Model/Rule.php index 5c4536b390d58..37c190370ca64 100644 --- a/app/code/Magento/CatalogRule/Model/Rule.php +++ b/app/code/Magento/CatalogRule/Model/Rule.php @@ -84,6 +84,11 @@ class Rule extends \Magento\Rule\Model\AbstractModel implements \Magento\Catalog */ protected $_relatedCacheTypes; + /** + * @var \Magento\Framework\Stdlib\DateTime + */ + protected $dateTime; + /** * @var \Magento\Framework\Model\ResourceModel\Iterator */ @@ -130,6 +135,7 @@ class Rule extends \Magento\Rule\Model\AbstractModel implements \Magento\Catalog protected $ruleConditionConverter; /** + * Rule constructor. * @param \Magento\Framework\Model\Context $context * @param \Magento\Framework\Registry $registry * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory @@ -145,8 +151,8 @@ class Rule extends \Magento\Rule\Model\AbstractModel implements \Magento\Catalog * @param \Magento\Customer\Model\Session $customerSession * @param \Magento\CatalogRule\Helper\Data $catalogRuleData * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypesList + * @param \Magento\Framework\Stdlib\DateTime $dateTime * @param Indexer\Rule\RuleProductProcessor $ruleProductProcessor - * @param Data\Condition\Converter $ruleConditionConverter * @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource * @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection * @param array $relatedCacheTypes @@ -170,8 +176,8 @@ public function __construct( \Magento\Customer\Model\Session $customerSession, \Magento\CatalogRule\Helper\Data $catalogRuleData, \Magento\Framework\App\Cache\TypeListInterface $cacheTypesList, + \Magento\Framework\Stdlib\DateTime $dateTime, \Magento\CatalogRule\Model\Indexer\Rule\RuleProductProcessor $ruleProductProcessor, - \Magento\CatalogRule\Model\Data\Condition\Converter $ruleConditionConverter, \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null, array $relatedCacheTypes = [], @@ -187,8 +193,8 @@ public function __construct( $this->_catalogRuleData = $catalogRuleData; $this->_cacheTypesList = $cacheTypesList; $this->_relatedCacheTypes = $relatedCacheTypes; + $this->dateTime = $dateTime; $this->_ruleProductProcessor = $ruleProductProcessor; - $this->ruleConditionConverter = $ruleConditionConverter; parent::__construct( $context, @@ -652,7 +658,7 @@ public function setIsActive($isActive) */ public function getRuleCondition() { - return $this->ruleConditionConverter->arrayToDataModel($this->getConditions()->asArray()); + return $this->getRuleConditionConverter()->arrayToDataModel($this->getConditions()->asArray()); } /** @@ -662,7 +668,7 @@ public function setRuleCondition($condition) { $this->getConditions() ->setConditions([]) - ->loadArray($this->ruleConditionConverter->dataModelToArray($condition)); + ->loadArray($this->getRuleConditionConverter()->dataModelToArray($condition)); return $this; } @@ -766,5 +772,17 @@ public function setExtensionAttributes(\Magento\CatalogRule\Api\Data\RuleExtensi { return $this->_setExtensionAttributes($extensionAttributes); } + + /** + * @return Data\Condition\Converter + */ + private function getRuleConditionConverter() + { + if (null === $this->ruleConditionConverter) { + $this->ruleConditionConverter = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\CatalogRule\Model\Data\Condition\Converter'); + } + return $this->ruleConditionConverter; + } //@codeCoverageIgnoreEnd } diff --git a/app/code/Magento/CatalogRule/Test/Unit/Model/Indexer/IndexBuilderTest.php b/app/code/Magento/CatalogRule/Test/Unit/Model/Indexer/IndexBuilderTest.php index a9e50b70d7bf9..5e907e827d2ae 100644 --- a/app/code/Magento/CatalogRule/Test/Unit/Model/Indexer/IndexBuilderTest.php +++ b/app/code/Magento/CatalogRule/Test/Unit/Model/Indexer/IndexBuilderTest.php @@ -42,7 +42,6 @@ class IndexBuilderTest extends \PHPUnit_Framework_TestCase */ protected $priceCurrency; - /** * @var \Magento\Eav\Model\Config|\PHPUnit_Framework_MockObject_MockObject */ @@ -202,9 +201,12 @@ protected function setUp() $this->eavConfig, $this->dateFormat, $this->dateTime, - $this->productFactory, - $this->metadataPool + $this->productFactory ); + + $this->setProperties($this->indexBuilder, [ + 'metadataPool' => $this->metadataPool + ]); } /** @@ -254,4 +256,20 @@ public function testUpdateCatalogRuleGroupWebsiteData() $this->indexBuilder->reindexByIds([1]); } + + /** + * @param $object + * @param array $properties + */ + private function setProperties($object, $properties = []) + { + $reflectionClass = new \ReflectionClass(get_class($object)); + foreach ($properties as $key => $value) { + if ($reflectionClass->hasProperty($key)) { + $reflectionProperty = $reflectionClass->getProperty($key); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue($object, $value); + } + } + } } diff --git a/app/code/Magento/CatalogSearch/Model/Adapter/Mysql/Filter/Preprocessor.php b/app/code/Magento/CatalogSearch/Model/Adapter/Mysql/Filter/Preprocessor.php index 31eb0e6df23f2..44be75ea5da6f 100644 --- a/app/code/Magento/CatalogSearch/Model/Adapter/Mysql/Filter/Preprocessor.php +++ b/app/code/Magento/CatalogSearch/Model/Adapter/Mysql/Filter/Preprocessor.php @@ -70,7 +70,6 @@ class Preprocessor implements PreprocessorInterface * @param Config $config * @param ResourceConnection $resource * @param TableMapper $tableMapper - * @param MetadataPool $metadataPool * @param string $attributePrefix */ public function __construct( @@ -79,7 +78,6 @@ public function __construct( Config $config, ResourceConnection $resource, TableMapper $tableMapper, - MetadataPool $metadataPool, $attributePrefix ) { $this->conditionManager = $conditionManager; @@ -88,7 +86,6 @@ public function __construct( $this->resource = $resource; $this->connection = $resource->getConnection(); $this->attributePrefix = $attributePrefix; - $this->metadataPool = $metadataPool; $this->tableMapper = $tableMapper; } @@ -110,7 +107,7 @@ private function processQueryWithField(FilterInterface $filter, $isNegation, $qu { /** @var Attribute $attribute */ $attribute = $this->config->getAttribute(Product::ENTITY, $filter->getField()); - $linkIdField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(); + $linkIdField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(); if ($filter->getField() === 'price') { $resultQuery = str_replace( $this->connection->quoteIdentifier('price'), @@ -182,7 +179,7 @@ private function processRangeNumeric(FilterInterface $filter, $query, $attribute $tableSuffix = $attribute->getBackendType() === 'decimal' ? '_decimal' : ''; $table = $this->resource->getTableName("catalog_product_index_eav{$tableSuffix}"); $select = $this->connection->select(); - $linkIdField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(); + $linkIdField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(); $currentStoreId = $this->scopeResolver->getScope()->getId(); @@ -229,4 +226,18 @@ private function processTermSelect(FilterInterface $filter, $isNegation) return $resultQuery; } + + /** + * Get product metadata pool + * + * @return \Magento\Framework\Model\Entity\MetadataPool + */ + protected function getMetadataPool() + { + if (!$this->metadataPool) { + $this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance() + ->get(\Magento\Framework\Model\Entity\MetadataPool::class); + } + return $this->metadataPool; + } } diff --git a/app/code/Magento/CatalogSearch/Model/ResourceModel/Advanced/Collection.php b/app/code/Magento/CatalogSearch/Model/ResourceModel/Advanced/Collection.php index 5a321fcd3b385..7b7bb4eea0019 100644 --- a/app/code/Magento/CatalogSearch/Model/ResourceModel/Advanced/Collection.php +++ b/app/code/Magento/CatalogSearch/Model/ResourceModel/Advanced/Collection.php @@ -56,21 +56,19 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection * @param \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper * @param \Magento\Framework\Validator\UniversalFactory $universalFactory * @param \Magento\Store\Model\StoreManagerInterface $storeManager - * @param \Magento\Framework\Module\Manager $moduleManager , + * @param \Magento\Framework\Module\Manager $moduleManager * @param \Magento\Catalog\Model\Indexer\Product\Flat\State $catalogProductFlatState * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig - * @param \Magento\Catalog\Model\Product\OptionFactory $productOptionFactory + * @param Product\OptionFactory $productOptionFactory * @param \Magento\Catalog\Model\ResourceModel\Url $catalogUrl * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate * @param \Magento\Customer\Model\Session $customerSession * @param \Magento\Framework\Stdlib\DateTime $dateTime * @param \Magento\Customer\Api\GroupManagementInterface $groupManagement - * @param \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation $productLimitation - * @param \Magento\Search\Api\SearchInterface $search + * @param \Magento\CatalogSearch\Model\Advanced\Request\Builder $requestBuilder + * @param \Magento\Search\Model\SearchEngine $searchEngine * @param \Magento\Framework\Search\Adapter\Mysql\TemporaryStorageFactory $temporaryStorageFactory - * @param SearchCriteriaBuilder $searchCriteriaBuilder - * @param FilterBuilder $filterBuilder - * @param \Magento\Framework\DB\Adapter\AdapterInterface|null $connection + * @param \Zend_Db_Adapter_Abstract $connection * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct( @@ -93,17 +91,14 @@ public function __construct( \Magento\Customer\Model\Session $customerSession, \Magento\Framework\Stdlib\DateTime $dateTime, \Magento\Customer\Api\GroupManagementInterface $groupManagement, - \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation $productLimitation, - \Magento\Search\Api\SearchInterface $search, + \Magento\CatalogSearch\Model\Advanced\Request\Builder $requestBuilder, + \Magento\Search\Model\SearchEngine $searchEngine, \Magento\Framework\Search\Adapter\Mysql\TemporaryStorageFactory $temporaryStorageFactory, - SearchCriteriaBuilder $searchCriteriaBuilder, - FilterBuilder $filterBuilder, - \Magento\Framework\DB\Adapter\AdapterInterface $connection = null + $connection = null ) { - $this->search = $search; + $this->requestBuilder = $requestBuilder; + $this->searchEngine = $searchEngine; $this->temporaryStorageFactory = $temporaryStorageFactory; - $this->searchCriteriaBuilder = $searchCriteriaBuilder; - $this->filterBuilder = $filterBuilder; parent::__construct( $entityFactory, $logger, @@ -124,7 +119,6 @@ public function __construct( $customerSession, $dateTime, $groupManagement, - $productLimitation, $connection ); } @@ -156,9 +150,9 @@ protected function _renderFiltersBefore() $this->addAttributeToSearch($attributeCode, $attributeValue); } } - $searchCriteria = $this->searchCriteriaBuilder->create(); + $searchCriteria = $this->getSearchCriteriaBuilder()->create(); $searchCriteria->setRequestName('advanced_search_container'); - $searchResult = $this->search->search($searchCriteria); + $searchResult = $this->getSearch()->search($searchCriteria); $temporaryStorage = $this->temporaryStorageFactory->create(); $table = $temporaryStorage->storeApiDocuments($searchResult->getItems()); @@ -200,17 +194,17 @@ private function addAttributeToSearch($attributeCode, $attributeValue) if (isset($attributeValue['from']) || isset($attributeValue['to'])) { $this->addRangeAttributeToSearch($attributeCode, $attributeValue); } elseif (!is_array($attributeValue)) { - $this->filterBuilder->setField($attributeCode)->setValue($attributeValue); - $this->searchCriteriaBuilder->addFilter($this->filterBuilder->create()); + $this->getFilterBuilder()->setField($attributeCode)->setValue($attributeValue); + $this->getSearchCriteriaBuilder()->addFilter($this->getFilterBuilder()->create()); } elseif (isset($attributeValue['like'])) { - $this->filterBuilder->setField($attributeCode)->setValue($attributeValue['like']); - $this->searchCriteriaBuilder->addFilter($this->filterBuilder->create()); + $this->getFilterBuilder()->setField($attributeCode)->setValue($attributeValue['like']); + $this->getSearchCriteriaBuilder()->addFilter($this->getFilterBuilder()->create()); } elseif (isset($attributeValue['in'])) { - $this->filterBuilder->setField($attributeCode)->setValue($attributeValue['in']); - $this->searchCriteriaBuilder->addFilter($this->filterBuilder->create()); + $this->getFilterBuilder()->setField($attributeCode)->setValue($attributeValue['in']); + $this->getSearchCriteriaBuilder()->addFilter($this->getFilterBuilder()->create()); } elseif (isset($attributeValue['in_set'])) { - $this->filterBuilder->setField($attributeCode)->setValue($attributeValue['in_set']); - $this->searchCriteriaBuilder->addFilter($this->filterBuilder->create()); + $this->getFilterBuilder()->setField($attributeCode)->setValue($attributeValue['in_set']); + $this->getSearchCriteriaBuilder()->addFilter($this->getFilterBuilder()->create()); } } @@ -224,12 +218,48 @@ private function addAttributeToSearch($attributeCode, $attributeValue) private function addRangeAttributeToSearch($attributeCode, $attributeValue) { if (isset($attributeValue['from']) && '' !== $attributeValue['from']) { - $this->filterBuilder->setField("{$attributeCode}.from")->setValue($attributeValue['from']); - $this->searchCriteriaBuilder->addFilter($this->filterBuilder->create()); + $this->getFilterBuilder()->setField("{$attributeCode}.from")->setValue($attributeValue['from']); + $this->getSearchCriteriaBuilder()->addFilter($this->getFilterBuilder()->create()); } if (isset($attributeValue['to']) && '' !== $attributeValue['to']) { - $this->filterBuilder->setField("{$attributeCode}.to")->setValue($attributeValue['to']); - $this->searchCriteriaBuilder->addFilter($this->filterBuilder->create()); + $this->getFilterBuilder()->setField("{$attributeCode}.to")->setValue($attributeValue['to']); + $this->getSearchCriteriaBuilder()->addFilter($this->getFilterBuilder()->create()); } } + + /** + * @return \Magento\Search\Api\SearchInterface + */ + private function getSearch() + { + if (null === $this->search) { + $this->search = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Search\Api\SearchInterface'); + } + return $this->search; + } + + /** + * @return SearchCriteriaBuilder + */ + private function getSearchCriteriaBuilder() + { + if (null === $this->searchCriteriaBuilder) { + $this->searchCriteriaBuilder = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Framework\Api\Search\SearchCriteriaBuilder'); + } + return $this->searchCriteriaBuilder; + } + + /** + * @return FilterBuilder + */ + private function getFilterBuilder() + { + if (null === $this->filterBuilder) { + $this->filterBuilder = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\Framework\Api\FilterBuilder'); + } + return $this->filterBuilder; + } } diff --git a/app/code/Magento/CatalogSearch/Model/ResourceModel/Fulltext/Collection.php b/app/code/Magento/CatalogSearch/Model/ResourceModel/Fulltext/Collection.php index b57fd76bab0f0..ba0729d0d82f1 100644 --- a/app/code/Magento/CatalogSearch/Model/ResourceModel/Fulltext/Collection.php +++ b/app/code/Magento/CatalogSearch/Model/ResourceModel/Fulltext/Collection.php @@ -103,7 +103,6 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection * @param \Magento\Customer\Model\Session $customerSession * @param \Magento\Framework\Stdlib\DateTime $dateTime * @param \Magento\Customer\Api\GroupManagementInterface $groupManagement - * @param \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation $productLimitation * @param \Magento\Search\Model\QueryFactory $catalogSearchData * @param \Magento\Framework\Search\Request\Builder $requestBuilder * @param \Magento\Search\Model\SearchEngine $searchEngine @@ -132,7 +131,6 @@ public function __construct( \Magento\Customer\Model\Session $customerSession, \Magento\Framework\Stdlib\DateTime $dateTime, \Magento\Customer\Api\GroupManagementInterface $groupManagement, - \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation $productLimitation, \Magento\Search\Model\QueryFactory $catalogSearchData, \Magento\Framework\Search\Request\Builder $requestBuilder, \Magento\Search\Model\SearchEngine $searchEngine, @@ -161,7 +159,6 @@ public function __construct( $customerSession, $dateTime, $groupManagement, - $productLimitation, $connection ); $this->requestBuilder = $requestBuilder; diff --git a/app/code/Magento/CatalogSearch/Model/ResourceModel/Search/Collection.php b/app/code/Magento/CatalogSearch/Model/ResourceModel/Search/Collection.php index 9e871e309806c..4be7bfa909242 100644 --- a/app/code/Magento/CatalogSearch/Model/ResourceModel/Search/Collection.php +++ b/app/code/Magento/CatalogSearch/Model/ResourceModel/Search/Collection.php @@ -57,7 +57,6 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection * @param \Magento\Customer\Model\Session $customerSession * @param \Magento\Framework\Stdlib\DateTime $dateTime * @param \Magento\Customer\Api\GroupManagementInterface $groupManagement - * @param \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation $productLimitation * @param \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $attributeCollectionFactory * @param \Magento\Framework\DB\Adapter\AdapterInterface $connection * @@ -83,7 +82,6 @@ public function __construct( \Magento\Customer\Model\Session $customerSession, \Magento\Framework\Stdlib\DateTime $dateTime, \Magento\Customer\Api\GroupManagementInterface $groupManagement, - \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation $productLimitation, \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $attributeCollectionFactory, \Magento\Framework\DB\Adapter\AdapterInterface $connection = null ) { @@ -108,7 +106,6 @@ public function __construct( $customerSession, $dateTime, $groupManagement, - $productLimitation, $connection ); } diff --git a/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Advanced/CollectionTest.php b/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Advanced/CollectionTest.php index 9b6e7801cbe9c..3a77ea5feb70f 100644 --- a/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Advanced/CollectionTest.php +++ b/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Advanced/CollectionTest.php @@ -65,6 +65,13 @@ protected function setUp() ); $this->search = $this->getMock('Magento\Search\Api\SearchInterface', [], [], '', false); + $this->prepareObjectManager([ + [ + 'Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation', + $this->getMock('Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation') + ], + ]); + $this->advancedCollection = $helper->getObject( 'Magento\CatalogSearch\Model\ResourceModel\Advanced\Collection', [ @@ -142,4 +149,20 @@ protected function getCriteriaBuilder() ->getMock(); return $criteriaBuilder; } + + /** + * @param $map + */ + private function prepareObjectManager($map) + { + $objectManagerMock = $this->getMock('Magento\Framework\ObjectManagerInterface'); + $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($map)); + $reflectionClass = new \ReflectionClass('Magento\Framework\App\ObjectManager'); + $reflectionProperty = $reflectionClass->getProperty('_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue($objectManagerMock); + } } diff --git a/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Fulltext/CollectionTest.php b/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Fulltext/CollectionTest.php index d1835bc07f567..d1415335dd448 100644 --- a/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Fulltext/CollectionTest.php +++ b/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Fulltext/CollectionTest.php @@ -32,6 +32,13 @@ protected function setUp() $criteriaBuilder = $this->getCriteriaBuilder(); $filterBuilder = $this->getFilterBuilder(); + $this->prepareObjectManager([ + [ + 'Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation', + $this->getMock('Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation') + ], + ]); + $this->model = $helper->getObject( 'Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection', [ @@ -110,4 +117,20 @@ protected function getFilterBuilder() $filterBuilder->expects($this->once())->method('create')->willReturn($this->filter); return $filterBuilder; } + + /** + * @param $map + */ + private function prepareObjectManager($map) + { + $objectManagerMock = $this->getMock('Magento\Framework\ObjectManagerInterface'); + $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($map)); + $reflectionClass = new \ReflectionClass('Magento\Framework\App\ObjectManager'); + $reflectionProperty = $reflectionClass->getProperty('_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue($objectManagerMock); + } } diff --git a/app/code/Magento/CatalogUrlRewrite/Service/V1/StoreViewService.php b/app/code/Magento/CatalogUrlRewrite/Service/V1/StoreViewService.php index d1d53e5fcce61..6cd04b94fd2c6 100644 --- a/app/code/Magento/CatalogUrlRewrite/Service/V1/StoreViewService.php +++ b/app/code/Magento/CatalogUrlRewrite/Service/V1/StoreViewService.php @@ -33,16 +33,13 @@ class StoreViewService /** * @param Config $eavConfig * @param \Magento\Framework\App\ResourceConnection $resource - * @param MetadataPool $metadataPool */ public function __construct( Config $eavConfig, - ResourceConnection $resource, - MetadataPool $metadataPool + ResourceConnection $resource ) { $this->eavConfig = $eavConfig; $this->connection = $resource->getConnection(); - $this->metadataPool = $metadataPool; } /** @@ -91,7 +88,7 @@ protected function doesEntityHaveOverriddenUrlAttributeForStore($storeId, $entit } $linkFieldName = $attribute->getEntity()->getLinkField(); if (!$linkFieldName) { - $linkFieldName = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(); + $linkFieldName = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(); } $select = $this->connection->select() ->from(['e' => $attribute->getEntity()->getEntityTable()], []) @@ -104,4 +101,18 @@ protected function doesEntityHaveOverriddenUrlAttributeForStore($storeId, $entit return in_array($storeId, $this->connection->fetchCol($select)); } + + /** + * Get product metadata pool + * + * @return \Magento\Framework\Model\Entity\MetadataPool + */ + private function getMetadataPool() + { + if (!$this->metadataPool) { + $this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance() + ->get(\Magento\Framework\Model\Entity\MetadataPool::class); + } + return $this->metadataPool; + } } diff --git a/app/code/Magento/Config/Test/Unit/Model/Config/Backend/Email/LogoTest.php b/app/code/Magento/Config/Test/Unit/Model/Config/Backend/Email/LogoTest.php index 8084e9fcea4aa..d439e97566450 100644 --- a/app/code/Magento/Config/Test/Unit/Model/Config/Backend/Email/LogoTest.php +++ b/app/code/Magento/Config/Test/Unit/Model/Config/Backend/Email/LogoTest.php @@ -61,6 +61,7 @@ protected function setUp() $this->typeListMock = $this->getMockBuilder('Magento\Framework\App\Cache\TypeListInterface') ->getMockForAbstractClass(); $this->uploaderFactoryMock = $this->getMockBuilder('Magento\MediaStorage\Model\File\UploaderFactory') + ->disableOriginalConstructor() ->setMethods(['create']) ->getMock(); $this->requestDataMock diff --git a/app/code/Magento/Config/Test/Unit/Model/Config/Backend/FileTest.php b/app/code/Magento/Config/Test/Unit/Model/Config/Backend/FileTest.php index 74d697d44b0be..7321297b26b41 100644 --- a/app/code/Magento/Config/Test/Unit/Model/Config/Backend/FileTest.php +++ b/app/code/Magento/Config/Test/Unit/Model/Config/Backend/FileTest.php @@ -17,6 +17,10 @@ use Magento\MediaStorage\Model\File\Uploader; use Magento\MediaStorage\Model\File\UploaderFactory; +/** + * Class FileTest + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ class FileTest extends \PHPUnit_Framework_TestCase { /** @var File */ @@ -61,6 +65,7 @@ protected function setUp() $this->typeListMock = $this->getMockBuilder('Magento\Framework\App\Cache\TypeListInterface') ->getMockForAbstractClass(); $this->uploaderFactoryMock = $this->getMockBuilder('Magento\MediaStorage\Model\File\UploaderFactory') + ->disableOriginalConstructor() ->setMethods(['create']) ->getMock(); $this->requestDataMock diff --git a/app/code/Magento/ConfigurableProduct/Model/ResourceModel/Indexer/Stock/Configurable.php b/app/code/Magento/ConfigurableProduct/Model/ResourceModel/Indexer/Stock/Configurable.php index 00a860c22da73..551df288ebcb0 100644 --- a/app/code/Magento/ConfigurableProduct/Model/ResourceModel/Indexer/Stock/Configurable.php +++ b/app/code/Magento/ConfigurableProduct/Model/ResourceModel/Indexer/Stock/Configurable.php @@ -25,7 +25,7 @@ class Configurable extends \Magento\CatalogInventory\Model\ResourceModel\Indexer */ protected function _getStockStatusSelect($entityIds = null, $usePrimaryTable = false) { - $metadata = $this->metadataPool->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class); + $metadata = $this->getMetadataPool()->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class); $connection = $this->getConnection(); $idxTable = $usePrimaryTable ? $this->getMainTable() : $this->getIdxTable(); $select = $connection->select()->from(['e' => $this->getTable('catalog_product_entity')], ['entity_id']); diff --git a/app/code/Magento/ConfigurableProduct/Model/ResourceModel/Product/Indexer/Price/Configurable.php b/app/code/Magento/ConfigurableProduct/Model/ResourceModel/Product/Indexer/Price/Configurable.php index b1a74e035b72d..4e6fa8ae5210d 100644 --- a/app/code/Magento/ConfigurableProduct/Model/ResourceModel/Product/Indexer/Price/Configurable.php +++ b/app/code/Magento/ConfigurableProduct/Model/ResourceModel/Product/Indexer/Price/Configurable.php @@ -71,7 +71,7 @@ protected function reindex($entityIds = null) */ private function getRelatedProducts($entityIds) { - $metadata = $this->metadataPool->getMetadata(ProductInterface::class); + $metadata = $this->getMetadataPool()->getMetadata(ProductInterface::class); $select = $this->getConnection()->select()->union( [ $this->getConnection()->select() @@ -158,7 +158,7 @@ protected function _prepareConfigurableOptionPriceTable() */ protected function _applyConfigurableOption() { - $metadata = $this->metadataPool->getMetadata(ProductInterface::class); + $metadata = $this->getMetadataPool()->getMetadata(ProductInterface::class); $connection = $this->getConnection(); $coaTable = $this->_getConfigurableOptionAggregateTable(); $copTable = $this->_getConfigurableOptionPriceTable(); diff --git a/app/code/Magento/ConfigurableProduct/Model/ResourceModel/Product/Type/Configurable/Product/Collection.php b/app/code/Magento/ConfigurableProduct/Model/ResourceModel/Product/Type/Configurable/Product/Collection.php index d94a7e3d5f82f..7b09ad4d8359b 100644 --- a/app/code/Magento/ConfigurableProduct/Model/ResourceModel/Product/Type/Configurable/Product/Collection.php +++ b/app/code/Magento/ConfigurableProduct/Model/ResourceModel/Product/Type/Configurable/Product/Collection.php @@ -50,7 +50,6 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection * @param \Magento\Customer\Model\Session $customerSession * @param \Magento\Framework\Stdlib\DateTime $dateTime * @param GroupManagementInterface $groupManagement - * @param \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation $productLimitation * @param MetadataPool $metadataPool * @param \Magento\Framework\DB\Adapter\AdapterInterface|null $connection * @@ -76,7 +75,6 @@ public function __construct( \Magento\Customer\Model\Session $customerSession, \Magento\Framework\Stdlib\DateTime $dateTime, GroupManagementInterface $groupManagement, - \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation $productLimitation, MetadataPool $metadataPool, \Magento\Framework\DB\Adapter\AdapterInterface $connection = null ) { @@ -101,7 +99,6 @@ public function __construct( $customerSession, $dateTime, $groupManagement, - $productLimitation, $connection ); } diff --git a/app/code/Magento/Downloadable/Model/ResourceModel/Indexer/Price.php b/app/code/Magento/Downloadable/Model/ResourceModel/Indexer/Price.php index d6ad051075f6e..e7e459450b991 100644 --- a/app/code/Magento/Downloadable/Model/ResourceModel/Indexer/Price.php +++ b/app/code/Magento/Downloadable/Model/ResourceModel/Indexer/Price.php @@ -97,7 +97,7 @@ protected function _applyDownloadableLink() $this->_prepareDownloadableLinkPriceTable(); $dlType = $this->_getAttribute('links_purchased_separately'); - $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(); + $linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(); $ifPrice = $connection->getIfNullSql('dlpw.price_id', 'dlpd.price'); diff --git a/app/code/Magento/DownloadableImportExport/Test/Unit/Model/Import/Product/Type/DownloadableTest.php b/app/code/Magento/DownloadableImportExport/Test/Unit/Model/Import/Product/Type/DownloadableTest.php index cdf77b1bc65b5..323f0f3caa584 100644 --- a/app/code/Magento/DownloadableImportExport/Test/Unit/Model/Import/Product/Type/DownloadableTest.php +++ b/app/code/Magento/DownloadableImportExport/Test/Unit/Model/Import/Product/Type/DownloadableTest.php @@ -699,6 +699,15 @@ public function testSetUploaderDirFalse($newSku, $bunch, $allowImport) ], ] ); + + $metadataPoolMock = $this + ->getMock('Magento\Framework\Model\Entity\MetadataPool', ['getLinkField'], [], '', false); + $metadataPoolMock->expects($this->any())->method('getMetadata')->willReturnSelf(); + + $this->prepareObjectManager([ + ['Magento\Framework\Model\Entity\MetadataPool', $metadataPoolMock], + ]); + $this->downloadableModelMock = $this->objectManagerHelper->getObject( '\Magento\DownloadableImportExport\Model\Import\Product\Type\Downloadable', [ @@ -855,4 +864,20 @@ protected function setPropertyValue(&$object, $property, $value) $reflectionProperty->setValue($object, $value); return $object; } + + /** + * @param $map + */ + private function prepareObjectManager($map) + { + $objectManagerMock = $this->getMock('Magento\Framework\ObjectManagerInterface'); + $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($map)); + $reflectionClass = new \ReflectionClass('Magento\Framework\App\ObjectManager'); + $reflectionProperty = $reflectionClass->getProperty('_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue($objectManagerMock); + } } diff --git a/app/code/Magento/Eav/Model/ResourceModel/Entity/Attribute.php b/app/code/Magento/Eav/Model/ResourceModel/Entity/Attribute.php index 4b98a0f4e63ba..e07a9f13a84d5 100644 --- a/app/code/Magento/Eav/Model/ResourceModel/Entity/Attribute.php +++ b/app/code/Magento/Eav/Model/ResourceModel/Entity/Attribute.php @@ -133,10 +133,10 @@ private function _getMaxSortOrder(AbstractModel $object) /** * Delete entity * - * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $object + * @param \Magento\Framework\Model\AbstractMode $object * @return $this */ - public function deleteEntity(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $object) + public function deleteEntity(\Magento\Framework\Model\AbstractModel $object) { if (!$object->getEntityAttributeId()) { return $this; diff --git a/app/code/Magento/GroupedProduct/Model/ResourceModel/Indexer/Stock/Grouped.php b/app/code/Magento/GroupedProduct/Model/ResourceModel/Indexer/Stock/Grouped.php index 532969ec946aa..9587a195b5e7c 100644 --- a/app/code/Magento/GroupedProduct/Model/ResourceModel/Indexer/Stock/Grouped.php +++ b/app/code/Magento/GroupedProduct/Model/ResourceModel/Indexer/Stock/Grouped.php @@ -32,7 +32,7 @@ protected function _getStockStatusSelect($entityIds = null, $usePrimaryTable = f ); $this->_addWebsiteJoinToSelect($select, true); $this->_addProductWebsiteJoinToSelect($select, 'cw.website_id', 'e.entity_id'); - $metadata = $this->metadataPool->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class); + $metadata = $this->getMetadataPool()->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class); $select->columns( 'cw.website_id' )->join( diff --git a/app/code/Magento/GroupedProduct/Model/ResourceModel/Product/Indexer/Price/Grouped.php b/app/code/Magento/GroupedProduct/Model/ResourceModel/Product/Indexer/Price/Grouped.php index 130244829ad28..718f88e4dca91 100644 --- a/app/code/Magento/GroupedProduct/Model/ResourceModel/Product/Indexer/Price/Grouped.php +++ b/app/code/Magento/GroupedProduct/Model/ResourceModel/Product/Indexer/Price/Grouped.php @@ -58,7 +58,7 @@ protected function _prepareGroupedProductPriceData($entityIds = null) } $connection = $this->getConnection(); $table = $this->getIdxTable(); - $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(); + $linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(); $select = $connection->select()->from( ['e' => $this->getTable('catalog_product_entity')], 'entity_id' diff --git a/app/code/Magento/GroupedProduct/Model/ResourceModel/Product/Link.php b/app/code/Magento/GroupedProduct/Model/ResourceModel/Product/Link.php index 41480d5927e99..99e4217e51af2 100644 --- a/app/code/Magento/GroupedProduct/Model/ResourceModel/Product/Link.php +++ b/app/code/Magento/GroupedProduct/Model/ResourceModel/Product/Link.php @@ -6,6 +6,7 @@ namespace Magento\GroupedProduct\Model\ResourceModel\Product; use Magento\Catalog\Api\Data\ProductInterface; +use Magento\Catalog\Model\ResourceModel\Product\Relation; use Magento\Framework\Model\Entity\MetadataPool; /** @@ -21,18 +22,22 @@ class Link extends \Magento\Catalog\Model\ResourceModel\Product\Link protected $metadataPool; /** + * Link constructor. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context + * @param Relation $catalogProductRelation * @param MetadataPool $metadataPool - * @param null $connectionName + * @param string|null $connectionName */ public function __construct( \Magento\Framework\Model\ResourceModel\Db\Context $context, + Relation $catalogProductRelation, MetadataPool $metadataPool, $connectionName = null ) { $this->metadataPool = $metadataPool; parent::__construct( $context, + $catalogProductRelation, $connectionName ); } diff --git a/app/code/Magento/GroupedProduct/Model/ResourceModel/Product/Type/Grouped/AssociatedProductsCollection.php b/app/code/Magento/GroupedProduct/Model/ResourceModel/Product/Type/Grouped/AssociatedProductsCollection.php index b650292f92deb..7541a39b2b5e6 100644 --- a/app/code/Magento/GroupedProduct/Model/ResourceModel/Product/Type/Grouped/AssociatedProductsCollection.php +++ b/app/code/Magento/GroupedProduct/Model/ResourceModel/Product/Type/Grouped/AssociatedProductsCollection.php @@ -48,7 +48,6 @@ class AssociatedProductsCollection extends \Magento\Catalog\Model\ResourceModel\ * @param \Magento\Customer\Model\Session $customerSession * @param \Magento\Framework\Stdlib\DateTime $dateTime * @param \Magento\Customer\Api\GroupManagementInterface $groupManagement - * @param \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation $productLimitation * @param \Magento\Framework\Registry $coreRegistry * @param \Magento\Catalog\Model\ProductTypes\ConfigInterface $config * @param \Magento\Framework\DB\Adapter\AdapterInterface|null $connection @@ -75,7 +74,6 @@ public function __construct( \Magento\Customer\Model\Session $customerSession, \Magento\Framework\Stdlib\DateTime $dateTime, \Magento\Customer\Api\GroupManagementInterface $groupManagement, - \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation $productLimitation, \Magento\Framework\Registry $coreRegistry, \Magento\Catalog\Model\ProductTypes\ConfigInterface $config, \Magento\Framework\DB\Adapter\AdapterInterface $connection = null @@ -102,7 +100,6 @@ public function __construct( $customerSession, $dateTime, $groupManagement, - $productLimitation, $connection ); } diff --git a/app/code/Magento/Reports/Model/ResourceModel/Product/Collection.php b/app/code/Magento/Reports/Model/ResourceModel/Product/Collection.php index 3ac7de4894524..34af05abd488b 100644 --- a/app/code/Magento/Reports/Model/ResourceModel/Product/Collection.php +++ b/app/code/Magento/Reports/Model/ResourceModel/Product/Collection.php @@ -82,7 +82,6 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection * @param \Magento\Customer\Model\Session $customerSession * @param \Magento\Framework\Stdlib\DateTime $dateTime * @param \Magento\Customer\Api\GroupManagementInterface $groupManagement - * @param \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation $productLimitation * @param \Magento\Catalog\Model\ResourceModel\Product $product * @param \Magento\Reports\Model\Event\TypeFactory $eventTypeFactory * @param \Magento\Catalog\Model\Product\Type $productType @@ -111,7 +110,6 @@ public function __construct( \Magento\Customer\Model\Session $customerSession, \Magento\Framework\Stdlib\DateTime $dateTime, \Magento\Customer\Api\GroupManagementInterface $groupManagement, - \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation $productLimitation, \Magento\Catalog\Model\ResourceModel\Product $product, \Magento\Reports\Model\Event\TypeFactory $eventTypeFactory, \Magento\Catalog\Model\Product\Type $productType, @@ -141,7 +139,6 @@ public function __construct( $customerSession, $dateTime, $groupManagement, - $productLimitation, $connection ); $this->_eventTypeFactory = $eventTypeFactory; diff --git a/app/code/Magento/Reports/Model/ResourceModel/Product/Index/Collection/AbstractCollection.php b/app/code/Magento/Reports/Model/ResourceModel/Product/Index/Collection/AbstractCollection.php index 6bae99e482467..36043a828173b 100644 --- a/app/code/Magento/Reports/Model/ResourceModel/Product/Index/Collection/AbstractCollection.php +++ b/app/code/Magento/Reports/Model/ResourceModel/Product/Index/Collection/AbstractCollection.php @@ -49,7 +49,6 @@ abstract class AbstractCollection extends \Magento\Catalog\Model\ResourceModel\P * @param \Magento\Customer\Model\Session $customerSession * @param \Magento\Framework\Stdlib\DateTime $dateTime * @param \Magento\Customer\Api\GroupManagementInterface $groupManagement - * @param \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation $productLimitation * @param \Magento\Customer\Model\Visitor $customerVisitor * @param mixed $connection * @@ -75,7 +74,6 @@ public function __construct( \Magento\Customer\Model\Session $customerSession, \Magento\Framework\Stdlib\DateTime $dateTime, \Magento\Customer\Api\GroupManagementInterface $groupManagement, - \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation $productLimitation, \Magento\Customer\Model\Visitor $customerVisitor, \Magento\Framework\DB\Adapter\AdapterInterface $connection = null ) { @@ -99,7 +97,6 @@ public function __construct( $customerSession, $dateTime, $groupManagement, - $productLimitation, $connection ); $this->_customerVisitor = $customerVisitor; diff --git a/app/code/Magento/Reports/Model/ResourceModel/Product/Lowstock/Collection.php b/app/code/Magento/Reports/Model/ResourceModel/Product/Lowstock/Collection.php index 6f0b48ede3e93..c5fd7b09fa252 100644 --- a/app/code/Magento/Reports/Model/ResourceModel/Product/Lowstock/Collection.php +++ b/app/code/Magento/Reports/Model/ResourceModel/Product/Lowstock/Collection.php @@ -66,7 +66,6 @@ class Collection extends \Magento\Reports\Model\ResourceModel\Product\Collection * @param \Magento\Customer\Model\Session $customerSession * @param \Magento\Framework\Stdlib\DateTime $dateTime * @param \Magento\Customer\Api\GroupManagementInterface $groupManagement - * @param \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation $productLimitation * @param \Magento\Catalog\Model\ResourceModel\Product $product * @param \Magento\Reports\Model\Event\TypeFactory $eventTypeFactory * @param \Magento\Catalog\Model\Product\Type $productType @@ -98,7 +97,6 @@ public function __construct( \Magento\Customer\Model\Session $customerSession, \Magento\Framework\Stdlib\DateTime $dateTime, \Magento\Customer\Api\GroupManagementInterface $groupManagement, - \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation $productLimitation, \Magento\Catalog\Model\ResourceModel\Product $product, \Magento\Reports\Model\Event\TypeFactory $eventTypeFactory, \Magento\Catalog\Model\Product\Type $productType, @@ -128,7 +126,6 @@ public function __construct( $customerSession, $dateTime, $groupManagement, - $productLimitation, $product, $eventTypeFactory, $productType, diff --git a/app/code/Magento/Review/Model/ResourceModel/Review/Product/Collection.php b/app/code/Magento/Review/Model/ResourceModel/Review/Product/Collection.php index 053c6b59bc107..f744f9ff88e2c 100644 --- a/app/code/Magento/Review/Model/ResourceModel/Review/Product/Collection.php +++ b/app/code/Magento/Review/Model/ResourceModel/Review/Product/Collection.php @@ -79,7 +79,6 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection * @param \Magento\Customer\Model\Session $customerSession * @param \Magento\Framework\Stdlib\DateTime $dateTime * @param \Magento\Customer\Api\GroupManagementInterface $groupManagement - * @param \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation $productLimitation * @param \Magento\Review\Model\RatingFactory $ratingFactory * @param \Magento\Review\Model\Rating\Option\VoteFactory $voteFactory * @param mixed $connection @@ -106,7 +105,6 @@ public function __construct( \Magento\Customer\Model\Session $customerSession, \Magento\Framework\Stdlib\DateTime $dateTime, \Magento\Customer\Api\GroupManagementInterface $groupManagement, - \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation $productLimitation, \Magento\Review\Model\RatingFactory $ratingFactory, \Magento\Review\Model\Rating\Option\VoteFactory $voteFactory, \Magento\Framework\DB\Adapter\AdapterInterface $connection = null @@ -133,7 +131,6 @@ public function __construct( $customerSession, $dateTime, $groupManagement, - $productLimitation, $connection ); } diff --git a/dev/tests/integration/testsuite/Magento/Catalog/_files/categories.php b/dev/tests/integration/testsuite/Magento/Catalog/_files/categories.php index 3b2122fc1ac7b..d8d2e9ea117c4 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/_files/categories.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/_files/categories.php @@ -21,13 +21,20 @@ ); /** @var Magento\Catalog\Api\CategoryLinkManagementInterface $linkManagement */ -$categoryLinkManagement = $objectManager->create( - 'Magento\Catalog\Api\CategoryLinkManagementInterface', - [ - 'productRepository' => $productRepository, - 'categoryLinkRepository' => $categoryLinkRepository - ] -); +$categoryLinkManagement = $objectManager->create('Magento\Catalog\Api\CategoryLinkManagementInterface'); +$reflectionClass = new \ReflectionClass(get_class($categoryLinkManagement)); +$properties = [ + 'productRepository' => $productRepository, + 'categoryLinkRepository' => $categoryLinkRepository +]; +foreach ($properties as $key => $value) { + if ($reflectionClass->hasProperty($key)) { + $reflectionProperty = $reflectionClass->getProperty($key); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue($categoryLinkManagement, $value); + } +} + /** * After installation system has two categories: root one with ID:1 and Default category with ID:2 diff --git a/dev/tests/static/testsuite/Magento/Test/Legacy/_files/obsolete_classes.php b/dev/tests/static/testsuite/Magento/Test/Legacy/_files/obsolete_classes.php index 7ffea7dfb18ac..be9b65ed051d3 100755 --- a/dev/tests/static/testsuite/Magento/Test/Legacy/_files/obsolete_classes.php +++ b/dev/tests/static/testsuite/Magento/Test/Legacy/_files/obsolete_classes.php @@ -762,7 +762,6 @@ ], ['Magento\Catalog\Model\Product\AttributeSet\AlreadyExistsException'], ['Magento\Catalog\Model\Product\Option\Type\File\Exception'], - ['Magento\Catalog\Model\Product\Option\Converter'], [ 'Magento\Catalog\Model\Product\Option\Type\File\LargeSizeException', 'Magento\Framework\Exception\File\LargeSizeException', diff --git a/lib/internal/Magento/Framework/TestFramework/Unit/Helper/ObjectManager.php b/lib/internal/Magento/Framework/TestFramework/Unit/Helper/ObjectManager.php index a0844a4a07ba4..0682dee63ade0 100644 --- a/lib/internal/Magento/Framework/TestFramework/Unit/Helper/ObjectManager.php +++ b/lib/internal/Magento/Framework/TestFramework/Unit/Helper/ObjectManager.php @@ -159,7 +159,17 @@ public function getObject($className, array $arguments = []) } $constructArguments = $this->getConstructArguments($className, $arguments); $reflectionClass = new \ReflectionClass($className); - return $reflectionClass->newInstanceArgs($constructArguments); + $newObject = $reflectionClass->newInstanceArgs($constructArguments); + + foreach (array_diff_key($arguments, $constructArguments) as $key => $value) { + if ($reflectionClass->hasProperty($key)) { + $reflectionProperty = $reflectionClass->getProperty($key); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue($newObject, $value); + } + } + + return $newObject; } /**