Skip to content

Commit

Permalink
add event-subscriber to throw flushed workflow events
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes committed Feb 15, 2021
1 parent 91b8b90 commit 94bc27b
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 10 deletions.
72 changes: 72 additions & 0 deletions Content/Infrastructure/Symfony/Workflow/FlushedEventSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?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 [
sprintf('workflow.%s.completed', WorkflowInterface::WORKFLOW_DEFAULT_NAME) => 'onCompleted',
];
}

/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;

/**
* @var mixed[]
*/
private $eventsToDispatch = [];

public function __construct(EventDispatcherInterface $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}

public function onCompleted(Event $event): void
{
$eventName = sprintf('workflow.%s.flushed', WorkflowInterface::WORKFLOW_DEFAULT_NAME);

$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 = [];
}
}
8 changes: 8 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,13 @@
</service>

<service id="Sulu\Bundle\ContentBundle\Content\Application\ContentManager\ContentManagerInterface" alias="sulu_content.content_manager"/>

<service id="sulu_content.workflow.flushed_listener" class="Sulu\Bundle\ContentBundle\Content\Infrastructure\Symfony\Workflow\FlushedEventSubscriber">
<argument type="service" id="event_dispatcher"/>

<tag name="kernel.event_subscriber"/>
<tag name="doctrine.event_listener" event="postFlush"/>
<tag name="doctrine.event_listener" event="onClear"/>
</service>
</services>
</container>
10 changes: 0 additions & 10 deletions Tests/Application/config/config.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
swiftmailer:
disable_delivery: true

doctrine:
orm:
mappings:
gedmo_tree:
type: xml
prefix: Gedmo\Tree\Entity
dir: "%kernel.project_dir%/../../vendor/gedmo/doctrine-extensions/lib/Gedmo/Tree/Entity"
alias: GedmoTree
is_bundle: false

#sulu_core:
# content:
# structure:
Expand Down

0 comments on commit 94bc27b

Please sign in to comment.