From cf09129cc233f66d424d481e32cc2ba564b81bd1 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Thu, 19 Aug 2021 10:24:41 +0200 Subject: [PATCH] fix(realtime): adapt to psalm --- app/realtime.php | 2 +- src/Appwrite/Messaging/Adapter.php | 4 +-- src/Appwrite/Messaging/Adapter/Realtime.php | 30 +++++++++++---------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/app/realtime.php b/app/realtime.php index 67a1d68e35..eb89df33be 100644 --- a/app/realtime.php +++ b/app/realtime.php @@ -260,7 +260,7 @@ $server->onWorkerStart(function (int $workerId) use ($server, $register, $stats, Console::error('Pub/sub failed (worker: ' . $workerId . ')'); } - $redis->subscribe(['realtime'], function ($redis, $channel, $payload) use ($server, $workerId, $stats, $register, $realtime) { + $redis->subscribe(['realtime'], function (Redis $redis, string $channel, string $payload) use ($server, $workerId, $stats, $register, $realtime) { $event = json_decode($payload, true); if ($event['permissionsChanged'] && isset($event['userId'])) { diff --git a/src/Appwrite/Messaging/Adapter.php b/src/Appwrite/Messaging/Adapter.php index d788e34d13..6ef2d5cfde 100644 --- a/src/Appwrite/Messaging/Adapter.php +++ b/src/Appwrite/Messaging/Adapter.php @@ -4,7 +4,7 @@ namespace Appwrite\Messaging; abstract class Adapter { - public abstract function subscribe(string $project, mixed $identifier, array $roles, array $channels): void; + public abstract function subscribe(string $projectId, mixed $identifier, array $roles, array $channels): void; public abstract function unsubscribe(mixed $identifier): void; - public static abstract function send(string $projectId, array $payload, string $event, array $channels, array $permissions, array $options): void; + public static abstract function send(string $projectId, array $payload, string $event, array $channels, array $roles, array $options): void; } diff --git a/src/Appwrite/Messaging/Adapter/Realtime.php b/src/Appwrite/Messaging/Adapter/Realtime.php index 1bd1245a96..98a9b30fac 100644 --- a/src/Appwrite/Messaging/Adapter/Realtime.php +++ b/src/Appwrite/Messaging/Adapter/Realtime.php @@ -35,15 +35,15 @@ class Realtime extends Adapter /** * Adds a subscribtion. - * @param string $projectId Project ID. - * @param mixed $connection Unique Identifier - Connection ID. - * @param array $roles Roles of the Subscription. - * @param array $channels Subscribed Channels. + * + * @param string $projectId + * @param mixed $identifier + * @param array $roles + * @param array $channels * @return void */ - public function subscribe(string $projectId, mixed $connection, array $roles, array $channels): void + public function subscribe(string $projectId, mixed $identifier, array $roles, array $channels): void { - //TODO: merge project & channel to a single layer if (!isset($this->subscriptions[$projectId])) { // Init Project $this->subscriptions[$projectId] = []; } @@ -54,11 +54,11 @@ class Realtime extends Adapter } foreach ($channels as $channel => $list) { - $this->subscriptions[$projectId][$role][$channel][$connection] = true; + $this->subscriptions[$projectId][$role][$channel][$identifier] = true; } } - $this->connections[$connection] = [ + $this->connections[$identifier] = [ 'projectId' => $projectId, 'roles' => $roles, 'channels' => $channels @@ -119,7 +119,7 @@ class Realtime extends Adapter /** * Sends an event to the Realtime Server. - * @param string $project + * @param string $projectId * @param array $payload * @param string $event * @param array $channels @@ -127,9 +127,9 @@ class Realtime extends Adapter * @param array $options * @return void */ - public static function send(string $project, array $payload, string $event, array $channels, array $roles, array $options = []): void + public static function send(string $projectId, array $payload, string $event, array $channels, array $roles, array $options = []): void { - if (empty($channels) || empty($roles) || empty($project)) return; + if (empty($channels) || empty($roles) || empty($projectId)) return; $permissionsChanged = array_key_exists('permissionsChanged', $options) && $options['permissionsChanged']; $userId = array_key_exists('userId', $options) ? $options['userId'] : null; @@ -137,7 +137,7 @@ class Realtime extends Adapter $redis = new \Redis(); //TODO: make this part of the constructor $redis->connect(App::getEnv('_APP_REDIS_HOST', ''), App::getEnv('_APP_REDIS_PORT', '')); $redis->publish('realtime', json_encode([ - 'project' => $project, + 'project' => $projectId, 'roles' => $roles, 'permissionsChanged' => $permissionsChanged, 'userId' => $userId, @@ -224,8 +224,10 @@ class Realtime extends Adapter /** * Create channels array based on the event name and payload. - * - * @return void + * + * @param string $event + * @param Document $payload + * @return array */ public static function fromPayload(string $event, Document $payload): array {