Skip to content
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

PD-95 Add Classification Store Adapter #45

Merged
merged 7 commits into from
Feb 15, 2024
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 @@ -68,4 +68,9 @@ services:
Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\DataObject\FieldDefinitionAdapter\FieldCollectionAdapter:
shared: false
tags:
- { name: "pimcore.generic_data_index.data-object.search_index_field_definition", type: "fieldcollections" }
- { name: "pimcore.generic_data_index.data-object.search_index_field_definition", type: "fieldcollections" }

Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\DataObject\FieldDefinitionAdapter\ClassificationStoreAdapter:
shared: false
tags:
- { name: "pimcore.generic_data_index.data-object.search_index_field_definition", type: "classificationstore" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under following license:
* - Pimcore Commercial License (PCL)
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license PCL
*/

namespace Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\DataObject\FieldDefinitionAdapter;

use InvalidArgumentException;
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\OpenSearch\AttributeType;
use Pimcore\Bundle\StaticResolverBundle\Models\DataObject\ClassificationStore\ServiceResolverInterface;
use Pimcore\Model\DataObject\ClassDefinition\Data;
use Pimcore\Model\DataObject\ClassDefinition\Data\Classificationstore;
use Pimcore\Model\DataObject\Classificationstore\GroupConfig;
use Pimcore\Model\DataObject\Classificationstore\GroupConfig\Listing as GroupListing;
use Pimcore\Model\DataObject\Classificationstore\KeyGroupRelation;
use Pimcore\Model\DataObject\Classificationstore\KeyGroupRelation\Listing as KeyGroupRelationListing;
use Symfony\Contracts\Service\Attribute\Required;

/**
* @internal
*/
final class ClassificationStoreAdapter extends AbstractAdapter
{
private ServiceResolverInterface $classificationService;

#[Required]
public function setClassificationService(ServiceResolverInterface $serviceResolver): void
{
$this->classificationService = $serviceResolver;
}

public function getOpenSearchMapping(): array
{
$classificationStore = $this->getFieldDefinition();
if (!$classificationStore instanceof Classificationstore) {
throw new InvalidArgumentException(
'Field definition must be an instance of ' . Classificationstore::class
);
}
$mapping = [];

$groups = $this->getClassificationStoreGroups($classificationStore->getStoreId());
foreach ($groups as $group) {
$keys = $this->getClassificationStoreKeysFromGroup($group);
$mapping[$group->getName()]['properties'] = $this->getMappingForGroupConfig($keys);
}

return [
'type' => AttributeType::NESTED,
'properties' => $mapping,
];
}

/**
* @param KeyGroupRelation[] $groupConfigs
*/
private function getMappingForGroupConfig(array $groupConfigs): array
{
$groupMapping = [];
foreach ($groupConfigs as $key) {
$definition = $this->classificationService->getFieldDefinitionFromKeyConfig($key);
if ($definition instanceof Data) {
$adapter = $this->getFieldDefinitionService()->getFieldDefinitionAdapter($definition);

if ($adapter) {
$groupMapping['default']['properties'][$key->getName()] = $adapter->getOpenSearchMapping();
}
}
}

return $groupMapping;
}

/**
* @return GroupConfig[]
*/
private function getClassificationStoreGroups(int $id): array
{
$listing = new GroupListing();
$listing->setCondition('storeId = :storeId', ['storeId' => $id]);

return $listing->getList();
}

/**
* @return KeyGroupRelation[]
*/
private function getClassificationStoreKeysFromGroup(GroupConfig $groupConfig): array
{
$listing = new KeyGroupRelationListing();
$listing->addConditionParam('groupId = ?', $groupConfig->getId());

return $listing->getList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function getOpenSearchMapping(): array
'value' => [
'type' => AttributeType::FLOAT->value,
],
'unitAbbrevation' => [
'unitId' => [
'type' => AttributeType::TEXT->value,
],
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under following license:
* - Pimcore Commercial License (PCL)
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license PCL
*/

namespace Pimcore\Bundle\GenericDataIndexBundle\Tests\Unit\Service\SearchIndex\DataObject\FieldDefinitionAdapter;

use Codeception\Test\Unit;
use InvalidArgumentException;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\DataObject\FieldDefinitionAdapter\ClassificationStoreAdapter;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\DataObject\FieldDefinitionServiceInterface;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\SearchIndexConfigServiceInterface;
use Pimcore\Model\DataObject\ClassDefinition\Data\Checkbox;
use Pimcore\Model\DataObject\ClassDefinition\Data\Classificationstore;

/**
* @internal
*/
final class ClassificationStoreAdapterTest extends Unit
{
public function testExceptionIsThrownWhenWrongFieldDefinition()
{
$searchIndexConfigServiceInterfaceMock = $this->makeEmpty(SearchIndexConfigServiceInterface::class);
$fieldDefinitionServiceInterfaceMock = $this->makeEmpty(FieldDefinitionServiceInterface::class);

$adapter = new ClassificationStoreAdapter(
$searchIndexConfigServiceInterfaceMock,
$fieldDefinitionServiceInterfaceMock
);

$relation = new Checkbox();
$adapter->setFieldDefinition($relation);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Field definition must be an instance of ' . Classificationstore::class);
$adapter->getOpenSearchMapping();
}
}
Loading