Skip to content

Commit 63776a4

Browse files
committed
Removes client container connection
Client was leaking the applications container. When needed the container can inject itself into handlers. There is no need to have this in the client.
1 parent 27cd1aa commit 63776a4

File tree

5 files changed

+37
-60
lines changed

5 files changed

+37
-60
lines changed

src/Client.php

+3-25
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,17 @@
88

99
final class Client implements ClientInterface
1010
{
11-
/**
12-
* @var ContainerInterface
13-
*/
14-
private $container;
15-
1611
/**
1712
* @var CommandBusInterface
1813
*/
1914
private $commandBus;
2015

2116
/**
22-
* @param ContainerInterface $container
23-
*/
24-
public function __construct(ContainerInterface $container)
25-
{
26-
$this->container = $container;
27-
$this->commandBus = $this->container->get(CommandBusInterface::class);
28-
}
29-
30-
/**
31-
* @return ContainerInterface
32-
*/
33-
public function getContainer(): ContainerInterface
34-
{
35-
return $this->container;
36-
}
37-
38-
/**
39-
* @return mixed
17+
* @param CommandBusInterface $commandBus
4018
*/
41-
public function getFromContainer(string $id)
19+
public function __construct(CommandBusInterface $commandBus)
4220
{
43-
return $this->container->get($id);
21+
$this->commandBus = $commandBus;
4422
}
4523

4624
/**

src/ClientInterface.php

-10
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,6 @@
77

88
interface ClientInterface
99
{
10-
/**
11-
* @return ContainerInterface
12-
*/
13-
public function getContainer(): ContainerInterface;
14-
15-
/**
16-
* @return mixed
17-
*/
18-
public function getFromContainer(string $id);
19-
2010
/**
2111
* @param $command
2212
* @return CancellablePromiseInterface

src/Factory.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ public static function create(
1919
LoopInterface $loop,
2020
array $options = []
2121
): Client {
22+
$container = self::createContainer($loop, $options);
2223
return new Client(
23-
self::createContainer($loop, $options)
24+
$container->get(CommandBusInterface::class)
2425
);
2526
}
2627

27-
private static function createContainer(LoopInterface $loop, array $options): ContainerInterface
28+
public static function createContainer(LoopInterface $loop, array $options): ContainerInterface
2829
{
2930
$container = new ContainerBuilder();
3031

tests/ClientTest.php

+1-14
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,8 @@ public function handle($command)
4040
);
4141

4242
$commandBus = new CommandBus($loop, $handlerMiddleware);
43+
$client = new Client($commandBus);
4344

44-
$container = ContainerBuilder::buildDevContainer();
45-
$container->set(CommandBusInterface::class, $commandBus);
46-
$client = new Client($container);
47-
48-
$this->assertSame($container, $client->getContainer());
49-
$this->assertSame($commandBus, $client->getFromContainer(CommandBusInterface::class));
5045
$this->assertSame($command, await($client->handle($command), $loop));
5146
}
52-
53-
/**
54-
* @expectedException \DI\Definition\Exception\DefinitionException
55-
*/
56-
public function testCommandBusMissing()
57-
{
58-
new Client(ContainerBuilder::buildDevContainer());
59-
}
6047
}

tests/FactoryTest.php

+30-9
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919

2020
final class FactoryTest extends TestCase
2121
{
22-
public function testCreate()
22+
public function testcreateContainer()
2323
{
2424
$loop = LoopFactory::create();
2525

2626
$stdClass = new \stdClass();
2727
$stdClass->foo = 'bar';
2828

29-
$client = Factory::create(
29+
$container = Factory::createContainer(
3030
$loop,
3131
[
3232
Options::HYDRATOR_OPTIONS => [],
@@ -42,16 +42,37 @@ public function testCreate()
4242
]
4343
);
4444

45-
$this->assertInstanceOf(Client::class, $client);
46-
47-
$container = $client->getContainer();
4845
$this->assertInstanceOf(LoopInterface::class, $container->get(LoopInterface::class));
4946
$this->assertSame($loop, $container->get(LoopInterface::class));
5047
$this->assertInstanceOf(Hydrator::class, $container->get(Hydrator::class));
5148
$this->assertInstanceOf(TransportClient::class, $container->get(ClientInterface::class));
5249
$this->assertInstanceOf(\stdClass::class, $container->get(\stdClass::class));
5350
$this->assertSame($stdClass, $container->get(\stdClass::class));
5451
$this->assertSame('bar', $container->get(\stdClass::class)->foo);
52+
}
53+
54+
public function testCreate()
55+
{
56+
$loop = LoopFactory::create();
57+
58+
$stdClass = new \stdClass();
59+
$stdClass->foo = 'bar';
60+
61+
$client = Factory::create(
62+
$loop,
63+
[
64+
Options::HYDRATOR_OPTIONS => [],
65+
Options::TRANSPORT_OPTIONS => [
66+
TransportOptions::USER_AGENT => 'User Agent',
67+
],
68+
Options::TRANSPORT_OPTIONS => [
69+
TransportOptions::USER_AGENT => '',
70+
],
71+
Options::CONTAINER_DEFINITIONS => [
72+
\stdClass::class => $stdClass,
73+
],
74+
]
75+
);
5576

5677
try {
5778
await($client->handle(new class() {}), $loop);
@@ -69,12 +90,12 @@ public function testCreate()
6990
*/
7091
public function testCreateMissingHydratorOptions()
7192
{
72-
Factory::create(
93+
Factory::createContainer(
7394
LoopFactory::create(),
7495
[
7596
Options::TRANSPORT_OPTIONS => [],
7697
]
77-
)->getContainer()->get(Hydrator::class);
98+
)->get(Hydrator::class);
7899
}
79100

80101
/**
@@ -83,11 +104,11 @@ public function testCreateMissingHydratorOptions()
83104
*/
84105
public function testCreateMissingTransportOptions()
85106
{
86-
Factory::create(
107+
Factory::createContainer(
87108
LoopFactory::create(),
88109
[
89110
Options::HYDRATOR_OPTIONS => [],
90111
]
91-
)->getContainer()->get(ClientInterface::class);
112+
)->get(ClientInterface::class);
92113
}
93114
}

0 commit comments

Comments
 (0)