Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
fre5h committed Nov 14, 2023
1 parent cec4a76 commit 6f6d0e2
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 27 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ jobs:
working-directory: './'

- name: 'Run PHPUnit'
run: ./vendor/bin/phpunit -v -c phpunit.xml.dist
run: ./vendor/bin/phpunit -c phpunit.xml.dist

code-coverage:
needs: test
Expand All @@ -151,7 +151,7 @@ jobs:
working-directory: './'

- name: 'Run PHPUnit with Code Coverage'
run: ./vendor/bin/phpunit -v -c phpunit.xml.dist --coverage-clover=coverage.xml
run: ./vendor/bin/phpunit -c phpunit.xml.dist --coverage-clover=coverage.xml

- name: 'Download Coverage Files'
uses: actions/download-artifact@v2
Expand Down
10 changes: 4 additions & 6 deletions Model/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,14 @@ public function getChannels(): iterable
}

/**
* @return array<string, mixed>
* @return array|\stdClass
*/
public function jsonSerialize(): array
public function jsonSerialize(): array|\stdClass
{
if (!empty($this->params)) {
$data = $this->params;
} else {
$data = new \stdClass(); // To have {} in json, restrictions of Centrifugo
return $this->params;
}

return $data;
return new \stdClass();
}
}
10 changes: 9 additions & 1 deletion Model/BatchRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ public function __construct(array $commands = [])
}
}

/**
* @return Method
*/
public function getMethod(): Method
{
return Method::BATCH;
}

/**
* @param CommandInterface $command
*/
Expand Down Expand Up @@ -88,7 +96,7 @@ public function prepareLineDelimitedJson(): string
$serializedCommands = [];

foreach ($this->getCommands() as $command) {
$serializedCommands[] = \json_encode($command, \JSON_THROW_ON_ERROR);
$serializedCommands['commands'] = \json_encode($command, \JSON_THROW_ON_ERROR);
}

if (!empty($serializedCommands)) {
Expand Down
1 change: 1 addition & 0 deletions Model/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ enum Method: string
case HISTORY_REMOVE = 'history_remove';
case CHANNELS = 'channels';
case INFO = 'info';
case BATCH = 'batch';
}
1 change: 1 addition & 0 deletions Resources/docs/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ class AdminsChannelAuthenticator implements ChannelAuthenticatorInterface

```php
<?php
declare(strict_types=1);

namespace App\Controller;

Expand Down
22 changes: 11 additions & 11 deletions Resources/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ But you can customize bundle configuration for your own needs.
# config/packages/fresh_centrifugo.yaml

fresh_centrifugo:
# Default value is 255, if you changed it in Centrifugo configuration,
# then you can change it in bundle configuration, it effects only on validation
channel_max_length: 255
jwt:
# You can set default TTL for all JWT tokens, if it not set, then default value is NULL
# Default NULL value means that tokens will not be expired
ttl: 86400
fake_mode: true # Enables fake mode for Centrifugo client, no real request will be sent.
api_key: '%env(CENTRIFUGO_API_KEY)%' # You can change API key here
api_endpoint: '%env(CENTRIFUGO_API_ENDPOINT)%' # You can change API endpoint here
secret: '%env(CENTRIFUGO_SECRET)%' # You can change secret here
# Default value is 255, if you changed it in Centrifugo configuration,
# then you can change it in bundle configuration, it effects only on validation
channel_max_length: 255
jwt:
# You can set default TTL for all JWT tokens, if it not set, then default value is NULL
# Default NULL value means that tokens will not be expired
ttl: 86400
fake_mode: true # Enables fake mode for Centrifugo client, no real request will be sent.
api_key: '%env(CENTRIFUGO_API_KEY)%' # You can change API key here
api_endpoint: '%env(CENTRIFUGO_API_ENDPOINT)%' # You can change API endpoint here
secret: '%env(CENTRIFUGO_SECRET)%' # You can change secret here
```
## More features
Expand Down
14 changes: 7 additions & 7 deletions Service/Centrifugo.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function broadcast(array $data, array $channels, bool $skipHistory = fals
/**
* {@inheritdoc}
*/
public function subscribe(string $user, string $channel, array $info = [], ?string $base64Info = null, ?string $client = null, ?string $session = null, array $data = [], ?string $base64Data = null, ?StreamPosition $recoverSince = null, ?Override $override = null): void
public function subscribe(string $user, string $channel, array $info = [], string $base64Info = null, string $client = null, string $session = null, array $data = [], string $base64Data = null, StreamPosition $recoverSince = null, Override $override = null): void
{
$this->doSendCommand(new Model\SubscribeCommand($user, $channel, $info, $base64Info, $client, $session, $data, $base64Data, $recoverSince, $override));
}
Expand All @@ -80,15 +80,15 @@ public function unsubscribe(string $user, string $channel, string $client = '',
/**
* {@inheritdoc}
*/
public function disconnect(string $user, array $whitelist = [], ?string $client = null, ?string $session = null, ?Disconnect $disconnectObject = null): void
public function disconnect(string $user, array $whitelist = [], string $client = null, string $session = null, Disconnect $disconnectObject = null): void
{
$this->doSendCommand(new Model\DisconnectCommand($user, $whitelist, $client, $session, $disconnectObject));
}

/**
* {@inheritdoc}
*/
public function refresh(string $user, ?string $client = null, ?string $session = null, ?bool $expired = null, ?int $expireAt = null): void
public function refresh(string $user, string $client = null, string $session = null, bool $expired = null, int $expireAt = null): void
{
$this->doSendCommand(new Model\RefreshCommand($user, $client, $session, $expired, $expireAt));
}
Expand All @@ -112,7 +112,7 @@ public function presenceStats(string $channel): array
/**
* {@inheritdoc}
*/
public function history(string $channel, bool $reverse = false, ?int $limit = null, ?StreamPosition $streamPosition = null): array
public function history(string $channel, bool $reverse = false, int $limit = null, StreamPosition $streamPosition = null): array
{
return (array) $this->doSendCommand(new Model\HistoryCommand($channel, $reverse, $limit, $streamPosition));
}
Expand All @@ -128,7 +128,7 @@ public function historyRemove(string $channel): void
/**
* {@inheritdoc}
*/
public function channels(?string $pattern = null): array
public function channels(string $pattern = null): array
{
return (array) $this->doSendCommand(new Model\ChannelsCommand($pattern));
}
Expand Down Expand Up @@ -163,7 +163,7 @@ private function doSendCommand(CommandInterface $command): ?array
if ($command instanceof Model\BatchRequest) {
$json = $command->prepareLineDelimitedJson();
} else {
$json = \json_encode($command, \JSON_THROW_ON_ERROR);
$json = json_encode($command, \JSON_THROW_ON_ERROR);
}

if ($this->profilerEnabled) {
Expand All @@ -172,7 +172,7 @@ private function doSendCommand(CommandInterface $command): ?array

$response = $this->httpClient->request(
Request::METHOD_POST,
\sprintf('%s/%s', $this->endpoint, $command->getMethod()),
sprintf('%s/%s', $this->endpoint, $command->getMethod()->value),
[
'headers' => [
'X-API-Key' => $this->apiKey,
Expand Down

0 comments on commit 6f6d0e2

Please sign in to comment.