-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a transactional command bus for league/tactician
- Loading branch information
Showing
4 changed files
with
222 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?php | ||
|
||
namespace RemiSan\TransactionManager\Command; | ||
|
||
use League\Tactician\CommandBus; | ||
use League\Tactician\Exception\InvalidCommandException; | ||
use RemiSan\TransactionManager\Exception\BeginException; | ||
use RemiSan\TransactionManager\Exception\CommitException; | ||
use RemiSan\TransactionManager\Exception\NoRunningTransactionException; | ||
use RemiSan\TransactionManager\Transactional; | ||
|
||
class TransactionalCommandBus extends CommandBus implements Transactional | ||
{ | ||
/** | ||
* @var CommandBus | ||
*/ | ||
private $commandBus; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $commandsToCommit; | ||
|
||
/** | ||
* @var boolean | ||
*/ | ||
private $transactionRunning; | ||
|
||
/** | ||
* TransactionalCommandBus constructor. | ||
* | ||
* @param CommandBus $commandBus | ||
*/ | ||
public function __construct(CommandBus $commandBus) | ||
{ | ||
parent::__construct([]); // Build it to prevent warnings but never use it | ||
|
||
$this->commandBus = $commandBus; | ||
|
||
$this->reset(); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function handle($command) | ||
{ | ||
if (!is_object($command)) { | ||
throw InvalidCommandException::forUnknownValue($command); | ||
} | ||
|
||
$this->checkTransactionIsRunning(); | ||
|
||
$this->commandsToCommit[] = $command; | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function beginTransaction() | ||
{ | ||
if ($this->isTransactionRunning()) { | ||
throw new BeginException(); | ||
} | ||
|
||
$this->set(); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function commit() | ||
{ | ||
$this->checkTransactionIsRunning(); | ||
|
||
foreach ($this->commandsToCommit as $command) { | ||
try { | ||
$this->commandBus->handle($command); | ||
} catch (\Exception $e) { | ||
throw new CommitException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
} | ||
|
||
$this->reset(); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function rollback() | ||
{ | ||
$this->checkTransactionIsRunning(); | ||
|
||
$this->reset(); | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
private function isTransactionRunning() | ||
{ | ||
return (boolean) $this->transactionRunning; | ||
} | ||
|
||
/** | ||
* @throws NoRunningTransactionException | ||
*/ | ||
private function checkTransactionIsRunning() | ||
{ | ||
if (! $this->isTransactionRunning()) { | ||
throw new NoRunningTransactionException(); | ||
} | ||
} | ||
|
||
private function set() | ||
{ | ||
$this->commandsToCommit = []; | ||
$this->transactionRunning = true; | ||
} | ||
|
||
private function reset() | ||
{ | ||
$this->commandsToCommit = null; | ||
$this->transactionRunning = false; | ||
} | ||
} |
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,89 @@ | ||
<?php | ||
|
||
namespace spec\RemiSan\TransactionManager\Command; | ||
|
||
use League\Tactician\CommandBus; | ||
use League\Tactician\Exception\InvalidCommandException; | ||
use PhpSpec\Exception\Exception; | ||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
use RemiSan\TransactionManager\Exception\BeginException; | ||
use RemiSan\TransactionManager\Exception\CommitException; | ||
use RemiSan\TransactionManager\Exception\NoRunningTransactionException; | ||
|
||
class TransactionalCommandBusSpec extends ObjectBehavior | ||
{ | ||
function let(CommandBus $commandBus) | ||
{ | ||
$this->beConstructedWith($commandBus); | ||
} | ||
|
||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType('RemiSan\TransactionManager\Command\TransactionalCommandBus'); | ||
} | ||
|
||
function it_should_handle_command_when_committing_if_transaction_is_running( | ||
CommandBus $commandBus, | ||
\stdClass $command | ||
) { | ||
$this->beginTransaction(); | ||
$this->handle($command); | ||
|
||
$commandBus->handle($command)->shouldBeCalled(); | ||
|
||
$this->commit(); | ||
} | ||
|
||
function it_should_not_handle_command_when_rollbacking(CommandBus $commandBus, \stdClass $command) | ||
{ | ||
$this->beginTransaction(); | ||
$this->handle($command); | ||
|
||
$commandBus->handle($command)->shouldNotBeCalled(); | ||
|
||
$this->rollback(); | ||
} | ||
|
||
function it_should_throw_an_exception_if_command_is_invalid() | ||
{ | ||
$this->beginTransaction(); | ||
$this->shouldThrow(InvalidCommandException::class) | ||
->duringHandle(''); | ||
} | ||
|
||
function it_should_throw_an_exception_committing_outside_a_transaction(\stdClass $command) | ||
{ | ||
$this->shouldThrow(NoRunningTransactionException::class) | ||
->duringHandle($command); | ||
} | ||
|
||
function it_should_throw_an_exception_if_sub_handle_fails(CommandBus $commandBus, \stdClass $command) | ||
{ | ||
$this->beginTransaction(); | ||
|
||
$this->handle($command); | ||
|
||
$commandBus->handle($command)->willThrow(new Exception()); | ||
|
||
$this->shouldThrow(CommitException::class) | ||
->duringCommit(); | ||
} | ||
|
||
function it_should_throw_an_exception_if_committing_outside_a_transaction() { | ||
$this->shouldThrow(NoRunningTransactionException::class) | ||
->duringCommit(); | ||
} | ||
|
||
function it_should_throw_an_exception_if_rollbacking_outside_a_transaction() { | ||
$this->shouldThrow(NoRunningTransactionException::class) | ||
->duringRollback(); | ||
} | ||
|
||
function it_should_not_be_possible_to_start_a_transaction_more_than_once() | ||
{ | ||
$this->beginTransaction(); | ||
$this->shouldThrow(BeginException::class) | ||
->duringBeginTransaction(); | ||
} | ||
} |