Skip to content

Commit c4b3aaf

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 24cd22d commit c4b3aaf

File tree

5 files changed

+36
-51
lines changed

5 files changed

+36
-51
lines changed

src/Client.php

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,17 @@
1313

1414
final class Client implements ClientInterface
1515
{
16-
/**
17-
* @var ContainerInterface
18-
*/
19-
private $container;
20-
2116
/**
2217
* @var CommandBusInterface
2318
*/
2419
private $commandBus;
2520

2621
/**
27-
* @param ContainerInterface $container
28-
*/
29-
public function __construct(ContainerInterface $container)
30-
{
31-
$this->container = $container;
32-
$this->commandBus = $this->container->get(CommandBusInterface::class);
33-
}
34-
35-
/**
36-
* @return ContainerInterface
37-
*/
38-
public function getContainer(): ContainerInterface
39-
{
40-
return $this->container;
41-
}
42-
43-
/**
44-
* @return mixed
22+
* @param CommandBusInterface $commandBus
4523
*/
46-
public function getFromContainer(string $id)
24+
public function __construct(CommandBusInterface $commandBus)
4725
{
48-
return $this->container->get($id);
26+
$this->commandBus = $commandBus;
4927
}
5028

5129
/**

src/ClientInterface.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,6 @@
88

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

src/Factory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ public static function create(
2121
LoopInterface $loop,
2222
array $options = []
2323
): Client {
24+
$container = self::createContainer($loop, $options);
2425
return new Client(
25-
self::createContainer($loop, $options)
26+
$container->get(CommandBusInterface::class)
2627
);
2728
}
2829

tests/ClientTest.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,8 @@ public function handle($command)
4444
);
4545

4646
$commandBus = new CommandBus($loop, $handlerMiddleware);
47+
$client = new Client($commandBus);
4748

48-
$container = ContainerBuilder::buildDevContainer();
49-
$container->set(CommandBusInterface::class, $commandBus);
50-
$client = new Client($container);
51-
52-
$this->assertSame($container, $client->getContainer());
53-
$this->assertSame($commandBus, $client->getFromContainer(CommandBusInterface::class));
5449
$this->assertSame($command, await($client->handle($command), $loop));
5550
}
5651

tests/FactoryTest.php

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818

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

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

28-
$client = Factory::create(
28+
$container = Factory::createContainer(
2929
$loop,
3030
[
3131
Options::HYDRATOR_OPTIONS => [],
@@ -36,16 +36,37 @@ public function testCreate()
3636
]
3737
);
3838

39-
$this->assertInstanceOf(Client::class, $client);
40-
41-
$container = $client->getContainer();
4239
$this->assertInstanceOf(LoopInterface::class, $container->get(LoopInterface::class));
4340
$this->assertSame($loop, $container->get(LoopInterface::class));
4441
$this->assertInstanceOf(Hydrator::class, $container->get(Hydrator::class));
4542
$this->assertInstanceOf(TransportClient::class, $container->get(ClientInterface::class));
4643
$this->assertInstanceOf(\stdClass::class, $container->get(\stdClass::class));
4744
$this->assertSame($stdClass, $container->get(\stdClass::class));
4845
$this->assertSame('bar', $container->get(\stdClass::class)->foo);
46+
}
47+
48+
public function testCreate()
49+
{
50+
$loop = LoopFactory::create();
51+
52+
$stdClass = new \stdClass();
53+
$stdClass->foo = 'bar';
54+
55+
$client = Factory::create(
56+
$loop,
57+
[
58+
Options::HYDRATOR_OPTIONS => [],
59+
Options::TRANSPORT_OPTIONS => [
60+
TransportOptions::USER_AGENT => 'User Agent',
61+
],
62+
Options::TRANSPORT_OPTIONS => [
63+
TransportOptions::USER_AGENT => '',
64+
],
65+
Options::CONTAINER_DEFINITIONS => [
66+
\stdClass::class => $stdClass,
67+
],
68+
]
69+
);
4970

5071
try {
5172
await($client->handle(new class() {
@@ -63,12 +84,12 @@ public function testCreate()
6384
*/
6485
public function testCreateMissingHydratorOptions()
6586
{
66-
Factory::create(
87+
Factory::createContainer(
6788
LoopFactory::create(),
6889
[
6990
Options::TRANSPORT_OPTIONS => [],
7091
]
71-
)->getContainer()->get(Hydrator::class);
92+
)->get(Hydrator::class);
7293
}
7394

7495
/**
@@ -77,11 +98,11 @@ public function testCreateMissingHydratorOptions()
7798
*/
7899
public function testCreateMissingTransportOptions()
79100
{
80-
Factory::create(
101+
Factory::createContainer(
81102
LoopFactory::create(),
82103
[
83104
Options::HYDRATOR_OPTIONS => [],
84105
]
85-
)->getContainer()->get(ClientInterface::class);
106+
)->get(ClientInterface::class);
86107
}
87108
}

0 commit comments

Comments
 (0)