Skip to content

Cleanup ObjectManager usage - Magento_Elasticsearch #26964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Elasticsearch\Elasticsearch5\Model\Adapter\BatchDataMapper;

use Magento\Elasticsearch\Model\ResourceModel\Index;
use Magento\AdvancedSearch\Model\Adapter\DataMapper\AdditionalFieldsProviderInterface;
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\AttributeProvider;
use Magento\Framework\App\ObjectManager;
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\ResolverInterface;
use Magento\Elasticsearch\Model\ResourceModel\Index;

/**
* Provide data mapping for categories fields
Expand All @@ -34,19 +34,17 @@ class CategoryFieldsProvider implements AdditionalFieldsProviderInterface

/**
* @param Index $resourceIndex
* @param AttributeProvider|null $attributeAdapterProvider
* @param ResolverInterface|null $fieldNameResolver
* @param AttributeProvider $attributeAdapterProvider
* @param ResolverInterface $fieldNameResolver
*/
public function __construct(
Index $resourceIndex,
AttributeProvider $attributeAdapterProvider = null,
ResolverInterface $fieldNameResolver = null
AttributeProvider $attributeAdapterProvider,
ResolverInterface $fieldNameResolver
) {
$this->resourceIndex = $resourceIndex;
$this->attributeAdapterProvider = $attributeAdapterProvider ?: ObjectManager::getInstance()
->get(AttributeProvider::class);
$this->fieldNameResolver = $fieldNameResolver ?: ObjectManager::getInstance()
->get(ResolverInterface::class);
$this->attributeAdapterProvider = $attributeAdapterProvider;
$this->fieldNameResolver = $fieldNameResolver;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@
namespace Magento\Elasticsearch\Elasticsearch5\Model\Adapter\DataMapper;

use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
use Magento\Elasticsearch\Model\Adapter\Container\Attribute as AttributeContainer;
use Magento\Elasticsearch\Model\Adapter\Document\Builder;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Customer\Api\Data\GroupInterface;
use Magento\Elasticsearch\Model\ResourceModel\Index;
use Magento\Elasticsearch\Model\Adapter\FieldMapperInterface;
use Magento\Elasticsearch\Model\Adapter\Container\Attribute as AttributeContainer;
use Magento\Elasticsearch\Model\Adapter\DataMapperInterface;
use Magento\Elasticsearch\Model\Adapter\FieldType\Date as DateFieldType;
use Magento\Elasticsearch\Model\Adapter\Document\Builder;
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\AttributeProvider;
use Magento\Framework\App\ObjectManager;
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\ResolverInterface;
use Magento\Elasticsearch\Model\Adapter\FieldMapperInterface;
use Magento\Elasticsearch\Model\Adapter\FieldType\Date as DateFieldType;
use Magento\Elasticsearch\Model\ResourceModel\Index;
use Magento\Store\Model\StoreManagerInterface;

/**
* Don't use this product data mapper class.
Expand Down Expand Up @@ -103,8 +102,8 @@ class ProductDataMapper implements DataMapperInterface
* @param FieldMapperInterface $fieldMapper
* @param StoreManagerInterface $storeManager
* @param DateFieldType $dateFieldType
* @param AttributeProvider|null $attributeAdapterProvider
* @param ResolverInterface|null $fieldNameResolver
* @param AttributeProvider $attributeAdapterProvider
* @param ResolverInterface $fieldNameResolver
*/
public function __construct(
Builder $builder,
Expand All @@ -113,19 +112,17 @@ public function __construct(
FieldMapperInterface $fieldMapper,
StoreManagerInterface $storeManager,
DateFieldType $dateFieldType,
AttributeProvider $attributeAdapterProvider = null,
ResolverInterface $fieldNameResolver = null
AttributeProvider $attributeAdapterProvider,
ResolverInterface $fieldNameResolver
) {
$this->builder = $builder;
$this->attributeContainer = $attributeContainer;
$this->resourceIndex = $resourceIndex;
$this->fieldMapper = $fieldMapper;
$this->storeManager = $storeManager;
$this->dateFieldType = $dateFieldType;
$this->attributeAdapterProvider = $attributeAdapterProvider ?: ObjectManager::getInstance()
->get(AttributeProvider::class);
$this->fieldNameResolver = $fieldNameResolver ?: ObjectManager::getInstance()
->get(ResolverInterface::class);
$this->attributeAdapterProvider = $attributeAdapterProvider;
$this->fieldNameResolver = $fieldNameResolver;

$this->mediaGalleryRoles = [
self::MEDIA_ROLE_IMAGE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Elasticsearch\Elasticsearch5\Model\Adapter\FieldMapper;

use Magento\Customer\Model\Session as CustomerSession;
use Magento\Eav\Model\Config;
use Magento\Elasticsearch\Elasticsearch5\Model\Adapter\FieldType;
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\AttributeProvider;
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\ResolverInterface;
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProviderInterface;
use Magento\Elasticsearch\Model\Adapter\FieldMapperInterface;
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\ResolverInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Elasticsearch\Elasticsearch5\Model\Adapter\FieldType;
use Magento\Framework\Registry;
use Magento\Store\Model\StoreManagerInterface as StoreManager;
use \Magento\Customer\Model\Session as CustomerSession;

/**
* Class ProductFieldMapper
* Elasticsearch5 Product Field Mapper Adapter
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
*/
class ProductFieldMapper implements FieldMapperInterface
{
Expand Down Expand Up @@ -73,31 +74,28 @@ class ProductFieldMapper implements FieldMapperInterface
* @param CustomerSession $customerSession
* @param StoreManager $storeManager
* @param Registry $coreRegistry
* @param ResolverInterface|null $fieldNameResolver
* @param AttributeProvider|null $attributeAdapterProvider
* @param FieldProviderInterface|null $fieldProvider
* @param ResolverInterface $fieldNameResolver
* @param AttributeProvider $attributeAdapterProvider
* @param FieldProviderInterface $fieldProvider
*/
public function __construct(
Config $eavConfig,
FieldType $fieldType,
CustomerSession $customerSession,
StoreManager $storeManager,
Registry $coreRegistry,
ResolverInterface $fieldNameResolver = null,
AttributeProvider $attributeAdapterProvider = null,
FieldProviderInterface $fieldProvider = null
ResolverInterface $fieldNameResolver,
AttributeProvider $attributeAdapterProvider,
FieldProviderInterface $fieldProvider
) {
$this->eavConfig = $eavConfig;
$this->fieldType = $fieldType;
$this->customerSession = $customerSession;
$this->storeManager = $storeManager;
$this->coreRegistry = $coreRegistry;
$this->fieldNameResolver = $fieldNameResolver ?: ObjectManager::getInstance()
->get(ResolverInterface::class);
$this->attributeAdapterProvider = $attributeAdapterProvider ?: ObjectManager::getInstance()
->get(AttributeProvider::class);
$this->fieldProvider = $fieldProvider ?: ObjectManager::getInstance()
->get(FieldProviderInterface::class);
$this->fieldNameResolver = $fieldNameResolver;
$this->attributeAdapterProvider = $attributeAdapterProvider;
$this->fieldProvider = $fieldProvider;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
*/
namespace Magento\Elasticsearch\Elasticsearch5\SearchAdapter;

use Magento\Framework\App\ObjectManager;
use Magento\Elasticsearch\SearchAdapter\Aggregation\Builder as AggregationBuilder;
use Magento\Elasticsearch\SearchAdapter\ConnectionManager;
use Magento\Elasticsearch\SearchAdapter\QueryContainerFactory;
use Magento\Elasticsearch\SearchAdapter\ResponseFactory;
use Magento\Framework\Search\AdapterInterface;
use Magento\Framework\Search\RequestInterface;
use Magento\Framework\Search\Response\QueryResponse;
use Magento\Elasticsearch\SearchAdapter\Aggregation\Builder as AggregationBuilder;
use Magento\Elasticsearch\SearchAdapter\ConnectionManager;
use \Magento\Elasticsearch\SearchAdapter\ResponseFactory;
use Psr\Log\LoggerInterface;

/**
Expand Down Expand Up @@ -44,7 +44,7 @@ class Adapter implements AdapterInterface
protected $aggregationBuilder;

/**
* @var \Magento\Elasticsearch\SearchAdapter\QueryContainerFactory
* @var QueryContainerFactory
*/
private $queryContainerFactory;

Expand All @@ -54,19 +54,15 @@ class Adapter implements AdapterInterface
* @var array
*/
private static $emptyRawResponse = [
"hits" =>
[
"hits" => []
],
"aggregations" =>
[
"price_bucket" => [],
"category_bucket" =>
[
"buckets" => []

]
'hits' => [
'hits' => []
],
'aggregations' => [
'price_bucket' => [],
'category_bucket' => [
'buckets' => []
]
]
];

/**
Expand All @@ -79,24 +75,23 @@ class Adapter implements AdapterInterface
* @param Mapper $mapper
* @param ResponseFactory $responseFactory
* @param AggregationBuilder $aggregationBuilder
* @param \Magento\Elasticsearch\SearchAdapter\QueryContainerFactory $queryContainerFactory
* @param QueryContainerFactory $queryContainerFactory
* @param LoggerInterface $logger
*/
public function __construct(
ConnectionManager $connectionManager,
Mapper $mapper,
ResponseFactory $responseFactory,
AggregationBuilder $aggregationBuilder,
\Magento\Elasticsearch\SearchAdapter\QueryContainerFactory $queryContainerFactory,
LoggerInterface $logger = null
QueryContainerFactory $queryContainerFactory,
LoggerInterface $logger
) {
$this->connectionManager = $connectionManager;
$this->mapper = $mapper;
$this->responseFactory = $responseFactory;
$this->aggregationBuilder = $aggregationBuilder;
$this->queryContainerFactory = $queryContainerFactory;
$this->logger = $logger ?: ObjectManager::getInstance()
->get(LoggerInterface::class);
$this->logger = $logger;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,20 @@ class Builder
* @param SearchIndexNameResolver $searchIndexNameResolver
* @param AggregationBuilder $aggregationBuilder
* @param ScopeResolverInterface $scopeResolver
* @param Sort|null $sortBuilder
*/
public function __construct(
Config $clientConfig,
SearchIndexNameResolver $searchIndexNameResolver,
AggregationBuilder $aggregationBuilder,
ScopeResolverInterface $scopeResolver
ScopeResolverInterface $scopeResolver,
?Sort $sortBuilder = null
) {
$this->clientConfig = $clientConfig;
$this->searchIndexNameResolver = $searchIndexNameResolver;
$this->aggregationBuilder = $aggregationBuilder;
$this->scopeResolver = $scopeResolver;
$this->sortBuilder = $sortBuilder ?: ObjectManager::getInstance()->get(Sort::class);
}

/**
Expand All @@ -88,7 +91,7 @@ public function initQuery(RequestInterface $request)
'from' => $request->getFrom(),
'size' => $request->getSize(),
'stored_fields' => ['_id', '_score'],
'sort' => $this->getSortBuilder()->getSort($request),
'sort' => $this->sortBuilder->getSort($request),
'query' => [],
],
];
Expand All @@ -109,17 +112,4 @@ public function initAggregations(
) {
return $this->aggregationBuilder->build($request, $searchQuery);
}

/**
* Get sort builder instance.
*
* @return Sort
*/
private function getSortBuilder()
{
if (null === $this->sortBuilder) {
$this->sortBuilder = ObjectManager::getInstance()->get(Sort::class);
}
return $this->sortBuilder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Elasticsearch\Model\Adapter\BatchDataMapper;

use Magento\Elasticsearch\Model\ResourceModel\Index;
use Magento\AdvancedSearch\Model\Adapter\DataMapper\AdditionalFieldsProviderInterface;
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\AttributeProvider;
use Magento\Framework\App\ObjectManager;
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\ResolverInterface;
use Magento\Elasticsearch\Model\ResourceModel\Index;

/**
* Provide data mapping for categories fields
Expand All @@ -34,19 +34,17 @@ class CategoryFieldsProvider implements AdditionalFieldsProviderInterface

/**
* @param Index $resourceIndex
* @param AttributeProvider|null $attributeAdapterProvider
* @param ResolverInterface|null $fieldNameResolver
* @param AttributeProvider $attributeAdapterProvider
* @param ResolverInterface $fieldNameResolver
*/
public function __construct(
Index $resourceIndex,
AttributeProvider $attributeAdapterProvider = null,
ResolverInterface $fieldNameResolver = null
AttributeProvider $attributeAdapterProvider,
ResolverInterface $fieldNameResolver
) {
$this->resourceIndex = $resourceIndex;
$this->attributeAdapterProvider = $attributeAdapterProvider ?: ObjectManager::getInstance()
->get(AttributeProvider::class);
$this->fieldNameResolver = $fieldNameResolver ?: ObjectManager::getInstance()
->get(ResolverInterface::class);
$this->attributeAdapterProvider = $attributeAdapterProvider;
$this->fieldNameResolver = $fieldNameResolver;
}

/**
Expand Down
Loading