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 16, 2021
1 parent 91b8b90 commit a6c4698
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 10 deletions.
76 changes: 76 additions & 0 deletions Content/Infrastructure/Symfony/Workflow/FlushedEventSubscriber.php
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 (WorkflowInterface::WORKFLOW_DEFAULT_NAME !== $event->getWorkflowName()) {
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 = [];
}
}
2 changes: 2 additions & 0 deletions DependencyInjection/SuluContentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class SuluContentExtension extends Extension implements PrependExtensionInterface
{
Expand Down Expand Up @@ -54,6 +55,7 @@ public function load(array $configs, ContainerBuilder $container): void
if ($container->hasParameter('kernel.bundles')) {
// TODO FIXME add test here
// @codeCoverageIgnoreStart
/** @var array<string, Bundle> $bundles */
$bundles = $container->getParameter('kernel.bundles');

if (\array_key_exists('SuluAutomationBundle', $bundles)) {
Expand Down
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?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\Tests\Unit\Content\Infrastructure\Symfony\Workflow;

use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\WorkflowInterface;
use Sulu\Bundle\ContentBundle\Content\Infrastructure\Symfony\Workflow\FlushedEventSubscriber;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Workflow\Event\Event;
use Symfony\Component\Workflow\Transition;

class FlushedEventSubscriberTest extends TestCase
{
protected function createFlushedEventSubscriber(EventDispatcherInterface $eventDispatcher): FlushedEventSubscriber
{
return new FlushedEventSubscriber($eventDispatcher);
}

public function testOnCompleteAndFlush(): void
{
$transition = $this->prophesize(Transition::class);
$transition->getName()->willReturn('published');

$event = $this->prophesize(Event::class);
$event->getWorkflowName()->willReturn(WorkflowInterface::WORKFLOW_DEFAULT_NAME);
$event->getTransition()->willReturn($transition->reveal());

$eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$eventDispatcher->dispatch(Argument::any())->shouldNotBeCalled();

$subscriber = $this->createFlushedEventSubscriber($eventDispatcher->reveal());
$subscriber->onCompleted($event->reveal());

$eventName = sprintf('workflow.%s.flushed', WorkflowInterface::WORKFLOW_DEFAULT_NAME);
$eventDispatcher->dispatch($event->reveal(), $eventName)->shouldBeCalledOnce();
$eventDispatcher->dispatch($event->reveal(), $eventName . '.published')->shouldBeCalledOnce();

$subscriber->postFlush();
}

public function testOnCompleteAndClear(): void
{
$transition = $this->prophesize(Transition::class);
$transition->getName()->willReturn('published');

$event = $this->prophesize(Event::class);
$event->getWorkflowName()->willReturn(WorkflowInterface::WORKFLOW_DEFAULT_NAME);
$event->getTransition()->willReturn($transition->reveal());

$eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$eventDispatcher->dispatch(Argument::any())->shouldNotBeCalled();

$subscriber = $this->createFlushedEventSubscriber($eventDispatcher->reveal());
$subscriber->onCompleted($event->reveal());

$subscriber->onClear();
}
}

0 comments on commit a6c4698

Please sign in to comment.