Skip to content

Commit

Permalink
Keep Client synced with refactored Gockets:
Browse files Browse the repository at this point in the history
- Updated API endpoints (rename GET /channel/show, add PATCH /channel/edit)
- Updated Channel model (tag field added)
- Implemented ChannelOptions model to have more consistent options when preparing or updating channel.

Also updated unit tests and composer dependencies. Upgraded gockets execution binary in bin directory.
  • Loading branch information
therealartz committed May 14, 2019
1 parent eddd4a2 commit 449b6fa
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 37 deletions.
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@ $client = new Client(new Params($host, $port));

#### Prepare channel

Creates new channel.
Creates new channel. Can accept optional argument instance of `Gockets\Model\ChannelOptions`.

```php
// Using variable from previous example
use Gockets\Model\ChannelOptions;

$channel = $client->prepare();
$options = new ChannelOptions('http://localhost/hook.php', 'tag');

// Using $client from previous sample

$channel = $client->prepare($options);
```

`Gockets\Model\Сhannel` example:
Expand All @@ -51,7 +55,8 @@ $channel = $client->prepare();
object(Gockets\Model\Channel) {
["publisherToken":private] => string(32) "f177965656399535ea241a3da40dfcbf"
["subscriberToken":private] => string(32) "90b09a2e2d43c83ed907854a46c710fd"
["hookUrl":private] => NULL
["hookUrl":private] => string(25) "http://localhost/hook.php"
["tag":private] => string(3) "tag"
["listeners":private] => int(0)
}
```
Expand All @@ -74,6 +79,18 @@ Returns empty or filled with `Gockets\Model\Сhannel` objects array.
$channels = $client->showAll();
```

#### Edit channel

Use to modify specific channel attributes (change hook url or tag).

```php
use Gockets\Model\ChannelOptions;

$options = new ChannelOptions('http://localhost/new_hook.php', 'someApplication|tagged');

$updatedChannel = $client->update($channel->getPublisherToken(), $options);
```

#### Publish data

Send some data to channel.
Expand Down
6 changes: 6 additions & 0 deletions bin/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
managementProtect: true
allowedHostnames:
- localhost
pingInterval: 10
logLevel: 3
port: 8844
Binary file modified bin/main
Binary file not shown.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "gockets-project/gockets-php",
"type": "library",
"version": "0.1.0",
"version": "0.2.0",
"description": "PHP client for Gockets https://github.com/gockets-project/gockets",
"keywords": ["gockets", "socket", "websocket", "php websocket"],
"require": {
Expand Down
32 changes: 16 additions & 16 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/Gockets/Adapter/ChannelAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ private function stdClassToChannel(stdClass $channel): Channel
$channel->publisher_token,
$channel->subscriber_token,
$channel->subscriber_message_hook_url,
$channel->tag,
$channel->listeners
);
}
Expand Down
37 changes: 30 additions & 7 deletions src/Gockets/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Gockets\Exception\ChannelNotFoundException;
use Gockets\Factory\HttpFactory;
use Gockets\Model\Channel;
use Gockets\Model\ChannelOptions;
use Gockets\Model\Params;
use Gockets\Model\Response;

Expand Down Expand Up @@ -49,15 +50,23 @@ public function __construct(Params $params)
self::$http = HttpFactory::createHttpClient($params);
}

