Skip to content

Commit

Permalink
PD-105 Add Events (#2)
Browse files Browse the repository at this point in the history
* PD-105 Add Asset ExtractMappingEvent.

* PD-105 Add UpdateIndexDataEvent for DataObjects and Assets.

* PD-105 Add ExtractMappingEvent DataObjects.

* Apply php-cs-fixer changes

* PD-105 Use namespace for exception.

---------

Co-authored-by: martineiber <martineiber@users.noreply.github.com>
  • Loading branch information
martineiber and martineiber authored Jan 24, 2024
1 parent 766d506 commit 0aed427
Show file tree
Hide file tree
Showing 12 changed files with 295 additions and 12 deletions.
43 changes: 43 additions & 0 deletions src/Event/Asset/ExtractMappingEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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\Event\Asset;

use Symfony\Contracts\EventDispatcher\Event;

/**
* Fires before the mapping will be sent to the search-server index.
* Can be used to add mappings for customized additional fields.
* You will find a description and example on how it works in the docs.
*/
final class ExtractMappingEvent extends Event
{
protected array $customFieldsMapping;

public function __construct(array $customFieldsMapping)
{
$this->customFieldsMapping = $customFieldsMapping;
}

public function getCustomFieldsMapping(): array
{
return $this->customFieldsMapping;
}

public function setCustomFieldsMapping(array $customFieldsMapping): self
{
$this->customFieldsMapping = $customFieldsMapping;

return $this;
}
}
53 changes: 53 additions & 0 deletions src/Event/Asset/UpdateIndexDataEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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\Event\Asset;

use Pimcore\Bundle\GenericDataIndexBundle\Event\UpdateIndexDataEventInterface;
use Pimcore\Model\Asset;
use Symfony\Contracts\EventDispatcher\Event;

/**
* Fires before the data for assets gets updated in the search index.
* Can be used to add additional customized attributes in the search index.
* You will find a description and example on how it works in the docs.
*/
final class UpdateIndexDataEvent extends Event implements UpdateIndexDataEventInterface
{
protected Asset $asset;

protected array $customFields;

public function __construct(Asset $asset, array $customFields)
{
$this->asset = $asset;
$this->customFields = $customFields;
}

public function getElement(): Asset
{
return $this->asset;
}

public function getCustomFields(): array
{
return $this->customFields;
}

public function setCustomFields(array $customFields): self
{
$this->customFields = $customFields;

return $this;
}
}
52 changes: 52 additions & 0 deletions src/Event/DataObject/ExtractMappingEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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\Event\DataObject;

use Pimcore\Model\DataObject\ClassDefinition;
use Symfony\Contracts\EventDispatcher\Event;

/**
* Fires before the mapping will be sent to the search server index.
* Can be used to add mappings for customized additional fields.
* You will find a description and example on how it works in the docs.
*/
final class ExtractMappingEvent extends Event
{
protected ClassDefinition $classDefinition;

protected array $customFieldsMapping;

public function __construct(ClassDefinition $classDefinition, array $customFieldsMapping)
{
$this->classDefinition = $classDefinition;
$this->customFieldsMapping = $customFieldsMapping;
}

public function getClassDefinition(): ClassDefinition
{
return $this->classDefinition;
}

public function getCustomFieldsMapping(): array
{
return $this->customFieldsMapping;
}

public function setCustomFieldsMapping(array $customFieldsMapping): self
{
$this->customFieldsMapping = $customFieldsMapping;

return $this;
}
}
53 changes: 53 additions & 0 deletions src/Event/DataObject/UpdateIndexDataEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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\Event\DataObject;

use Pimcore\Bundle\GenericDataIndexBundle\Event\UpdateIndexDataEventInterface;
use Pimcore\Model\DataObject\Concrete;
use Symfony\Contracts\EventDispatcher\Event;

/**
* Fires before the data for data objects gets updated in the search server index.
* Can be used to add additional customized attributes in the search index.
* You will find a description and example on how it works in the docs.
*/
final class UpdateIndexDataEvent extends Event implements UpdateIndexDataEventInterface
{
protected Concrete $dataObject;

protected array $customFields;

public function __construct(Concrete $dataObject, array $customFields)
{
$this->dataObject = $dataObject;
$this->customFields = $customFields;
}

public function getElement(): Concrete
{
return $this->dataObject;
}

public function getCustomFields(): array
{
return $this->customFields;
}

public function setCustomFields(array $customFields): self
{
$this->customFields = $customFields;

return $this;
}
}
25 changes: 25 additions & 0 deletions src/Event/UpdateIndexDataEventInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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\Event;

use Pimcore\Model\Element\ElementInterface;

interface UpdateIndexDataEventInterface
{
public function getElement(): ElementInterface;

public function getCustomFields(): array;

public function setCustomFields(array $customFields): self;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Doctrine\DBAL\Query\QueryBuilder;
use Exception;
use Pimcore\Bundle\GenericDataIndexBundle\Event\UpdateIndexDataEventInterface;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\SearchIndexConfigService;
use Pimcore\Model\Element\ElementInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
Expand Down Expand Up @@ -50,6 +51,11 @@ abstract public function childrenPathRewriteNeeded(ElementInterface $element): b

abstract public function getNormalizer(): NormalizerInterface;

abstract public function getUpdateIndexDataEvent(
ElementInterface $element,
array $customFields
): UpdateIndexDataEventInterface;

/**
* @throws Exception
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@

namespace Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexService\ElementTypeAdapter;

use InvalidArgumentException;
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\ElementType;
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\IndexName;
use Pimcore\Bundle\GenericDataIndexBundle\Event\Asset\UpdateIndexDataEvent;
use Pimcore\Bundle\GenericDataIndexBundle\Event\UpdateIndexDataEventInterface;
use Pimcore\Bundle\GenericDataIndexBundle\Service\Normalizer\AssetNormalizer;
use Pimcore\Model\Asset;
use Pimcore\Model\Element\ElementInterface;
Expand Down Expand Up @@ -56,4 +59,15 @@ public function getNormalizer(): NormalizerInterface
{
return $this->normalizer;
}

public function getUpdateIndexDataEvent(
ElementInterface $element,
array $customFields
): UpdateIndexDataEventInterface {
if(!$element instanceof Asset) {
throw new InvalidArgumentException('Element must be of type Asset');
}

return new UpdateIndexDataEvent($element, $customFields);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Query\QueryBuilder;
use InvalidArgumentException;
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\ElementType;
use Pimcore\Bundle\GenericDataIndexBundle\Event\DataObject\UpdateIndexDataEvent;
use Pimcore\Bundle\GenericDataIndexBundle\Event\UpdateIndexDataEventInterface;
use Pimcore\Bundle\GenericDataIndexBundle\Service\Normalizer\DataObjectNormalizer;
use Pimcore\Model\DataObject\AbstractObject;
use Pimcore\Model\DataObject\ClassDefinition;
Expand Down Expand Up @@ -117,4 +120,15 @@ public function getRelatedItemsOnUpdateQuery(

return $select;
}

public function getUpdateIndexDataEvent(
ElementInterface $element,
array $customFields
): UpdateIndexDataEventInterface {
if (!$element instanceof Concrete) {
throw new InvalidArgumentException('Element must be instance of ' . Concrete::class);
}

return new UpdateIndexDataEvent($element, $customFields);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\OpenSearch\OpenSearchService;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\SearchIndexConfigService;
use Pimcore\Bundle\GenericDataIndexBundle\Traits\LoggerAwareTrait;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

abstract class AbstractIndexHandler implements IndexHandlerInterface
{
Expand All @@ -25,6 +26,7 @@ abstract class AbstractIndexHandler implements IndexHandlerInterface
public function __construct(
protected readonly OpenSearchService $openSearchService,
protected readonly SearchIndexConfigService $searchIndexConfigService,
protected readonly EventDispatcherInterface $eventDispatcher
) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexService\IndexHandler;

use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\FieldCategory;
use Pimcore\Bundle\GenericDataIndexBundle\Event\Asset\ExtractMappingEvent;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexService\ElementTypeAdapter\AssetTypeAdapter;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\SearchIndexConfigService;
use Symfony\Contracts\Service\Attribute\Required;
Expand All @@ -32,10 +33,9 @@ protected function extractMappingProperties(mixed $context = null): array
FieldCategory::STANDARD_FIELDS->value => [],
FieldCategory::CUSTOM_FIELDS->value => [],
];
//$extractMappingEvent = new ExtractMappingEvent($mappingProperties[FieldCategory::CUSTOM_FIELDS->value]);
//$this->eventDispatcher->dispatch($extractMappingEvent);
//$mappingProperties[FieldCategory::CUSTOM_FIELDS->value]['properties'] =
// $extractMappingEvent->getCustomFieldsMapping();

$mappingProperties[FieldCategory::CUSTOM_FIELDS->value]['properties'] =
$this->fireEventAndGetCustomFieldsMapping($mappingProperties[FieldCategory::CUSTOM_FIELDS->value]);

return $mappingProperties;
}
Expand All @@ -50,4 +50,12 @@ public function setAssetAdapter(AssetTypeAdapter $assetAdapter): void
{
$this->assetAdapter = $assetAdapter;
}

private function fireEventAndGetCustomFieldsMapping($customFields): array
{
$extractMappingEvent = new ExtractMappingEvent($customFields);
$this->eventDispatcher->dispatch($extractMappingEvent);

return $extractMappingEvent->getCustomFieldsMapping();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexService\IndexHandler;

use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\FieldCategory;
use Pimcore\Bundle\GenericDataIndexBundle\Event\DataObject\ExtractMappingEvent;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\DataObject\FieldDefinitionService;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexService\ElementTypeAdapter\DataObjectTypeAdapter;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\SearchIndexConfigService;
Expand Down Expand Up @@ -77,9 +78,11 @@ private function extractMappingByClassDefinition(ClassDefinition $classDefinitio
}
}

//$extractMappingEvent = new ExtractMappingEvent($classDefinition, $mappingProperties[FieldCategory::CUSTOM_FIELDS->value]);
//$this->eventDispatcher->dispatch($extractMappingEvent);
//$mappingProperties[FieldCategory::CUSTOM_FIELDS->value]['properties'] = $extractMappingEvent->getCustomFieldsMapping();
$mappingProperties[FieldCategory::CUSTOM_FIELDS->value]['properties'] =
$this->fireEventAndGetCustomFieldsMapping(
$classDefinition,
$mappingProperties[FieldCategory::CUSTOM_FIELDS->value]
);

return $mappingProperties;
}
Expand All @@ -95,4 +98,12 @@ public function setDataObjectTypeAdapter(DataObjectTypeAdapter $dataObjectTypeAd
{
$this->dataObjectTypeAdapter = $dataObjectTypeAdapter;
}

public function fireEventAndGetCustomFieldsMapping(ClassDefinition $classDefinition, array $customFields): array
{
$extractMappingEvent = new ExtractMappingEvent($classDefinition, $customFields);
$this->eventDispatcher->dispatch($extractMappingEvent);

return $extractMappingEvent->getCustomFieldsMapping();
}
}
Loading

0 comments on commit 0aed427

Please sign in to comment.