Skip to content

Commit a0515e8

Browse files
committed
Added getFromContainer to client, small command bus optimization, added test for missing command bus
1 parent c2699ac commit a0515e8

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

src/Client.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use ApiClients\Tools\CommandBus\CommandBus;
66
use Interop\Container\ContainerInterface;
7-
use InvalidArgumentException;
87
use React\Promise\CancellablePromiseInterface;
98

109
final class Client
@@ -14,16 +13,18 @@ final class Client
1413
*/
1514
private $container;
1615

16+
/**
17+
* @var CommandBus
18+
*/
19+
private $commandBus;
20+
1721
/**
1822
* @param ContainerInterface $container
1923
*/
2024
public function __construct(ContainerInterface $container)
2125
{
2226
$this->container = $container;
23-
24-
if (!$this->container->has(CommandBus::class)) {
25-
throw new InvalidArgumentException();
26-
}
27+
$this->commandBus = $this->container->get(CommandBus::class);
2728
}
2829

2930
/**
@@ -34,8 +35,20 @@ public function getContainer(): ContainerInterface
3435
return $this->container;
3536
}
3637

38+
/**
39+
* @return mixed
40+
*/
41+
public function getFromContainer(string $id)
42+
{
43+
return $this->container->get($id);
44+
}
45+
46+
/**
47+
* @param $command
48+
* @return CancellablePromiseInterface
49+
*/
3750
public function handle($command): CancellablePromiseInterface
3851
{
39-
return $this->container->get(CommandBus::class)->handle($command);
52+
return $this->commandBus->handle($command);
4053
}
4154
}

tests/ClientTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ public function handle($command)
4545
$client = new Client($container);
4646

4747
$this->assertSame($container, $client->getContainer());
48+
$this->assertSame($commandBus, $client->getFromContainer(CommandBus::class));
4849
$this->assertSame($command, await($client->handle($command), $loop));
4950
}
51+
52+
/**
53+
* @expectedException \DI\Definition\Exception\DefinitionException
54+
*/
55+
public function testCommandBusMissing()
56+
{
57+
new Client(ContainerBuilder::buildDevContainer());
58+
}
5059
}

0 commit comments

Comments
 (0)