Skip to content

Commit b78911b

Browse files
committed
SharedAppClientHandler
1 parent c4abea5 commit b78911b

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Pusher\CommandBus\Handler;
4+
5+
use ApiClients\Client\Pusher\AsyncClient;
6+
use ApiClients\Client\Pusher\CommandBus\Command\SharedAppClientCommand;
7+
use React\EventLoop\LoopInterface;
8+
use React\Promise\PromiseInterface;
9+
use function React\Promise\resolve;
10+
use function WyriHaximus\React\futureFunctionPromise;
11+
12+
final class SharedAppClientHandler
13+
{
14+
/**
15+
* @var LoopInterface
16+
*/
17+
private $loop;
18+
19+
/**
20+
* @var array
21+
*/
22+
private $apps = [];
23+
24+
/**
25+
* SharedAppClientHandler constructor.
26+
* @param LoopInterface $loop
27+
*/
28+
public function __construct(LoopInterface $loop)
29+
{
30+
$this->loop = $loop;
31+
}
32+
33+
/**
34+
* @param SharedAppClientCommand $command
35+
* @return PromiseInterface
36+
*/
37+
public function handle(SharedAppClientCommand $command): PromiseInterface
38+
{
39+
if (isset($this->apps[$command->getAppId()])) {
40+
return resolve($this->apps[$command->getAppId()]);
41+
}
42+
43+
$this->apps[$command->getAppId()] = new AsyncClient($this->loop, $command->getAppId());
44+
return resolve($this->apps[$command->getAppId()]);
45+
}
46+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Tests\Client\Pusher\CommandBus\Handler;
4+
5+
use ApiClients\Client\Pusher\AsyncClient;
6+
use ApiClients\Client\Pusher\CommandBus\Command\SharedAppClientCommand;
7+
use ApiClients\Client\Pusher\CommandBus\Handler\SharedAppClientHandler;
8+
use ApiClients\Tools\TestUtilities\TestCase;
9+
use React\EventLoop\Factory;
10+
use function Clue\React\Block\await;
11+
12+
class SharedAppClientHandlerTest extends TestCase
13+
{
14+
public function testHandle()
15+
{
16+
$loop = Factory::create();
17+
$appId = uniqid('app-id-', true);
18+
$handler = new SharedAppClientHandler($loop);
19+
20+
$app = await($handler->handle(new SharedAppClientCommand($appId)), $loop);
21+
$this->assertInstanceOf(AsyncClient::class, await($handler->handle(new SharedAppClientCommand($appId)), $loop));
22+
$this->assertSame($app, await($handler->handle(new SharedAppClientCommand($appId)), $loop));
23+
$this->assertNotSame($app, await($handler->handle(new SharedAppClientCommand(md5($appId))), $loop));
24+
}
25+
}

0 commit comments

Comments
 (0)