Skip to content

Commit

Permalink
[TransactionalMessenger] Added support for Symfony Command Component
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalzombie committed Jan 25, 2023
1 parent 207cb81 commit b793534
Show file tree
Hide file tree
Showing 17 changed files with 479 additions and 422 deletions.
2 changes: 2 additions & 0 deletions EventListener/CommitTransactionOnTerminateEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
use FRZB\Component\TransactionalMessenger\Attribute\Transactional;
use FRZB\Component\TransactionalMessenger\Enum\CommitType;
use FRZB\Component\TransactionalMessenger\MessageBus\CommitTransactionInterface as CommitService;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpKernel\KernelEvents;

#[AsEventListener(ConsoleEvents::TERMINATE, priority: Transactional::LISTENER_PRIORITY)]
#[AsEventListener(KernelEvents::TERMINATE, priority: Transactional::LISTENER_PRIORITY)]
class CommitTransactionOnTerminateEventListener
{
Expand Down
25 changes: 25 additions & 0 deletions EventListener/RollbackTransactionOnConsoleErrorEventListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace FRZB\Component\TransactionalMessenger\EventListener;

use FRZB\Component\TransactionalMessenger\Attribute\Transactional;
use FRZB\Component\TransactionalMessenger\MessageBus\RollbackTransactionInterface as RollbackService;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;

#[AsEventListener(ConsoleEvents::ERROR, priority: Transactional::LISTENER_PRIORITY)]
class RollbackTransactionOnConsoleErrorEventListener
{
public function __construct(
private readonly RollbackService $service,
) {
}

public function __invoke(ConsoleErrorEvent $event): void
{
$this->service->rollback($event->getError());
}
}
26 changes: 26 additions & 0 deletions EventListener/RollbackTransactionOnConsoleSignalEventListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace FRZB\Component\TransactionalMessenger\EventListener;

use FRZB\Component\TransactionalMessenger\Attribute\Transactional;
use FRZB\Component\TransactionalMessenger\Exception\DispatchException;
use FRZB\Component\TransactionalMessenger\MessageBus\RollbackTransactionInterface as RollbackService;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleSignalEvent;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;

#[AsEventListener(ConsoleEvents::SIGNAL, priority: Transactional::LISTENER_PRIORITY)]
class RollbackTransactionOnConsoleSignalEventListener
{
public function __construct(
private readonly RollbackService $service,
) {
}

public function __invoke(ConsoleSignalEvent $event): void
{
$this->service->rollback(DispatchException::fromSignal($event));
}
}
10 changes: 10 additions & 0 deletions Exception/DispatchException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,20 @@
namespace FRZB\Component\TransactionalMessenger\Exception;

use JetBrains\PhpStorm\Immutable;
use Symfony\Component\Console\Event\ConsoleSignalEvent;

#[Immutable]
final class DispatchException extends \LogicException
{
private const MESSAGE_SIGNAL_CONSOLE_EVENT = 'Rollback transaction: Message was interrupted in "%s" command on signal';

public static function fromSignal(ConsoleSignalEvent $event): self
{
$message = sprintf(self::MESSAGE_SIGNAL_CONSOLE_EVENT, $event->getCommand());

return new self($message, $event->getHandlingSignal());
}

public static function fromThrowable(\Throwable $previous): self
{
return new self($previous->getMessage(), (int) $previous->getCode(), $previous);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testInvokeMethod(string $eventListenerClass): void
(new $eventListenerClass($this->commitService))();
}

public function dataProvider(): iterable
public static function dataProvider(): iterable
{
yield ClassHelper::getShortName(CommitTransactionOnTerminateEventListener::class) => [
'event_listener' => CommitTransactionOnTerminateEventListener::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function testInvokeMethod(string $eventListenerClass, object $event): voi
(new $eventListenerClass($this->rollbackService))($event);
}

public function dataProvider(): iterable
public static function dataProvider(): iterable
{
yield ClassHelper::getShortName(RollbackTransactionOnExceptionEventListener::class) => [
'event_listener' => RollbackTransactionOnExceptionEventListener::class,
Expand Down
2 changes: 1 addition & 1 deletion Tests/Unit/Helper/AttributeHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function testHasAttributeMethod(string $className, bool $hasAttributes):
self::assertSame($hasAttributes, AttributeHelper::hasAttribute($className, Transactional::class));
}

public function dataProvider(): iterable
public static function dataProvider(): iterable
{
yield sprintf('%s', ClassHelper::getShortName(TransactionalOnTerminateMessage::class)) => [
'class_name' => TransactionalOnTerminateMessage::class,
Expand Down
8 changes: 4 additions & 4 deletions Tests/Unit/Helper/ClassHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function testGetReflectionAttributesClassMethod(string $className, bool $
;
}

public function shortNameProvider(): iterable
public static function shortNameProvider(): iterable
{
yield sprintf('%s', ClassHelper::getShortName(TransactionalOnTerminateMessage::class)) => [
'class_name' => TransactionalOnTerminateMessage::class,
Expand Down Expand Up @@ -80,7 +80,7 @@ public function shortNameProvider(): iterable
];
}

public function reflectionProvider(): iterable
public static function reflectionProvider(): iterable
{
yield sprintf('%s', ClassHelper::getShortName(TransactionalOnTerminateMessage::class)) => [
'class_name' => TransactionalOnTerminateMessage::class,
Expand Down Expand Up @@ -113,7 +113,7 @@ public function reflectionProvider(): iterable
];
}

public function parentReflectionProvider(): iterable
public static function parentReflectionProvider(): iterable
{
yield sprintf('%s', ClassHelper::getShortName(TransactionalOnTerminateMessage::class)) => [
'class_name' => TransactionalOnTerminateMessage::class,
Expand Down Expand Up @@ -146,7 +146,7 @@ public function parentReflectionProvider(): iterable
];
}

public function reflectionAttributesProvider(): iterable
public static function reflectionAttributesProvider(): iterable
{
yield sprintf('%s', ClassHelper::getShortName(TransactionalOnTerminateMessage::class)) => [
'class_name' => TransactionalOnTerminateMessage::class,
Expand Down
2 changes: 1 addition & 1 deletion Tests/Unit/Helper/EnvelopeHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function testWrapMethod(object $target): void
self::assertNotNull($envelope->last(DispatchAfterCurrentBusStamp::class));
}

public function dataProvider(): iterable
public static function dataProvider(): iterable
{
yield sprintf('%s', ClassHelper::getShortName(TransactionalOnTerminateMessage::class)) => [
'target' => new TransactionalOnTerminateMessage(),
Expand Down
2 changes: 1 addition & 1 deletion Tests/Unit/Helper/TransactionHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function getIsDispatchableMethod(string $className, bool $isAllowed, arra
self::assertSame($isAllowed, TransactionHelper::isDispatchable($className, ...$commitTypes));
}

public function transactionalProvider(): iterable
public static function transactionalProvider(): iterable
{
yield sprintf('%s', ClassHelper::getShortName(TransactionalOnTerminateMessage::class)) => [
'class_name' => TransactionalOnTerminateMessage::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function testPrivateDispatchEnvelopeMethod(): void
self::assertSame(spl_object_hash($envelope), spl_object_hash((new \ReflectionMethod($this->messageBus, 'dispatchEnvelope'))->invoke($this->messageBus, $envelope)));
}

public function dataProvider(): iterable
public static function dataProvider(): iterable
{
yield sprintf('%s is succeed commit', ClassHelper::getShortName(TransactionalOnTerminateMessage::class)) => [
'message' => new TransactionalOnTerminateMessage(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function testDispatchMethod(object $message, int $pendingCount, int $expe
self::assertSame($pendingCount, $this->pendingStorage->count());
}

public function dataProvider(): iterable
public static function dataProvider(): iterable
{
yield sprintf('%s is dispatched delayed', ClassHelper::getShortName(TransactionalOnTerminateMessage::class)) => [
'message' => new TransactionalOnTerminateMessage(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function testRollbackMethod(
self::assertSame($failedCount, $this->failedStorage->count());
}

public function dataProvider(): iterable
public static function dataProvider(): iterable
{
yield sprintf('%s is dispatched delayed', ClassHelper::getShortName(TransactionalOnTerminateMessage::class)) => [
'message' => new TransactionalOnTerminateMessage(),
Expand Down
2 changes: 1 addition & 1 deletion Tests/Unit/ValueObject/FailedEnvelopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function testConstructorMethod(object $message): void
self::assertSame(spl_object_hash($message), spl_object_hash($failedEnvelope->envelope->getMessage()));
}

public function dataProvider(): iterable
public static function dataProvider(): iterable
{
yield 'TransactionalOnTerminateMessage with FailedEnvelope' => [
'message' => new TransactionalOnTerminateMessage(),
Expand Down
2 changes: 1 addition & 1 deletion Tests/Unit/ValueObject/PendingEnvelopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function testConstructorMethod(object $message, array $commitTypes): void
self::assertTrue($pendingEnvelope->isTransactional(...$commitTypes));
}

public function dataProvider(): iterable
public static function dataProvider(): iterable
{
yield 'TransactionalOnTerminateMessage with PendingEnvelope' => [
'message' => new TransactionalOnTerminateMessage(),
Expand Down
2 changes: 1 addition & 1 deletion Tests/Unit/ValueObject/SucceedEnvelopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function testConstructorMethod(object $message): void
self::assertSame(spl_object_hash($message), spl_object_hash($succeedEnvelope->envelope->getMessage()));
}

public function dataProvider(): iterable
public static function dataProvider(): iterable
{
yield 'TransactionalOnTerminateMessage with SucceedEnvelope' => [
'message' => new TransactionalOnTerminateMessage(),
Expand Down

0 comments on commit b793534

Please sign in to comment.