Skip to content

Commit 49050b1

Browse files
Sasa BlagojevicSasa Blagojevic
authored andcommitted
Improve CommandBus and add version to composer.json
1 parent 6da69dd commit 49050b1

File tree

4 files changed

+23
-28
lines changed

4 files changed

+23
-28
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"php",
77
"command-bus"
88
],
9+
"version": "0.1.0",
910
"license": "MIT",
1011
"authors": [
1112
{

src/CommandBus.php

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use SasaB\CommandBus\Response\Map;
1616
use SasaB\CommandBus\Response\Item;
1717
use SasaB\CommandBus\Response\Text;
18-
use SasaB\CommandBus\Response\Void;
18+
use SasaB\CommandBus\Response\None;
1919
use SasaB\CommandBus\Response\Double;
2020
use SasaB\CommandBus\Response\Integer;
2121
use SasaB\CommandBus\Response\Boolean;
@@ -37,7 +37,7 @@ public function __construct(ContainerInterface $diContainer, array $middleware =
3737
$this->mapper = $mapper ?? new MapByName();
3838
}
3939

40-
private function getHandlerFor(Command $command)
40+
private function getHandlerFor(Command $command): Handler
4141
{
4242
return $this->diContainer->get(
4343
$this->mapper->getHandlerName($command)
@@ -48,39 +48,36 @@ public function dispatch(Command $command): Response
4848
{
4949
$chain = $this->middlewares;
5050

51-
$response = $chain($command);
51+
$response = $this->parseResponse(
52+
$chain($command)
53+
);
54+
55+
return $response->setUuid($command->uuid());
56+
}
5257

58+
private function parseResponse($response)
59+
{
5360
switch ($response) {
5461
case null:
55-
$response = new Void();
56-
break;
62+
return new None();
5763
case is_int($response):
58-
$response = new Integer($response);
59-
break;
64+
return new Integer($response);
6065
case is_float($response):
61-
$response = new Double($response);
62-
break;
66+
return new Double($response);
6367
case is_bool($response):
64-
$response = new Boolean($response);
65-
break;
68+
return new Boolean($response);
6669
case is_string($response):
67-
$response = new Text($response);
68-
break;
70+
return new Text($response);
6971
case is_array($response):
70-
$is_map = $response && is_string(array_keys($response)[0]);
71-
$response = $is_map
72+
return $response && is_string(array_keys($response)[0])
7273
? new Map($response)
7374
: new Collection($response);
74-
break;
7575
case $response instanceof Response:
7676
// do nothing, it's already a custom response object
77-
break;
77+
return $response;
7878
default:
79-
$response = new Item($response);
80-
break;
79+
return new Item($response);
8180
}
82-
83-
return $response->setUuid($command->uuid());
8481
}
8582

8683
private function createMiddlewareChain(array $chain): \Closure

src/Response/Void.php renamed to src/Response/None.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99
namespace SasaB\CommandBus\Response;
1010

1111

12-
final class Void extends Response
12+
final class None extends Response
1313
{
14-
public function getContent()
15-
{
16-
return null;
17-
}
14+
public function getContent() {}
1815
}

tests/Unit/ResponseTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use SasaB\CommandBus\Response\Item;
1818
use SasaB\CommandBus\Response\Map;
1919
use SasaB\CommandBus\Response\Text;
20-
use SasaB\CommandBus\Response\Void;
20+
use SasaB\CommandBus\Response\None;
2121
use SasaB\CommandBus\Tests\TestCommand;
2222
use SasaB\CommandBus\Tests\TestCase;
2323

@@ -41,7 +41,7 @@ public function testItCanReturnVoidResponse()
4141
new TestCommand()
4242
);
4343

44-
self::assertInstanceOf(Void::class, $response);
44+
self::assertInstanceOf(None::class, $response);
4545
}
4646

4747
public function testItCanReturnIntegerResponse()

0 commit comments

Comments
 (0)