-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add event-subscriber to throw flushed workflow events
- Loading branch information
1 parent
91b8b90
commit 5323396
Showing
4 changed files
with
86 additions
and
10 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
Content/Infrastructure/Symfony/Workflow/FlushedEventSubscriber.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,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) Sulu GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\ContentBundle\Content\Infrastructure\Symfony\Workflow; | ||
|
||
use Sulu\Bundle\ContentBundle\Content\Domain\Model\WorkflowInterface; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\Workflow\Event\Event; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class FlushedEventSubscriber implements EventSubscriberInterface | ||
{ | ||
/** | ||
* @return string[] | ||
*/ | ||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
'workflow.completed' => 'onCompleted', | ||
]; | ||
} | ||
|
||
/** | ||
* @var EventDispatcherInterface | ||
*/ | ||
private $eventDispatcher; | ||
|
||
/** | ||
* @var mixed[] | ||
*/ | ||
private $eventsToDispatch = []; | ||
|
||
public function __construct(EventDispatcherInterface $eventDispatcher) | ||
{ | ||
$this->eventDispatcher = $eventDispatcher; | ||
} | ||
|
||
public function onCompleted(Event $event): void | ||
{ | ||
if ($event->getWorkflowName() !== WorkflowInterface::WORKFLOW_DEFAULT_NAME) { | ||
return; | ||
} | ||
|
||
$eventName = sprintf('workflow.%s.flushed', $event->getWorkflowName()); | ||
|
||
$this->eventsToDispatch[] = [$event, $eventName]; | ||
$this->eventsToDispatch[] = [$event, sprintf('%s.%s', $eventName, $event->getTransition()->getName())]; | ||
} | ||
|
||
public function postFlush(): void | ||
{ | ||
foreach ($this->eventsToDispatch as $item) { | ||
$this->eventDispatcher->dispatch($item[0], $item[1]); | ||
} | ||
|
||
$this->eventsToDispatch = []; | ||
} | ||
|
||
public function onClear(): void | ||
{ | ||
$this->eventsToDispatch = []; | ||
} | ||
} |
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