Skip to content

Commit b9f1849

Browse files
ENGCOM-5317: Cache Elasticsearch field mappers per entity #23283
2 parents edc9614 + bdb633a commit b9f1849

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

app/code/Magento/Elasticsearch/Model/Adapter/FieldMapper/FieldMapperResolver.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
use Magento\Elasticsearch\Model\Adapter\FieldMapperInterface;
1010
use Magento\Elasticsearch\Model\Config;
1111

12+
/**
13+
* Field Mapper resolver.
14+
*/
1215
class FieldMapperResolver implements FieldMapperInterface
1316
{
1417
/**
@@ -24,11 +27,11 @@ class FieldMapperResolver implements FieldMapperInterface
2427
private $fieldMappers;
2528

2629
/**
27-
* Field Mapper instance
30+
* Field Mapper instance per entity
2831
*
29-
* @var FieldMapperInterface
32+
* @var FieldMapperInterface[]
3033
*/
31-
private $fieldMapperEntity;
34+
private $fieldMapperEntity = [];
3235

3336
/**
3437
* @param ObjectManagerInterface $objectManager
@@ -43,7 +46,7 @@ public function __construct(
4346
}
4447

4548
/**
46-
* {@inheritdoc}
49+
* @inheritdoc
4750
*/
4851
public function getFieldName($attributeCode, $context = [])
4952
{
@@ -52,7 +55,7 @@ public function getFieldName($attributeCode, $context = [])
5255
}
5356

5457
/**
55-
* {@inheritdoc}
58+
* @inheritdoc
5659
*/
5760
public function getAllAttributesTypes($context = [])
5861
{
@@ -69,8 +72,9 @@ public function getAllAttributesTypes($context = [])
6972
*/
7073
private function getEntity($entityType)
7174
{
72-
if (empty($this->fieldMapperEntity)) {
75+
if (empty($this->fieldMapperEntity[$entityType])) {
7376
if (empty($entityType)) {
77+
// phpcs:ignore Magento2.Exceptions.DirectThrow
7478
throw new \Exception(
7579
'No entity type given'
7680
);
@@ -81,13 +85,13 @@ private function getEntity($entityType)
8185
);
8286
}
8387
$fieldMapperClass = $this->fieldMappers[$entityType];
84-
$this->fieldMapperEntity = $this->objectManager->create($fieldMapperClass);
85-
if (!($this->fieldMapperEntity instanceof FieldMapperInterface)) {
88+
$this->fieldMapperEntity[$entityType] = $this->objectManager->create($fieldMapperClass);
89+
if (!($this->fieldMapperEntity[$entityType] instanceof FieldMapperInterface)) {
8690
throw new \InvalidArgumentException(
8791
'Field mapper must implement \Magento\Elasticsearch\Model\Adapter\FieldMapperInterface'
8892
);
8993
}
9094
}
91-
return $this->fieldMapperEntity;
95+
return $this->fieldMapperEntity[$entityType];
9296
}
9397
}

0 commit comments

Comments
 (0)