public function prepare(?string $hookUrl = null): Channel
public function prepare(?ChannelOptions $channelOptions = null): Channel
{
if (is_null($hookUrl)) {
if (is_null($channelOptions)) {
$options = [];
} else {
$body = [];

if (!empty($channelOptions->getHookUrl())) {
$body['subscriber_message_hook_url'] = $channelOptions->getHookUrl();
}

if (!empty($channelOptions->getTag())) {
$body['tag'] = $channelOptions->getTag();
}

$options = [
\GuzzleHttp\RequestOptions::JSON => [
'subscriber_message_hook_url' => $hookUrl,
],
\GuzzleHttp\RequestOptions::JSON => $body,
];
}

Expand All @@ -68,7 +77,7 @@ public function prepare(?string $hookUrl = null): Channel

public function show(string $publisherToken): Channel
{
$result = self::$http->get("channel/{$publisherToken}");
$result = self::$http->get("channel/show/{$publisherToken}");

if ($result->getStatusCode() === 404) {
throw new ChannelNotFoundException($result->getBody()->getContents());
Expand All @@ -79,11 +88,25 @@ public function show(string $publisherToken): Channel

public function showAll(): array
{
$result = self::$http->get('channel');
$result = self::$http->get('channel/show');

return $this->channelAdapter->convertJsonArray($result->getBody()->getContents());
}

public function edit(string $publisherToken, ChannelOptions $channelOptions): Channel
{
$options = [
\GuzzleHttp\RequestOptions::JSON => [
'subscriber_message_hook_url' => $channelOptions->getHookUrl(),
'tag' => $channelOptions->getTag(),
],
];

$result = self::$http->patch("channel/edit/{$publisherToken}", $options);

return $this->channelAdapter->convertJson($result->getBody()->getContents());
}

public function publish($data, string $publisherToken): Response
{
$result = self::$http->post("channel/publish/{$publisherToken}", [
Expand Down
14 changes: 12 additions & 2 deletions src/Gockets/Contract/GocketsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Gockets\Exception\ChannelNotFoundException;
use Gockets\Model\Channel;
use Gockets\Model\ChannelOptions;
use Gockets\Model\Response;

/**
Expand All @@ -16,10 +17,10 @@ interface GocketsInterface
/**
* Prepares channel with specifyed (optionally) hook URL.
*
* @param string|null $hookUrl
* @param ChannelOptions|null $channelOptions
* @return Channel
*/
public function prepare(?string $hookUrl = null): Channel;
public function prepare(?ChannelOptions $channelOptions = null): Channel;

/**
* Get specific channel.
Expand All @@ -37,6 +38,15 @@ public function show(string $publisherToken): Channel;
*/
public function showAll(): array;

/**
* Edit specific channel (change hook url or tag).
*
* @param string $publisherToken Publisher token of channel to update
* @param ChannelOptions $channelOptions
* @return Channel
*/
public function edit(string $publisherToken, ChannelOptions $channelOptions): Channel;

/**
* Pushes data to a Websocket connection passed in body of request.
*
Expand Down
17 changes: 15 additions & 2 deletions src/Gockets/Model/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@ final class Channel

private $hookUrl;

private $tag;

private $listeners;

public function __construct(string $publisherToken, string $subscriberToken, ?string $hookUrl = null, int $listeners = 0)
{
public function __construct(
string $publisherToken,
string $subscriberToken,
?string $hookUrl = null,
?string $tag = null,
int $listeners = 0
) {
$this->publisherToken = $publisherToken;
$this->subscriberToken = $subscriberToken;
$this->hookUrl = $hookUrl;
$this->tag = $tag;
$this->listeners = $listeners;
}

Expand All @@ -38,6 +46,11 @@ public function getHookUrl(): ?string
return $this->hookUrl;
}

public function getTag(): ?string
{
return $this->tag;
}

public function getListeners(): int
{
return $this->listeners;
Expand Down
29 changes: 29 additions & 0 deletions src/Gockets/Model/ChannelOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types=1);

namespace Gockets\Model;

/**
* Channel options object to be sent on channel prepare
*/
final class ChannelOptions
{
private $hookUrl;

private $tag;

public function __construct(?string $hookUrl, ?string $tag)
{
$this->hookUrl = $hookUrl;
$this->tag = $tag;
}

public function getHookUrl(): ?string
{
return $this->hookUrl;
}

public function getTag(): ?string
{
return $this->tag;
}
}
8 changes: 6 additions & 2 deletions tests/Gockets/Adapter/ChannelAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,30 @@ final class ChannelAdapterTest extends TestCase
public function testConvertJson(): void
{
$adapter = new ChannelAdapter();
$json = '{"publisher_token":"ce5c546376052619973c1c660e2790f7","subscriber_token":"6f682b85480508142b3d765bbec58ab1","subscriber_message_hook_url":"http://localhost/log.php","listeners":0}';
$json = '{"publisher_token":"ce5c546376052619973c1c660e2790f7","subscriber_token":"6f682b85480508142b3d765bbec58ab1","subscriber_message_hook_url":"http://localhost/log.php","tag":null,"listeners":0}';

$channel = $adapter->convertJson($json);

Assert::assertEquals('ce5c546376052619973c1c660e2790f7', $channel->getPublisherToken());
Assert::assertEquals('6f682b85480508142b3d765bbec58ab1', $channel->getSubscriberToken());
Assert::assertEquals('http://localhost/log.php', $channel->getHookUrl());
Assert::assertNull($channel->getTag());
Assert::assertEquals(0, $channel->getListeners());
}

public function testConvertJsonArray(): void
{
$adapter = new ChannelAdapter();
$json = '{"channels":[{"publisher_token":"ce5c546376052619973c1c660e2790f7","subscriber_token":"6f682b85480508142b3d765bbec58ab1","subscriber_message_hook_url":"http://localhost/log.php","listeners":0},{"publisher_token":"aa1690eff31903c4dd7d3cd4c034a3b1","subscriber_token":"b3303f4ea00913882a3fac3c893570af","subscriber_message_hook_url":"http://localhost/log2.php","listeners":0}]}';
$json = '{"channels":[{"publisher_token":"ce5c546376052619973c1c660e2790f7","subscriber_token":"6f682b85480508142b3d765bbec58ab1","subscriber_message_hook_url":"http://localhost/log.php","tag":"firstChannelTag","listeners":0},{"publisher_token":"aa1690eff31903c4dd7d3cd4c034a3b1","subscriber_token":"b3303f4ea00913882a3fac3c893570af","subscriber_message_hook_url":"http://localhost/log2.php","tag":null,"listeners":0}]}';

$channels = $adapter->convertJsonArray($json);

Assert::assertEquals('firstChannelTag', $channels[0]->getTag());

Assert::assertEquals('aa1690eff31903c4dd7d3cd4c034a3b1', $channels[1]->getPublisherToken());
Assert::assertEquals('b3303f4ea00913882a3fac3c893570af', $channels[1]->getSubscriberToken());
Assert::assertEquals('http://localhost/log2.php', $channels[1]->getHookUrl());
Assert::assertNull($channels[1]->getTag());
Assert::assertEquals(0, $channels[1]->getListeners());
}
}
Loading

0 comments on commit 449b6fa

Please sign in to comment.