Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

### New features

- Add `\BSP\CommandBus`
- Add `\BSP\Command`
- Add `\BSP\CommandHandler`

### Bugfixes

</details>
8 changes: 8 additions & 0 deletions src/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);

namespace BSP;

interface Command
{
}
17 changes: 17 additions & 0 deletions src/CommandBus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);

namespace BSP;

abstract class CommandBus
{
/** @var CommandHandler[] */
protected $handlers = [];

public function execute(Command $command): void
{
if (isset($this->handlers[get_class($command)])) {
$this->handlers[get_class($command)]->handle($command);
}
}
}
9 changes: 9 additions & 0 deletions src/CommandHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);

namespace BSP;

interface CommandHandler
{
public function handle(Command $command): void;
}
28 changes: 28 additions & 0 deletions tests/CommandBusTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);

namespace BSP\Tests;

use BSP\Tests\Mock\AddSugarToCoffee;
use BSP\Tests\Mock\AddSugarToCoffeHandler;
use BSP\Tests\Mock\Coffee;
use BSP\Tests\Mock\CoffeeCommandBus;
use PHPUnit\Framework\TestCase;

final class CommandBusTest extends TestCase
{
public function testCommandBus(): void
{
$coffee = new Coffee();

$this->assertSame(0, $coffee::$sugars);

$command = new AddSugarToCoffee(2, $coffee);
$commandHandler = new AddSugarToCoffeHandler();
$commandBus = new CoffeeCommandBus($commandHandler);

$commandBus->execute($command);

$this->assertSame(2, $coffee::$sugars);
}
}
19 changes: 19 additions & 0 deletions tests/Mock/AddSugarToCoffeHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);

namespace BSP\Tests\Mock;

use BSP\Command;
use BSP\CommandHandler;

final class AddSugarToCoffeHandler implements CommandHandler
{
/**
* @param AddSugarToCoffee $command
*/
public function handle(Command $command): void
{
$coffee = $command->coffee();
$coffee::$sugars += $command->nbSugars();
}
}
28 changes: 28 additions & 0 deletions tests/Mock/AddSugarToCoffee.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);

namespace BSP\Tests\Mock;

use BSP\Command;

final class AddSugarToCoffee implements Command
{
private $nbSugars;
private $coffee;

public function __construct(int $nbSugars, Coffee $coffee)
{
$this->nbSugars = $nbSugars;
$this->coffee = $coffee;
}

public function nbSugars(): int
{
return $this->nbSugars;
}

public function coffee(): Coffee
{
return $this->coffee;
}
}
9 changes: 9 additions & 0 deletions tests/Mock/Coffee.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);

namespace BSP\Tests\Mock;

final class Coffee
{
public static $sugars = 0;
}
14 changes: 14 additions & 0 deletions tests/Mock/CoffeeCommandBus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);

namespace BSP\Tests\Mock;

use BSP\CommandBus;

final class CoffeeCommandBus extends CommandBus
{
public function __construct(AddSugarToCoffeHandler $addSugarToCoffeHandler)
{
$this->handlers[AddSugarToCoffee::class] = $addSugarToCoffeHandler;
}
}