Skip to content

Commit 6300efe

Browse files
authored
Merge pull request #1695 from algolia/feat/MAGE-1154-record-builders
MAGE-1184: Entity Helpers refactoring
2 parents d280dc2 + 8840c63 commit 6300efe

File tree

12 files changed

+1425
-1129
lines changed

12 files changed

+1425
-1129
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Api\RecordBuilder;
4+
5+
use Magento\Framework\DataObject;
6+
7+
interface RecordBuilderInterface
8+
{
9+
public function buildRecord(DataObject $entity): array;
10+
}

Helper/Entity/AbstractEntityHelper.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ public function __construct(
1717
*/
1818
abstract public function getIndexNameSuffix(): string;
1919

20+
/**
21+
* Get the settings related to an entity to index
22+
*
23+
* @param int|null $storeId
24+
* @return array
25+
*/
26+
abstract public function getIndexSettings(?int $storeId = null): array;
27+
2028
/**
2129
* For a given entity helper, return the Algolia index for the specified store
2230
* @param int $storeId The store index desired

Helper/Entity/AdditionalSectionHelper.php

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Algolia\AlgoliaSearch\Helper\Entity;
44

5-
use Algolia\AlgoliaSearch\Service\AlgoliaConnector;
5+
use Algolia\AlgoliaSearch\Service\AdditionalSection\RecordBuilder as AdditionalSectionRecordBuilder;
66
use Algolia\AlgoliaSearch\Service\IndexNameFetcher;
77
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
88
use Magento\Eav\Model\Config;
@@ -15,16 +15,20 @@ class AdditionalSectionHelper extends AbstractEntityHelper
1515
public const INDEX_NAME_SUFFIX = '_section';
1616

1717
public function __construct(
18-
protected ManagerInterface $eventManager,
19-
protected CollectionFactory $collectionFactory,
20-
protected Config $eavConfig,
21-
protected IndexNameFetcher $indexNameFetcher,
22-
)
23-
{
18+
protected ManagerInterface $eventManager,
19+
protected CollectionFactory $collectionFactory,
20+
protected Config $eavConfig,
21+
protected AdditionalSectionRecordBuilder $additionalSectionRecordBuilder,
22+
protected IndexNameFetcher $indexNameFetcher,
23+
) {
2424
parent::__construct($indexNameFetcher);
2525
}
2626

27-
public function getIndexSettings($storeId): array
27+
/**
28+
* @param int|null $storeId
29+
* @return array
30+
*/
31+
public function getIndexSettings(?int $storeId = null): array
2832
{
2933
$indexSettings = [
3034
'searchableAttributes' => ['unordered(value)'],
@@ -68,23 +72,13 @@ public function getAttributeValues($storeId, $section): array
6872
}
6973

7074
$values = array_map(function ($value) use ($section, $storeId) {
71-
$record = [
72-
AlgoliaConnector::ALGOLIA_API_OBJECT_ID => $value,
73-
'value' => $value,
74-
];
75-
76-
$transport = new DataObject($record);
77-
$this->eventManager->dispatch(
78-
'algolia_additional_section_item_index_before',
79-
['section' => $section, 'record' => $transport, 'store_id' => $storeId]
80-
);
81-
$this->eventManager->dispatch(
82-
'algolia_additional_section_items_before_index',
83-
['section' => $section, 'record' => $transport, 'store_id' => $storeId]
84-
);
85-
$record = $transport->getData();
86-
87-
return $record;
75+
$dataObject = new DataObject([
76+
'value' => $value,
77+
'section' => $section,
78+
'store_id' => $storeId
79+
]);
80+
81+
return $this->additionalSectionRecordBuilder->buildRecord($dataObject);
8882
}, $values);
8983

9084
return $values;

0 commit comments

Comments
 (0)