Skip to content

Commit

Permalink
Refactored AblyBroadcaster and tests with socketIdObject
Browse files Browse the repository at this point in the history
  • Loading branch information
sacOO7 committed Sep 24, 2024
1 parent fdac7b0 commit ced106e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 33 deletions.
28 changes: 9 additions & 19 deletions src/AblyBroadcaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Ably\LaravelBroadcaster;

use _PHPStan_cb8f9103f\Nette\Neon\Exception;
use Ably\AblyRest;
use Ably\Exceptions\AblyException;
use Ably\Models\Message as AblyMessage;
Expand Down Expand Up @@ -169,17 +168,19 @@ public function validAuthenticationResponse($request, $result)
/**
* Broadcast the given event.
*
* @param array $channels
* @param string $event
* @param array $payload
* @param array $channels
* @param string $event
* @param array $payload
* @return void
*
* @throws \Illuminate\Broadcasting\BroadcastException
* @throws \Exception
*/
public function broadcast($channels, $event, $payload = [])
{
$socketIdObject = Utils::decodeSocketId(Arr::pull($payload, 'socket'));
$socketId = Arr::pull($payload, 'socket');
try {
$socketIdObject = Utils::decodeSocketId($socketId);
foreach ($this->formatChannels($channels) as $channel) {
$this->ably->channels->get($channel)->publish(
$this->buildAblyMessage($event, $payload, $socketIdObject)
Expand Down Expand Up @@ -314,9 +315,8 @@ public function formatChannels($channels)
*
* @param string $event
* @param array $payload
* @param array|null $socketIdObject
* @param object $socketIdObject
* @return AblyMessage
* @throws Exception
*/
protected function buildAblyMessage($event, $payload = [], $socketIdObject = null)
{
Expand All @@ -326,18 +326,8 @@ protected function buildAblyMessage($event, $payload = [], $socketIdObject = nul
});

if ($socketIdObject) {
$connectionKey_key = 'connectionKey';
if (array_key_exists($connectionKey_key, $socketIdObject)) {
$message->connectionKey = $socketIdObject[$connectionKey_key];
} else {
throw new Exception(Utils::missingKeyErrorForSocketId($connectionKey_key));
}
$clientId_key = 'clientId';
if (array_key_exists($clientId_key, $socketIdObject)) {
$message->clientId = $socketIdObject[$clientId_key];
} else {
throw new Exception(Utils::missingKeyErrorForSocketId($clientId_key));
}
$message->connectionKey = $socketIdObject->connectionKey;
$message->clientId = $socketIdObject->clientId;
}
return $message;
}
Expand Down
28 changes: 21 additions & 7 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Ably\LaravelBroadcaster;

use Ably\Exceptions\AblyException;
use Illuminate\Broadcasting\BroadcastException;

class Utils
Expand Down Expand Up @@ -65,15 +66,28 @@ public static function base64urlEncode($str)
return rtrim(strtr(base64_encode($str), '+/', '-_'), '=');
}


const SOCKET_ID_ERROR = "please make sure to send base64 encoded json with "
."'connectionKey' and 'clientId' as keys. 'clientId' is null if connection is not identified";

/**
* @throws AblyException
*/
public static function decodeSocketId($socketId) {
$socketIdObject = null;
if ($socketId) {
return json_decode(base64_decode($socketId), true);
try {
$socketIdObject = json_decode(base64_decode($socketId));
} catch (\Exception $e) {
throw new AblyException("SocketId decoding failed, ".self::SOCKET_ID_ERROR, 0, $e);
}
if (!isset($socketIdObject->connectionKey)) {
throw new AblyException("ConnectionKey is missing, ".self::SOCKET_ID_ERROR);
}
if (!isset($socketIdObject->clientId)) {
throw new AblyException("ClientId is missing, ".self::SOCKET_ID_ERROR);
}
}
return null;
}

public static function missingKeyErrorForSocketId($keyName) {
return $keyName." not present in socketId, please make sure to send base64 encoded json with "
."connectionKey and clientId as keys. clientId is null if connection is not identified.";
return $socketIdObject;
}
}
18 changes: 11 additions & 7 deletions tests/AblyBroadcasterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,25 +347,29 @@ public function testPayloadShouldNotIncludeSocketKey()
// self::assertArrayNotHasKey('socket', $message->data);

}
public function testBuildMessageUsingProvidedSocketIdObject()
public function testBuildMessageBasedOnSocketIdObject()
{
$broadcaster = m::mock(AblyBroadcasterExposed::class, [$this->ably, []])->makePartial();

$payload = [
'foo' => 'bar',
'chat' => 'hello there'
];
$message = $broadcaster->buildAblyMessage('testEvent', $payload);
self::assertEquals('testEvent', $message->name);
self::assertEquals($payload, $message->data);
self::assertNull($message->connectionKey);
self::assertNull($message->clientId);

$socketIdObject = [
'connectionKey' => 'key',
'clientId' => 'id'
];
$socketIdObject = new \stdClass();
$socketIdObject->connectionKey = 'foo';
$socketIdObject->clientId = 'sacOO7';

$message = $broadcaster->buildAblyMessage('testEvent', $payload, $socketIdObject);
self::assertEquals('key', $message->connectionKey);
self::assertEquals('id', $message->clientId);
self::assertEquals('testEvent', $message->name);
self::assertEquals($payload, $message->data);
self::assertEquals('foo', $message->connectionKey);
self::assertEquals('sacOO7', $message->clientId);
}
}

Expand Down

0 comments on commit ced106e

Please sign in to comment.