Skip to content

Commit

Permalink
Improve CommandBus and add version to composer.json
Browse files Browse the repository at this point in the history
  • Loading branch information
Sasa Blagojevic authored and Sasa Blagojevic committed Dec 20, 2020
1 parent 6da69dd commit 49050b1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 28 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"php",
"command-bus"
],
"version": "0.1.0",
"license": "MIT",
"authors": [
{
Expand Down
39 changes: 18 additions & 21 deletions src/CommandBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use SasaB\CommandBus\Response\Map;
use SasaB\CommandBus\Response\Item;
use SasaB\CommandBus\Response\Text;
use SasaB\CommandBus\Response\Void;
use SasaB\CommandBus\Response\None;
use SasaB\CommandBus\Response\Double;
use SasaB\CommandBus\Response\Integer;
use SasaB\CommandBus\Response\Boolean;
Expand All @@ -37,7 +37,7 @@ public function __construct(ContainerInterface $diContainer, array $middleware =
$this->mapper = $mapper ?? new MapByName();
}

private function getHandlerFor(Command $command)
private function getHandlerFor(Command $command): Handler
{
return $this->diContainer->get(
$this->mapper->getHandlerName($command)
Expand All @@ -48,39 +48,36 @@ public function dispatch(Command $command): Response
{
$chain = $this->middlewares;

$response = $chain($command);
$response = $this->parseResponse(
$chain($command)
);

return $response->setUuid($command->uuid());
}

private function parseResponse($response)
{
switch ($response) {
case null:
$response = new Void();
break;
return new None();
case is_int($response):
$response = new Integer($response);
break;
return new Integer($response);
case is_float($response):
$response = new Double($response);
break;
return new Double($response);
case is_bool($response):
$response = new Boolean($response);
break;
return new Boolean($response);
case is_string($response):
$response = new Text($response);
break;
return new Text($response);
case is_array($response):
$is_map = $response && is_string(array_keys($response)[0]);
$response = $is_map
return $response && is_string(array_keys($response)[0])
? new Map($response)
: new Collection($response);
break;
case $response instanceof Response:
// do nothing, it's already a custom response object
break;
return $response;
default:
$response = new Item($response);
break;
return new Item($response);
}

return $response->setUuid($command->uuid());
}

private function createMiddlewareChain(array $chain): \Closure
Expand Down
7 changes: 2 additions & 5 deletions src/Response/Void.php → src/Response/None.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
namespace SasaB\CommandBus\Response;


final class Void extends Response
final class None extends Response
{
public function getContent()
{
return null;
}
public function getContent() {}
}
4 changes: 2 additions & 2 deletions tests/Unit/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use SasaB\CommandBus\Response\Item;
use SasaB\CommandBus\Response\Map;
use SasaB\CommandBus\Response\Text;
use SasaB\CommandBus\Response\Void;
use SasaB\CommandBus\Response\None;
use SasaB\CommandBus\Tests\TestCommand;
use SasaB\CommandBus\Tests\TestCase;

Expand All @@ -41,7 +41,7 @@ public function testItCanReturnVoidResponse()
new TestCommand()
);

self::assertInstanceOf(Void::class, $response);
self::assertInstanceOf(None::class, $response);
}

public function testItCanReturnIntegerResponse()
Expand Down

0 comments on commit 49050b1

Please sign in to comment.