-
-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2106 from dpfaffenbauer/features/index-messenger
[IndexBundle] process Index Messages async
- Loading branch information
Showing
10 changed files
with
171 additions
and
48 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
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
80 changes: 80 additions & 0 deletions
80
src/CoreShop/Bundle/IndexBundle/Messenger/Handler/IndexMessageHandler.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,80 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* CoreShop | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - CoreShop Commercial License (CCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) CoreShop GmbH (https://www.coreshop.org) | ||
* @license https://www.coreshop.org/license GPLv3 and CCL | ||
* | ||
*/ | ||
|
||
namespace CoreShop\Bundle\IndexBundle\Messenger\Handler; | ||
|
||
use CoreShop\Bundle\IndexBundle\Messenger\IndexMessage; | ||
use CoreShop\Component\Index\Model\IndexableInterface; | ||
use CoreShop\Component\Index\Service\IndexUpdaterServiceInterface; | ||
use CoreShop\Component\Pimcore\DataObject\InheritanceHelper; | ||
use Pimcore\Model\DataObject\AbstractObject; | ||
use Pimcore\Model\DataObject\ClassDefinition; | ||
use Pimcore\Model\DataObject\Concrete; | ||
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException; | ||
use Symfony\Component\Messenger\Handler\MessageHandlerInterface; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
|
||
class IndexMessageHandler implements MessageHandlerInterface | ||
{ | ||
private array $validObjectTypes = [AbstractObject::OBJECT_TYPE_OBJECT, AbstractObject::OBJECT_TYPE_VARIANT]; | ||
|
||
public function __construct( | ||
private IndexUpdaterServiceInterface $indexUpdaterService, | ||
private MessageBusInterface $messageBus, | ||
) { | ||
} | ||
|
||
public function __invoke(IndexMessage $indexMessage) | ||
{ | ||
$indexable = Concrete::getById($indexMessage->getIndexableId()); | ||
|
||
if (!$indexable instanceof IndexableInterface) { | ||
throw new UnrecoverableMessageHandlingException('Indexable given does not implement IndexableInterface'); | ||
} | ||
|
||
InheritanceHelper::useInheritedValues(function () use ($indexable, $indexMessage) { | ||
if ($indexMessage->isDelete()) { | ||
$this->indexUpdaterService->removeIndices($indexable); | ||
return; | ||
} | ||
|
||
$this->indexUpdaterService->updateIndices($indexable, $indexMessage->isSaveVersionOnly()); | ||
}); | ||
|
||
$classDefinition = ClassDefinition::getById($indexable->getClassId()); | ||
|
||
if ($classDefinition && ($classDefinition->getAllowInherit() || $classDefinition->getAllowVariants())) { | ||
$this->updateInheritableChildren($indexable, $indexMessage->isSaveVersionOnly()); | ||
} | ||
} | ||
|
||
private function updateInheritableChildren(AbstractObject $object, bool $isVersionChange): void | ||
{ | ||
if (!$object->hasChildren($this->validObjectTypes)) { | ||
return; | ||
} | ||
|
||
$children = $object->getChildren($this->validObjectTypes); | ||
|
||
foreach ($children as $child) { | ||
if ($child instanceof IndexableInterface && $child::class === $object::class) { | ||
$this->messageBus->dispatch(new IndexMessage($child->getId(), $isVersionChange)); | ||
} | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/CoreShop/Bundle/IndexBundle/Messenger/IndexMessage.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,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* CoreShop | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - CoreShop Commercial License (CCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) CoreShop GmbH (https://www.coreshop.org) | ||
* @license https://www.coreshop.org/license GPLv3 and CCL | ||
* | ||
*/ | ||
|
||
namespace CoreShop\Bundle\IndexBundle\Messenger; | ||
|
||
class IndexMessage | ||
{ | ||
public function __construct( | ||
protected int $indexableId, | ||
protected bool $saveVersionOnly = false, | ||
protected bool $isDelete = false, | ||
) | ||
{ | ||
} | ||
|
||
public function getIndexableId(): int | ||
{ | ||
return $this->indexableId; | ||
} | ||
|
||
public function isSaveVersionOnly(): bool | ||
{ | ||
return $this->saveVersionOnly; | ||
} | ||
|
||
public function isDelete(): bool | ||
{ | ||
return $this->isDelete; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
src/CoreShop/Bundle/IndexBundle/Resources/config/pimcore/config.yml
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
imports: | ||
- { resource: admin.yml } | ||
- { resource: messenger.yml } | ||
|
||
pimcore: | ||
objects: | ||
|
7 changes: 7 additions & 0 deletions
7
src/CoreShop/Bundle/IndexBundle/Resources/config/pimcore/messenger.yml
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,7 @@ | ||
framework: | ||
messenger: | ||
transports: | ||
coreshop_index: | ||
dsn: "doctrine://default?queue_name=coreshop_index" | ||
routing: | ||
'CoreShop\Bundle\IndexBundle\Messenger\IndexMessage': coreshop_index |
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
10 changes: 10 additions & 0 deletions
10
src/CoreShop/Bundle/IndexBundle/Resources/config/services/messenger.yml
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,10 @@ | ||
services: | ||
_defaults: | ||
autoconfigure: false | ||
|
||
CoreShop\Bundle\IndexBundle\Messenger\Handler\IndexMessageHandler: | ||
arguments: | ||
- '@CoreShop\Component\Index\Service\IndexUpdaterServiceInterface' | ||
- '@messenger.default_bus' | ||
tags: | ||
- { name: messenger.message_handler } |