-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Centralize logic for class definition reindexing (#200)
- Loading branch information
1 parent
50eedd2
commit b9a4de6
Showing
7 changed files
with
174 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/Exception/ClassDefinitionIndexUpdateFailedException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license http://www.pimcore.org/license GPLv3 and PCL | ||
*/ | ||
|
||
namespace Pimcore\Bundle\GenericDataIndexBundle\Exception; | ||
|
||
use Pimcore\Extension\Bundle\Exception\RuntimeException; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class ClassDefinitionIndexUpdateFailedException extends RuntimeException implements GenericDataIndexBundleExceptionInterface | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/Service/SearchIndex/ClassDefinition/ClassDefinitionReindexService.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license http://www.pimcore.org/license GPLv3 and PCL | ||
*/ | ||
|
||
namespace Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\ClassDefinition; | ||
|
||
use Exception; | ||
use Pimcore\Bundle\GenericDataIndexBundle\Exception\ClassDefinitionIndexUpdateFailedException; | ||
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexQueue\EnqueueServiceInterface; | ||
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexService\IndexHandler\DataObjectIndexHandler; | ||
use Pimcore\Bundle\GenericDataIndexBundle\Service\SettingsStoreServiceInterface; | ||
use Pimcore\Model\DataObject\ClassDefinition; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final readonly class ClassDefinitionReindexService implements ClassDefinitionReindexServiceInterface | ||
{ | ||
public function __construct( | ||
private DataObjectIndexHandler $dataObjectIndexHandler, | ||
private EnqueueServiceInterface $enqueueService, | ||
private SettingsStoreServiceInterface $settingsStoreService, | ||
) { | ||
} | ||
|
||
public function reindexClassDefinition( | ||
ClassDefinition $classDefinition, | ||
bool $skipIfClassNotChanged = false, | ||
bool $enqueueItems = false, | ||
): bool { | ||
try { | ||
$changed = $this->reindexMapping($classDefinition, $skipIfClassNotChanged); | ||
|
||
if ($changed && $enqueueItems) { | ||
$this->enqueueService->enqueueByClassDefinition($classDefinition); | ||
} | ||
|
||
return $changed; | ||
} catch (Exception $exception) { | ||
throw new ClassDefinitionIndexUpdateFailedException( | ||
message: $exception->getMessage(), | ||
previous: $exception | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
private function reindexMapping( | ||
ClassDefinition $classDefinition, | ||
bool $skipIfClassNotChanged | ||
): bool { | ||
$mappingProperties = $this->dataObjectIndexHandler->getMappingProperties($classDefinition); | ||
$currentCheckSum = $this->dataObjectIndexHandler->getClassMappingCheckSum($mappingProperties); | ||
$storedCheckSum = $this->settingsStoreService->getClassMappingCheckSum($classDefinition->getId()); | ||
|
||
if ($skipIfClassNotChanged && $storedCheckSum === $currentCheckSum) { | ||
return false; | ||
} | ||
|
||
$this->dataObjectIndexHandler | ||
->reindexMapping( | ||
context: $classDefinition, | ||
mappingProperties: $mappingProperties | ||
); | ||
|
||
$this->settingsStoreService->storeClassMapping( | ||
classDefinitionId: $classDefinition->getId(), | ||
data: $this->dataObjectIndexHandler->getClassMappingCheckSum( | ||
$mappingProperties | ||
) | ||
); | ||
|
||
return true; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/Service/SearchIndex/ClassDefinition/ClassDefinitionReindexServiceInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license http://www.pimcore.org/license GPLv3 and PCL | ||
*/ | ||
|
||
namespace Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\ClassDefinition; | ||
|
||
use Pimcore\Bundle\GenericDataIndexBundle\Exception\ClassDefinitionIndexUpdateFailedException; | ||
use Pimcore\Model\DataObject\ClassDefinition; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
interface ClassDefinitionReindexServiceInterface | ||
{ | ||
/** | ||
* @throws ClassDefinitionIndexUpdateFailedException | ||
*/ | ||
public function reindexClassDefinition( | ||
ClassDefinition $classDefinition, | ||
bool $skipIfClassNotChanged = false, | ||
bool $enqueueItems = false, | ||
): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters