Skip to content

Commit 09928ad

Browse files
committed
Shared app client service
1 parent 8bb4843 commit 09928ad

File tree

2 files changed

+53
-18
lines changed

2 files changed

+53
-18
lines changed

src/CommandBus/Handler/SharedAppClientHandler.php

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,26 @@
22

33
namespace ApiClients\Client\Pusher\CommandBus\Handler;
44

5-
use ApiClients\Client\Pusher\AsyncClient;
65
use ApiClients\Client\Pusher\CommandBus\Command\SharedAppClientCommand;
7-
use React\EventLoop\LoopInterface;
6+
use ApiClients\Client\Pusher\Service\SharedAppClientService;
87
use React\Promise\PromiseInterface;
98
use function React\Promise\resolve;
109
use function WyriHaximus\React\futureFunctionPromise;
1110

1211
final class SharedAppClientHandler
1312
{
1413
/**
15-
* @var LoopInterface
14+
* @var SharedAppClientService
1615
*/
17-
private $loop;
18-
19-
/**
20-
* @var array
21-
*/
22-
private $apps = [];
16+
private $service;
2317

2418
/**
2519
* SharedAppClientHandler constructor.
26-
* @param LoopInterface $loop
20+
* @param SharedAppClientService $service
2721
*/
28-
public function __construct(LoopInterface $loop)
22+
public function __construct(SharedAppClientService $service)
2923
{
30-
$this->loop = $loop;
24+
$this->service = $service;
3125
}
3226

3327
/**
@@ -36,11 +30,6 @@ public function __construct(LoopInterface $loop)
3630
*/
3731
public function handle(SharedAppClientCommand $command): PromiseInterface
3832
{
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()]);
33+
return resolve($this->service->handle($command->getAppId()));
4534
}
4635
}
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\Service;
4+
5+
use ApiClients\Client\Pusher\AsyncClient;
6+
use ApiClients\Foundation\Service\ServiceInterface;
7+
use React\EventLoop\LoopInterface;
8+
use React\Promise\CancellablePromiseInterface;
9+
use function React\Promise\resolve;
10+
use function WyriHaximus\React\futureFunctionPromise;
11+
12+
final class SharedAppClientService implements ServiceInterface
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 string|null $appId
35+
* @return CancellablePromiseInterface
36+
*/
37+
public function handle(string $appId = null): CancellablePromiseInterface
38+
{
39+
if (isset($this->apps[$appId])) {
40+
return resolve($this->apps[$appId]);
41+
}
42+
43+
$this->apps[$appId] = new AsyncClient($this->loop, $appId);
44+
return resolve($this->apps[$appId]);
45+
}
46+
}

0 commit comments

Comments
 (0)