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

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 1c11eba commit 4a8f72e
Showing 24 changed files with 42 additions and 79 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -86,15 +86,11 @@ jobs:
fail-fast: false
matrix:
php-version:
- '8.1'
- '8.2'
symfony-version:
- '5.4'
- '6.0'
- '6.1'
- '6.2'
- '6.3'
- '6.4'
- '7.0'
steps:
- name: 'Checkout Code'
uses: actions/checkout@v2
2 changes: 1 addition & 1 deletion .styleci.yml
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ risky: true
preset: symfony
tab-width: 4
use-tabs: false
version: 8.1
version: 8.2
enabled:
- combine_consecutive_unsets
- ordered_class_elements
6 changes: 1 addition & 5 deletions Command/BroadcastCommand.php
Original file line number Diff line number Diff line change
@@ -56,11 +56,7 @@ public function __construct(CentrifugoInterface $centrifugo, protected readonly
*/
protected function configure(): void
{
if (Kernel::MAJOR_VERSION >= 6) { // @phpstan-ignore-line
$channelsArgument = new InputArgument('channels', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'List of channels to publish data to', null, $this->getChannelsForAutocompletion());
} else { // @phpstan-ignore-line
$channelsArgument = new InputArgument('channels', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'List of channels to publish data to');
}
$channelsArgument = new InputArgument('channels', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'List of channels to publish data to', null, $this->getChannelsForAutocompletion());

$this
->setDefinition(
6 changes: 1 addition & 5 deletions Command/ChannelsCommand.php
Original file line number Diff line number Diff line change
@@ -36,11 +36,7 @@ final class ChannelsCommand extends AbstractCommand
*/
protected function configure(): void
{
if (Kernel::MAJOR_VERSION >= 6) { // @phpstan-ignore-line
$patternArgument = new InputArgument('pattern', InputArgument::OPTIONAL, 'Pattern to filter channels', null, $this->getChannelsForAutocompletion());
} else { // @phpstan-ignore-line
$patternArgument = new InputArgument('pattern', InputArgument::OPTIONAL, 'Pattern to filter channels');
}
$patternArgument = new InputArgument('pattern', InputArgument::OPTIONAL, 'Pattern to filter channels', null, $this->getChannelsForAutocompletion());

$this
->setDefinition(
6 changes: 1 addition & 5 deletions Command/HistoryCommand.php
Original file line number Diff line number Diff line change
@@ -57,11 +57,7 @@ public function __construct(CentrifugoInterface $centrifugo, protected readonly
*/
protected function configure(): void
{
if (Kernel::MAJOR_VERSION >= 6) { // @phpstan-ignore-line
$channelArgument = new InputArgument('channel', InputArgument::REQUIRED, 'Channel name', null, $this->getChannelsForAutocompletion());
} else { // @phpstan-ignore-line
$channelArgument = new InputArgument('channel', InputArgument::REQUIRED, 'Channel name');
}
$channelArgument = new InputArgument('channel', InputArgument::REQUIRED, 'Channel name', null, $this->getChannelsForAutocompletion());

$this
->setDefinition(
6 changes: 1 addition & 5 deletions Command/HistoryRemoveCommand.php
Original file line number Diff line number Diff line change
@@ -47,11 +47,7 @@ public function __construct(CentrifugoInterface $centrifugo, protected readonly
*/
protected function configure(): void
{
if (Kernel::MAJOR_VERSION >= 6) { // @phpstan-ignore-line
$channelArgument = new InputArgument('channel', InputArgument::REQUIRED, 'Name of channel to remove history', null, $this->getChannelsForAutocompletion());
} else { // @phpstan-ignore-line
$channelArgument = new InputArgument('channel', InputArgument::REQUIRED, 'Name of channel to remove history');
}
$channelArgument = new InputArgument('channel', InputArgument::REQUIRED, 'Name of channel to remove history', null, $this->getChannelsForAutocompletion());

$this
->setDefinition(
6 changes: 1 addition & 5 deletions Command/PresenceCommand.php
Original file line number Diff line number Diff line change
@@ -47,11 +47,7 @@ public function __construct(CentrifugoInterface $centrifugo, protected readonly
*/
protected function configure(): void
{
if (Kernel::MAJOR_VERSION >= 6) { // @phpstan-ignore-line
$channelArgument = new InputArgument('channel', InputArgument::REQUIRED, 'Name of channel to call presence from', null, $this->getChannelsForAutocompletion());
} else { // @phpstan-ignore-line
$channelArgument = new InputArgument('channel', InputArgument::REQUIRED, 'Name of channel to call presence from');
}
$channelArgument = new InputArgument('channel', InputArgument::REQUIRED, 'Name of channel to call presence from', null, $this->getChannelsForAutocompletion());

$this
->setDefinition(
6 changes: 1 addition & 5 deletions Command/PresenceStatsCommand.php
Original file line number Diff line number Diff line change
@@ -47,11 +47,7 @@ public function __construct(CentrifugoInterface $centrifugo, protected readonly
*/
protected function configure(): void
{
if (Kernel::MAJOR_VERSION >= 6) { // @phpstan-ignore-line
$channelArgument = new InputArgument('channel', InputArgument::REQUIRED, 'Name of channel to call presence from', null, $this->getChannelsForAutocompletion());
} else { // @phpstan-ignore-line
$channelArgument = new InputArgument('channel', InputArgument::REQUIRED, 'Name of channel to call presence from');
}
$channelArgument = new InputArgument('channel', InputArgument::REQUIRED, 'Name of channel to call presence from', null, $this->getChannelsForAutocompletion());

$this
->setDefinition(
6 changes: 1 addition & 5 deletions Command/PublishCommand.php
Original file line number Diff line number Diff line change
@@ -56,11 +56,7 @@ public function __construct(CentrifugoInterface $centrifugo, protected readonly
*/
protected function configure(): void
{
if (Kernel::MAJOR_VERSION >= 6) { // @phpstan-ignore-line
$channelArgument = new InputArgument('channel', InputArgument::REQUIRED, 'Name of channel to publish', null, $this->getChannelsForAutocompletion());
} else { // @phpstan-ignore-line
$channelArgument = new InputArgument('channel', InputArgument::REQUIRED, 'Name of channel to publish');
}
$channelArgument = new InputArgument('channel', InputArgument::REQUIRED, 'Name of channel to publish', null, $this->getChannelsForAutocompletion());

$this
->setDefinition(
6 changes: 1 addition & 5 deletions Command/SubscribeCommand.php
Original file line number Diff line number Diff line change
@@ -67,11 +67,7 @@ public function __construct(CentrifugoInterface $centrifugo, protected readonly
*/
protected function configure(): void
{
if (Kernel::MAJOR_VERSION >= 6) { // @phpstan-ignore-line
$channelArgument = new InputArgument('channel', InputArgument::REQUIRED, 'Name of channel to subscribe user to', null, $this->getChannelsForAutocompletion());
} else { // @phpstan-ignore-line
$channelArgument = new InputArgument('channel', InputArgument::REQUIRED, 'Name of channel to subscribe user to');
}
$channelArgument = new InputArgument('channel', InputArgument::REQUIRED, 'Name of channel to subscribe user to', null, $this->getChannelsForAutocompletion());

$this
->setDefinition(
6 changes: 1 addition & 5 deletions Command/UnsubscribeCommand.php
Original file line number Diff line number Diff line change
@@ -54,11 +54,7 @@ public function __construct(CentrifugoInterface $centrifugo, protected readonly
*/
protected function configure(): void
{
if (Kernel::MAJOR_VERSION >= 6) { // @phpstan-ignore-line
$channelArgument = new InputArgument('channel', InputArgument::REQUIRED, 'Name of channel to unsubscribe user to', null, $this->getChannelsForAutocompletion());
} else { // @phpstan-ignore-line
$channelArgument = new InputArgument('channel', InputArgument::REQUIRED, 'Name of channel to unsubscribe user to');
}
$channelArgument = new InputArgument('channel', InputArgument::REQUIRED, 'Name of channel to unsubscribe user to', null, $this->getChannelsForAutocompletion());

$this
->setDefinition(
9 changes: 2 additions & 7 deletions Model/AbstractCommand.php
Original file line number Diff line number Diff line change
@@ -12,8 +12,6 @@

namespace Fresh\CentrifugoBundle\Model;

use JetBrains\PhpStorm\ArrayShape;

/**
* AbstractCommand.
*
@@ -56,15 +54,12 @@ public function getChannels(): iterable
/**
* @return array<string, mixed>
*/
#[ArrayShape(['method' => 'string', 'params' => 'array'])]
public function jsonSerialize(): array
{
$data = ['method' => $this->method->value];

if (!empty($this->params)) {
$data['params'] = $this->params;
$data = $this->params;
} else {
$data['params'] = new \stdClass(); // To have {} in json, restrictions of Centrifugo
$data = new \stdClass(); // To have {} in json, restrictions of Centrifugo
}

return $data;
2 changes: 1 addition & 1 deletion Model/ChannelsCommand.php
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ final class ChannelsCommand extends AbstractCommand implements ResultableCommand
/**
* @param string|null $pattern
*/
public function __construct(?string $pattern = null)
public function __construct(string $pattern = null)
{
$params = [];

5 changes: 5 additions & 0 deletions Model/CommandInterface.php
Original file line number Diff line number Diff line change
@@ -23,4 +23,9 @@ interface CommandInterface
* @return string[]
*/
public function getChannels(): iterable;

/**
* @return Method
*/
public function getMethod(): Method;
}
2 changes: 1 addition & 1 deletion Model/DisconnectCommand.php
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@ final class DisconnectCommand extends AbstractCommand
* @param string|null $session
* @param Disconnect|null $disconnectObject
*/
public function __construct(string $user, array $clientIdWhitelist = [], ?string $client = null, ?string $session = null, ?Disconnect $disconnectObject = null)
public function __construct(string $user, array $clientIdWhitelist = [], string $client = null, string $session = null, Disconnect $disconnectObject = null)
{
$params = [
'user' => $user,
2 changes: 1 addition & 1 deletion Model/HistoryCommand.php
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ final class HistoryCommand extends AbstractCommand implements ResultableCommandI
* @param int|null $limit
* @param StreamPosition|null $streamPosition
*/
public function __construct(protected readonly string $channel, bool $reverse = false, ?int $limit = null, ?StreamPosition $streamPosition = null)
public function __construct(protected readonly string $channel, bool $reverse = false, int $limit = null, StreamPosition $streamPosition = null)
{
$params = [
'channel' => $channel,
2 changes: 1 addition & 1 deletion Model/PublishCommand.php
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ final class PublishCommand extends AbstractCommand
* @param array<string, mixed> $tags
* @param string|null $base64data
*/
public function __construct(array $data, protected readonly string $channel, bool $skipHistory = false, array $tags = [], ?string $base64data = null)
public function __construct(array $data, protected readonly string $channel, bool $skipHistory = false, array $tags = [], string $base64data = null)
{
$params = [
'channel' => $channel,
2 changes: 1 addition & 1 deletion Model/RefreshCommand.php
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@ final class RefreshCommand extends AbstractCommand
* @param bool|null $expired
* @param int|null $expireAt
*/
public function __construct(string $user, ?string $client = null, ?string $session = null, ?bool $expired = null, ?int $expireAt = null)
public function __construct(string $user, string $client = null, string $session = null, bool $expired = null, int $expireAt = null)
{
$params = ['user' => $user];

2 changes: 1 addition & 1 deletion Model/SubscribeCommand.php
Original file line number Diff line number Diff line change
@@ -33,7 +33,7 @@ final class SubscribeCommand extends AbstractCommand
* @param StreamPosition|null $recoverSince
* @param Override|null $override
*/
public function __construct(string $user, protected readonly string $channel, array $info = [], ?string $base64Info = null, ?string $client = null, ?string $session = null, array $data = [], ?string $base64Data = null, ?StreamPosition $recoverSince = null, ?Override $override = null)
public function __construct(string $user, protected readonly string $channel, array $info = [], string $base64Info = null, string $client = null, string $session = null, array $data = [], string $base64Data = null, StreamPosition $recoverSince = null, Override $override = null)
{
$params = [
'user' => $user,
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@

## Features 🎁

- [x] Compatible with [Centrifugo 4.1.5](https://github.com/centrifugal/centrifugo/releases/tag/v4.1.5) 🚀
- [x] Compatible with latest [Centrifugo 5.1.1](https://github.com/centrifugal/centrifugo/releases/tag/v5.1.1) 🚀
- [x] Wrapper over [Centrifugo HTTP API](https://centrifugal.dev/docs/server/server_api#http-api) 🔌
- [X] Authentication with JWT token (HMAC algorithm) for [anonymous](./Resources/docs/authentication.md#anonymous), [authenticated user](./Resources/docs/authentication.md#authenticated-user) and [private channel](./Resources/docs/authentication.md#private-channel) 🗝️
- [x] [Batch request](./Resources/docs/centrifugo_service_methods.md#batch-request) in [JSON streaming format](https://en.wikipedia.org/wiki/JSON_streaming) 💪
@@ -22,8 +22,8 @@

## Requirements 🧐

* PHP 8.1
* Symfony 5.4, 6.0, 6.1, 6.2, 6.3, 6.4
* PHP 8.2
* Symfony 6.3, 6.4, 7.0

## Installation 🌱

4 changes: 2 additions & 2 deletions Service/Centrifugo.php
Original file line number Diff line number Diff line change
@@ -172,10 +172,10 @@ private function doSendCommand(CommandInterface $command): ?array

$response = $this->httpClient->request(
Request::METHOD_POST,
$this->endpoint,
\sprintf('%s/%s', $this->endpoint, $command->getMethod()),
[
'headers' => [
'Authorization' => 'apikey '.$this->apiKey,
'X-API-Key' => $this->apiKey,
'Content-Type' => 'application/json',
],
'body' => $json,
6 changes: 3 additions & 3 deletions Service/Credentials/CredentialsGenerator.php
Original file line number Diff line number Diff line change
@@ -41,7 +41,7 @@ public function __construct(private readonly JwtGenerator $jwtGenerator, private
*
* @return string
*/
public function generateJwtTokenForUser(CentrifugoUserInterface $user, ?string $base64info = null, array $channels = []): string
public function generateJwtTokenForUser(CentrifugoUserInterface $user, string $base64info = null, array $channels = []): string
{
$jwtPayload = new JwtPayload(
$user->getCentrifugoSubject(),
@@ -60,7 +60,7 @@ public function generateJwtTokenForUser(CentrifugoUserInterface $user, ?string $
*
* @return string
*/
public function generateJwtTokenForAnonymous(?string $base64info = null, array $channels = []): string
public function generateJwtTokenForAnonymous(string $base64info = null, array $channels = []): string
{
$jwtPayload = new JwtPayload(
'',
@@ -81,7 +81,7 @@ public function generateJwtTokenForAnonymous(?string $base64info = null, array $
*
* @return string
*/
public function generateJwtTokenForPrivateChannel(string $client, string $channel, ?string $base64info = null, ?bool $eto = null): string
public function generateJwtTokenForPrivateChannel(string $client, string $channel, string $base64info = null, bool $eto = null): string
{
$jwtPayload = new JwtPayloadForPrivateChannel(
$client,
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -41,7 +41,7 @@
"phpstan/phpstan-deprecation-rules": "^1.1",
"phpstan/phpstan-phpunit": "^1.3",
"phpstan/phpstan-symfony": "^1.3",
"phpunit/phpunit": "^9.5",
"phpunit/phpunit": "^10.4",
"slam/phpstan-extensions": "^6.0"
},
"autoload": {
15 changes: 9 additions & 6 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd"
colors="true"
bootstrap="vendor/autoload.php"
>
@@ -15,14 +15,17 @@
</testsuite>
</testsuites>

<coverage includeUncoveredFiles="false">
<source>
<include>
<directory suffix=".php">./</directory>
<directory>./</directory>
</include>
<exclude>
<directory suffix=".php">./Resources</directory>
<directory suffix=".php">./Tests</directory>
<directory suffix=".php">./vendor</directory>
<directory>./Resources</directory>
<directory>./Tests</directory>
<directory>./vendor</directory>
</exclude>
</source>

<coverage includeUncoveredFiles="false">
</coverage>
</phpunit>

0 comments on commit 4a8f72e

Please sign in to comment.