Skip to content

Commit d484aae

Browse files
vgoncharenkoduhon
authored andcommitted
MAGETWO-94482: [2.3] Fixed procedure of creating mapping for dynamic fields in elasticsearch
- fix tests
1 parent 704e082 commit d484aae

File tree

13 files changed

+75
-139
lines changed

13 files changed

+75
-139
lines changed

app/code/Magento/Elasticsearch/Elasticsearch5/Model/Adapter/FieldMapper/Product/FieldProvider/FieldIndex/Converter.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
namespace Magento\Elasticsearch\Elasticsearch5\Model\Adapter\FieldMapper\Product\FieldProvider\FieldIndex;
77

88
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldIndex\ConverterInterface;
9-
use Magento\Framework\Exception\LocalizedException;
109

1110
/**
1211
* Field type converter from internal index type to elastic service.
@@ -32,12 +31,12 @@ class Converter implements ConverterInterface
3231
*
3332
* @param string $internalType
3433
* @return string|boolean
35-
* @throws LocalizedException
34+
* @throws \DomainException
3635
*/
3736
public function convert(string $internalType)
3837
{
3938
if (!isset($this->mapping[$internalType])) {
40-
throw new LocalizedException(__('Unsupported internal field index type: %1', $internalType));
39+
throw new \DomainException(sprintf('Unsupported internal field index type: %s', $internalType));
4140
}
4241
return $this->mapping[$internalType];
4342
}

app/code/Magento/Elasticsearch/Elasticsearch5/Model/Adapter/FieldMapper/Product/FieldProvider/FieldType/Converter.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
namespace Magento\Elasticsearch\Elasticsearch5\Model\Adapter\FieldMapper\Product\FieldProvider\FieldType;
77

88
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldType\ConverterInterface;
9-
use Magento\Framework\Exception\LocalizedException;
109

1110
/**
1211
* Field type converter from internal data types to elastic service.
@@ -41,12 +40,12 @@ class Converter implements ConverterInterface
4140
*
4241
* @param string $internalType
4342
* @return string
44-
* @throws LocalizedException
43+
* @throws \DomainException
4544
*/
4645
public function convert(string $internalType): string
4746
{
4847
if (!isset($this->mapping[$internalType])) {
49-
throw new LocalizedException(__('Unsupported internal field type: %1', $internalType));
48+
throw new \DomainException(sprintf('Unsupported internal field type: %s', $internalType));
5049
}
5150
return $this->mapping[$internalType];
5251
}

app/code/Magento/Elasticsearch/Elasticsearch5/Model/Adapter/FieldMapper/Product/FieldProvider/FieldType/Resolver/CompositeResolver.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,14 @@ class CompositeResolver implements ResolverInterface
2424
*/
2525
public function __construct(array $items)
2626
{
27-
$this->items = (function (ResolverInterface ...$items) {
28-
return $items;
29-
})(...$items);
27+
foreach ($items as $item) {
28+
if (!$item instanceof ResolverInterface) {
29+
throw new \InvalidArgumentException(
30+
sprintf('Instance of the field type resolver is expected, got %s instead.', get_class($item))
31+
);
32+
}
33+
}
34+
$this->items = $items;
3035
}
3136

