replace channel id with channel name

This commit is contained in:
freek 2018-12-01 12:26:08 +01:00
parent 81bbf83aa5
commit 84dad775e5
5 changed files with 33 additions and 33 deletions

View File

@ -28,18 +28,18 @@ class DashboardLogger
]); ]);
} }
public static function occupied(ConnectionInterface $connection, string $channelId) public static function occupied(ConnectionInterface $connection, string $channelName)
{ {
static::log($connection->client->appId, static::TYPE_OCCUPIED, [ static::log($connection->client->appId, static::TYPE_OCCUPIED, [
'details' => "Channel: {$channelId}", 'details' => "Channel: {$channelName}",
]); ]);
} }
public static function subscribed(ConnectionInterface $connection, string $channelId) public static function subscribed(ConnectionInterface $connection, string $channelName)
{ {
static::log($connection->client->appId, static::TYPE_SUBSCRIBED, [ static::log($connection->client->appId, static::TYPE_SUBSCRIBED, [
'socketId' => $connection->socketId, 'socketId' => $connection->socketId,
'details' => "Channel: {$channelId}", 'details' => "Channel: {$channelName}",
]); ]);
} }
@ -59,10 +59,10 @@ class DashboardLogger
]); ]);
} }
public static function vacated(ConnectionInterface $connection, string $channelId) public static function vacated(ConnectionInterface $connection, string $channelName)
{ {
static::log($connection->client->appId, static::TYPE_VACATED, [ static::log($connection->client->appId, static::TYPE_VACATED, [
'details' => "Channel: {$channelId}", 'details' => "Channel: {$channelName}",
]); ]);
} }
@ -76,13 +76,13 @@ class DashboardLogger
public static function log($appId, string $type, array $attributes = []) public static function log($appId, string $type, array $attributes = [])
{ {
$channelId = static::LOG_CHANNEL_PREFIX . $type; $channelName = static::LOG_CHANNEL_PREFIX . $type;
$channel = app(ChannelManager::class)->find($appId, $channelId); $channel = app(ChannelManager::class)->find($appId, $channelName);
optional($channel)->broadcast([ optional($channel)->broadcast([
'event' => 'log-message', 'event' => 'log-message',
'channel' => $channelId, 'channel' => $channelName,
'data' => [ 'data' => [
'type' => $type, 'type' => $type,
'time' => strftime("%H:%M:%S") 'time' => strftime("%H:%M:%S")

View File

@ -11,18 +11,18 @@ class TriggerEventController extends Controller
{ {
$this->ensureValidSignature($request); $this->ensureValidSignature($request);
foreach ($request->json()->get('channels', []) as $channelId) { foreach ($request->json()->get('channels', []) as $channelName) {
$channel = $this->channelManager->find($request->appId, $channelId); $channel = $this->channelManager->find($request->appId, $channelName);
optional($channel)->broadcastToEveryoneExcept([ optional($channel)->broadcastToEveryoneExcept([
'channel' => $channelId, 'channel' => $channelName,
'event' => $request->json()->get('name'), 'event' => $request->json()->get('name'),
'data' => $request->json()->get('data'), 'data' => $request->json()->get('data'),
], $request->json()->get('socket_id')); ], $request->json()->get('socket_id'));
DashboardLogger::apiMessage( DashboardLogger::apiMessage(
$request->appId, $request->appId,
$channelId, $channelName,
$request->json()->get('name'), $request->json()->get('name'),
$request->json()->get('data') $request->json()->get('data')
); );

View File

@ -11,14 +11,14 @@ use stdClass;
class Channel class Channel
{ {
/** @var string */ /** @var string */
protected $channelId; protected $channelName;
/** @var \Ratchet\ConnectionInterface[] */ /** @var \Ratchet\ConnectionInterface[] */
protected $subscriptions = []; protected $subscriptions = [];
public function __construct(string $channelId) public function __construct(string $channelName)
{ {
$this->channelId = $channelId; $this->channelId = $channelName;
} }
public function hasConnections(): bool public function hasConnections(): bool

View File

@ -12,29 +12,29 @@ class ChannelManager
/** @var array */ /** @var array */
protected $channels = []; protected $channels = [];
public function findOrCreate(string $appId, string $channelId): Channel public function findOrCreate(string $appId, string $channelName): Channel
{ {
if (!isset($this->channels[$appId][$channelId])) { if (!isset($this->channels[$appId][$channelName])) {
$channelClass = $this->determineChannelClass($channelId); $channelClass = $this->determineChannelClass($channelName);
$this->channels[$appId][$channelId] = new $channelClass($channelId);
}
return $this->channels[$appId][$channelId]; $this->channels[$appId][$channelName] = new $channelClass($channelName);
} }
public function find(string $appId, string $channelId): ?Channel return $this->channels[$appId][$channelName];
}
public function find(string $appId, string $channelName): ?Channel
{ {
return $this->channels[$appId][$channelId] ?? null; return $this->channels[$appId][$channelName] ?? null;
} }
protected function determineChannelClass(string $channelId): string protected function determineChannelClass(string $channelName): string
{ {
if (starts_with($channelId, 'private-')) { if (starts_with($channelName, 'private-')) {
return PrivateChannel::class; return PrivateChannel::class;
} }
if (starts_with($channelId, 'presence-')) { if (starts_with($channelName, 'presence-')) {
return PresenceChannel::class; return PresenceChannel::class;
} }
@ -62,8 +62,8 @@ class ChannelManager
*/ */
collect(array_get($this->channels, $connection->client->appId, [])) collect(array_get($this->channels, $connection->client->appId, []))
->reject->hasConnections() ->reject->hasConnections()
->each(function (Channel $channel, string $channelId) use ($connection) { ->each(function (Channel $channel, string $channelName) use ($connection) {
unset($this->channels[$connection->client->appId][$channelId]); unset($this->channels[$connection->client->appId][$channelName]);
}); });
if (count(array_get($this->channels, $connection->client->appId, [])) === 0) { if (count(array_get($this->channels, $connection->client->appId, [])) === 0) {

View File

@ -76,9 +76,9 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
return $connection; return $connection;
} }
protected function getChannel(ConnectionInterface $connection, string $channelId) protected function getChannel(ConnectionInterface $connection, string $channelName)
{ {
return $this->channelManager->findOrCreate($connection->client->appId, $channelId); return $this->channelManager->findOrCreate($connection->client->appId, $channelName);
} }
protected function markTestAsPassed() protected function markTestAsPassed()