3237
/**

app/code/Magento/Elasticsearch/Model/Adapter/FieldMapper/Product/AttributeProvider.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Magento\Eav\Model\Config;
99
use Magento\Catalog\Api\Data\ProductAttributeInterface;
1010
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\AttributeAdapter\DummyAttribute;
11-
use Magento\Framework\Exception\LocalizedException;
1211
use Psr\Log\LoggerInterface;
1312

1413
/**
@@ -66,23 +65,15 @@ public function __construct(
6665
}
6766

6867
/**
69-
* Create class instance with specified parameters
70-
*
71-
* @param string $attributeCode
72-
* @return AttributeAdapter
68+
* {@inheritdoc}
7369
*/
7470
public function getByAttributeCode(string $attributeCode): AttributeAdapter
7571
{
7672
if (!isset($this->cachedPool[$attributeCode])) {
77-
$attribute = null;
78-
try {
79-
$attribute = $this->eavConfig->getAttribute(
80-
ProductAttributeInterface::ENTITY_TYPE_CODE,
81-
$attributeCode
82-
);
83-
} catch (LocalizedException $exception) {
84-
$this->logger->critical($exception);
85-
}
73+
$attribute = $this->eavConfig->getAttribute(
74+
ProductAttributeInterface::ENTITY_TYPE_CODE,
75+
$attributeCode
76+
);
8677
if (null === $attribute) {
8778
$attribute = $this->objectManager->create(DummyAttribute::class);
8879
}

app/code/Magento/Elasticsearch/Model/Adapter/FieldMapper/Product/CompositeFieldProvider.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@ class CompositeFieldProvider implements FieldProviderInterface
2121
*/
2222
public function __construct(array $providers)
2323
{
24-
$this->providers = (function (FieldProviderInterface ...$providers) {
25-
return $providers;
26-
})(...$providers);
24+
foreach ($providers as $provider) {
25+
if (!$provider instanceof FieldProviderInterface) {
26+
throw new \InvalidArgumentException(
27+
sprintf('Instance of the field provider is expected, got %s instead.', get_class($provider))
28+
);
29+
}
30+
}
31+
$this->providers = $providers;
2732
}
2833

2934
/**

app/code/Magento/Elasticsearch/Model/Adapter/FieldMapper/Product/FieldProvider/DynamicField.php

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\ResolverInterface
1717
as FieldNameResolver;
1818
use Magento\Framework\Api\SearchCriteriaBuilder;
19-
use Magento\Framework\Exception\LocalizedException;
20-
use Psr\Log\LoggerInterface;
2119

2220
/**
2321
* Provide dynamic fields for product.
@@ -65,11 +63,6 @@ class DynamicField implements FieldProviderInterface
6563
*/
6664
private $fieldNameResolver;
6765

68-
/**
69-
* @var LoggerInterface
70-
*/
71-
private $logger;
72-
7366
/**
7467
* @param FieldTypeConverterInterface $fieldTypeConverter
7568
* @param IndexTypeConverterInterface $indexTypeConverter
@@ -78,7 +71,6 @@ class DynamicField implements FieldProviderInterface
7871
* @param CategoryListInterface $categoryList
7972
* @param FieldNameResolver $fieldNameResolver
8073
* @param AttributeProvider $attributeAdapterProvider
81-
* @param LoggerInterface $logger
8274
*/
8375
public function __construct(
8476
FieldTypeConverterInterface $fieldTypeConverter,
@@ -87,8 +79,7 @@ public function __construct(
8779
SearchCriteriaBuilder $searchCriteriaBuilder,
8880
CategoryListInterface $categoryList,
8981
FieldNameResolver $fieldNameResolver,
90-
AttributeProvider $attributeAdapterProvider,
91-
LoggerInterface $logger
82+
AttributeProvider $attributeAdapterProvider
9283
) {
9384
$this->groupRepository = $groupRepository;
9485
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
@@ -97,14 +88,10 @@ public function __construct(
9788
$this->categoryList = $categoryList;
9889
$this->fieldNameResolver = $fieldNameResolver;
9990
$this->attributeAdapterProvider = $attributeAdapterProvider;
100-
$this->logger = $logger;
10191
}
10292

10393
/**
104-
* Get mapping for dynamic fields.
105-
*
106-
* @param array $context
107-
* @return array
94+
* {@inheritdoc}
10895
*/
10996
public function getFields(array $context = []): array
11097
{
@@ -132,21 +119,17 @@ public function getFields(array $context = []): array
132119
];
133120
}
134121

135-
try {
136-
$groups = $this->groupRepository->getList($searchCriteria)->getItems();
137-
$priceAttribute = $this->attributeAdapterProvider->getByAttributeCode('price');
138-
foreach ($groups as $group) {
139-
$groupPriceKey = $this->fieldNameResolver->getFieldName(
140-
$priceAttribute,
141-
['customerGroupId' => $group->getId(), 'websiteId' => $context['websiteId']]
142-
);
143-
$allAttributes[$groupPriceKey] = [
144-
'type' => $this->fieldTypeConverter->convert(FieldTypeConverterInterface::INTERNAL_DATA_TYPE_FLOAT),
145-
'store' => true
146-
];
147-
}
148-
} catch (LocalizedException $exception) {
149-
$this->logger->critical($exception);
122+
$groups = $this->groupRepository->getList($searchCriteria)->getItems();
123+
$priceAttribute = $this->attributeAdapterProvider->getByAttributeCode('price');
124+
foreach ($groups as $group) {
125+
$groupPriceKey = $this->fieldNameResolver->getFieldName(
126+
$priceAttribute,
127+
['customerGroupId' => $group->getId(), 'websiteId' => $context['websiteId']]
128+
);
129+
$allAttributes[$groupPriceKey] = [
130+
'type' => $this->fieldTypeConverter->convert(FieldTypeConverterInterface::INTERNAL_DATA_TYPE_FLOAT),
131+
'store' => true
132+
];
150133
}
151134

152135
return $allAttributes;

app/code/Magento/Elasticsearch/Model/Adapter/FieldMapper/Product/FieldProvider/FieldIndex/Converter.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
*/
66
namespace Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldIndex;
77

8-
use Magento\Framework\Exception\LocalizedException;
9-
108
/**
119
* Field type converter from internal index type to elastic service.
1210
*/
@@ -31,12 +29,12 @@ class Converter implements ConverterInterface
3129
*
3230
* @param string $internalType
3331
* @return string|boolean
34-
* @throws LocalizedException
32+
* @throws \DomainException
3533
*/
3634
public function convert(string $internalType)
3735
{
3836
if (!isset($this->mapping[$internalType])) {
39-
throw new LocalizedException(__('Unsupported internal field index type: %1', $internalType));
37+
throw new \DomainException(sprintf('Unsupported internal field index type: %s', $internalType));
4038
}
4139
return $this->mapping[$internalType];
4240
}

app/code/Magento/Elasticsearch/Model/Adapter/FieldMapper/Product/FieldProvider/FieldName/Resolver/CategoryName.php

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77
namespace Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\Resolver;
88

99
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\AttributeAdapter;
10-
use Magento\Framework\Exception\LocalizedException;
1110
use Magento\Framework\Registry;
1211
use Magento\Store\Model\StoreManagerInterface as StoreManager;
1312
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\ResolverInterface;
1413
use Magento\Framework\App\ObjectManager;
15-
use Psr\Log\LoggerInterface;
1614

1715
/**
1816
* Resolver field name for Category name attribute.
@@ -30,21 +28,13 @@ class CategoryName implements ResolverInterface
3028
private $coreRegistry;
3129

3230
/**
33-
* @var LoggerInterface
34-
*/
35-
private $logger;
36-
37-
/**
38-
* @param LoggerInterface $logger
3931
* @param StoreManager $storeManager
4032
* @param Registry $coreRegistry
4133
*/
4234
public function __construct(
43-
LoggerInterface $logger,
4435
StoreManager $storeManager = null,
4536
Registry $coreRegistry = null
4637
) {
47-
$this->logger = $logger;
4838
$this->storeManager = $storeManager ?: ObjectManager::getInstance()
4939
->get(StoreManager::class);
5040
$this->coreRegistry = $coreRegistry ?: ObjectManager::getInstance()
@@ -68,24 +58,16 @@ public function getFieldName(AttributeAdapter $attribute, $context = []): ?strin
6858
}
6959

7060
/**
71-
* Resolve category id.
72-
*
73-
* @param array $context
74-
* @return int
61+
* {@inheritdoc}
7562
*/
7663
private function resolveCategoryId($context): int
7764
{
7865
if (isset($context['categoryId'])) {
7966
$id = $context['categoryId'];
8067
} else {
81-
$id = \Magento\Catalog\Model\Category::ROOT_CATEGORY_ID;
82-
try {
83-
$id = $this->coreRegistry->registry('current_category')
84-
? $this->coreRegistry->registry('current_category')->getId()
85-
: $this->storeManager->getStore()->getRootCategoryId();
86-
} catch (LocalizedException $exception) {
87-
$this->logger->critical($exception);
88-
}
68+
$id = $this->coreRegistry->registry('current_category')
69+
? $this->coreRegistry->registry('current_category')->getId()
70+
: $this->storeManager->getStore()->getRootCategoryId();
8971
}
9072

9173
return $id;

app/code/Magento/Elasticsearch/Model/Adapter/FieldMapper/Product/FieldProvider/FieldName/Resolver/CompositeResolver.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,14 @@ class CompositeResolver implements ResolverInterface
2424
*/
2525
public function __construct(array $items)
2626
{
27-
$this->items = (function (ResolverInterface ...$items) {
28-
return $items;
29-
})(...$items);
27+
foreach ($items as $item) {
28+
if (!$item instanceof ResolverInterface) {
29+
throw new \InvalidArgumentException(
30+
sprintf('Instance of the field name resolver is expected, got %s instead.', get_class($item))
31+
);
32+
}
33+
}
34+
$this->items = $items;
3035
}
3136

3237
/**

app/code/Magento/Elasticsearch/Model/Adapter/FieldMapper/Product/FieldProvider/FieldName/Resolver/Position.php

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77
namespace Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\Resolver;
88

99
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\AttributeAdapter;
10-
use Magento\Framework\Exception\LocalizedException;
1110
use Magento\Framework\Registry;
1211
use Magento\Store\Model\StoreManagerInterface as StoreManager;
1312
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\ResolverInterface;
1413
use Magento\Framework\App\ObjectManager;
15-
use Psr\Log\LoggerInterface;
1614

1715
/**
1816
* Resolver field name for position attribute.
@@ -30,21 +28,13 @@ class Position implements ResolverInterface
3028
private $coreRegistry;
3129

3230
/**
33-
* @var LoggerInterface
34-
*/
35-
private $logger;
36-
37-
/**
38-
* @param LoggerInterface $logger
3931
* @param StoreManager $storeManager
4032
* @param Registry $coreRegistry
4133
*/
4234
public function __construct(
43-
LoggerInterface $logger,
4435
StoreManager $storeManager = null,
4536
Registry $coreRegistry = null
4637
) {
47-
$this->logger = $logger;
4838
$this->storeManager = $storeManager ?: ObjectManager::getInstance()
4939
->get(StoreManager::class);
5040
$this->coreRegistry = $coreRegistry ?: ObjectManager::getInstance()
@@ -64,24 +54,16 @@ public function getFieldName(AttributeAdapter $attribute, $context = []): ?strin
6454
}
6555

6656
/**
67-
* Resolve category id.
68-
*
69-
* @param array $context
70-
* @return int
57+
* {@inheritdoc}
7158
*/
7259
private function resolveCategoryId($context)
7360
{
7461
if (isset($context['categoryId'])) {
7562
$id = $context['categoryId'];
7663
} else {
77-
$id = \Magento\Catalog\Model\Category::ROOT_CATEGORY_ID;
78-
try {
79-
$id = $this->coreRegistry->registry('current_category')
80-
? $this->coreRegistry->registry('current_category')->getId()
81-
: $this->storeManager->getStore()->getRootCategoryId();
82-
} catch (LocalizedException $exception) {
83-
$this->logger->critical($exception);
84-
}
64+
$id = $this->coreRegistry->registry('current_category')
65+
? $this->coreRegistry->registry('current_category')->getId()
66+
: $this->storeManager->getStore()->getRootCategoryId();
8567
}
8668

8769
return $id;

0 commit comments

Comments
 (0)