diff --git a/Command/ArgumentDataTrait.php b/Command/ArgumentDataTrait.php index e5ad435..f293534 100644 --- a/Command/ArgumentDataTrait.php +++ b/Command/ArgumentDataTrait.php @@ -39,7 +39,7 @@ protected function initializeDataArgument(InputInterface $input): void } try { - $this->data = \json_decode($data, true, 512, JSON_THROW_ON_ERROR); + $this->data = \json_decode($data, true, 512, \JSON_THROW_ON_ERROR); } catch (\JsonException $e) { throw new InvalidArgumentException('Argument "data" is not a valid JSON.'); } diff --git a/Command/HistoryCommand.php b/Command/HistoryCommand.php index 2a3dc19..104e4d4 100644 --- a/Command/HistoryCommand.php +++ b/Command/HistoryCommand.php @@ -90,7 +90,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $io->title('Publications'); foreach ($data['publications'] as $info) { - $io->writeln(\json_encode($info['data'], JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR)); + $io->writeln(\json_encode($info['data'], \JSON_PRETTY_PRINT | \JSON_THROW_ON_ERROR)); $io->newLine(); $io->writeln('------------'); $io->newLine(); diff --git a/Command/InfoCommand.php b/Command/InfoCommand.php index e5a7a4d..55bfbe9 100644 --- a/Command/InfoCommand.php +++ b/Command/InfoCommand.php @@ -88,7 +88,7 @@ private function writeParameter(SymfonyStyle $io, string $key, $value, int $padd if ($padding > 0) { $formattedKey = $last ? '└ ' : '├ '; $formattedKey .= $key; - $formattedKey = \str_pad($formattedKey, \strlen($formattedKey) + $padding, ' ', STR_PAD_LEFT); + $formattedKey = \str_pad($formattedKey, \strlen($formattedKey) + $padding, ' ', \STR_PAD_LEFT); } if (!\is_array($value)) { diff --git a/Command/PresenceCommand.php b/Command/PresenceCommand.php index 6603c92..ff8fb21 100644 --- a/Command/PresenceCommand.php +++ b/Command/PresenceCommand.php @@ -119,7 +119,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int */ private function formatConnInfo(array $connInfo): string { - $json = \json_encode($connInfo, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR); + $json = \json_encode($connInfo, \JSON_PRETTY_PRINT | \JSON_THROW_ON_ERROR); return array_reduce( \explode("\n", $json), diff --git a/DependencyInjection/Compiler/RegisterCentrifugoPass.php b/DependencyInjection/Compiler/RegisterCentrifugoPass.php index 876c0cc..0bc8de3 100644 --- a/DependencyInjection/Compiler/RegisterCentrifugoPass.php +++ b/DependencyInjection/Compiler/RegisterCentrifugoPass.php @@ -40,8 +40,8 @@ public function process(ContainerBuilder $container): void $definition = new Definition( Centrifugo::class, [ - $container->resolveEnvPlaceholders('%env(CENTRIFUGO_API_ENDPOINT)%'), - $container->resolveEnvPlaceholders('%env(CENTRIFUGO_API_KEY)%'), + $container->getParameter('centrifugo.api_endpoint'), + $container->getParameter('centrifugo.api_key'), $container->findDefinition('http_client'), $container->findDefinition(ResponseProcessor::class), $container->findDefinition(CommandHistoryLogger::class), diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 3d08d22..932edc1 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -54,6 +54,18 @@ public function getConfigTreeBuilder(): TreeBuilder ->defaultFalse() ->info('Enables fake mode for Centrifugo client, no real request will be sent.') ->end() + ->scalarNode('api_key') + ->defaultValue('%env(CENTRIFUGO_API_KEY)%') + ->info('Centrifugo API key') + ->end() + ->scalarNode('api_endpoint') + ->defaultValue('%env(CENTRIFUGO_API_ENDPOINT)%') + ->info('Centrifugo API endpoint') + ->end() + ->scalarNode('secret') + ->defaultValue('%env(CENTRIFUGO_SECRET)%') + ->info('Centrifugo secret') + ->end() ->end() ; diff --git a/DependencyInjection/FreshCentrifugoExtension.php b/DependencyInjection/FreshCentrifugoExtension.php index e3ed6d2..f4325d6 100644 --- a/DependencyInjection/FreshCentrifugoExtension.php +++ b/DependencyInjection/FreshCentrifugoExtension.php @@ -38,6 +38,9 @@ public function load(array $configs, ContainerBuilder $container): void $container->setParameter('centrifugo.channel_max_length', (int) $config['channel_max_length']); $container->setParameter('centrifugo.jwt.ttl', $config['jwt']['ttl']); $container->setParameter('centrifugo.fake_mode', $config['fake_mode']); + $container->setParameter('centrifugo.api_key', $config['api_key']); + $container->setParameter('centrifugo.api_endpoint', $config['api_endpoint']); + $container->setParameter('centrifugo.secret', $config['secret']); $container->registerForAutoconfiguration(ChannelAuthenticatorInterface::class)->addTag('centrifugo.channel_authenticator'); } } diff --git a/Model/BatchRequest.php b/Model/BatchRequest.php index f97c531..7396074 100644 --- a/Model/BatchRequest.php +++ b/Model/BatchRequest.php @@ -88,7 +88,7 @@ public function prepareLineDelimitedJson(): string $serializedCommands = []; foreach ($this->getCommands() as $command) { - $serializedCommands[] = \json_encode($command, JSON_THROW_ON_ERROR); + $serializedCommands[] = \json_encode($command, \JSON_THROW_ON_ERROR); } if (!empty($serializedCommands)) { diff --git a/README.md b/README.md index 04e6186..8155935 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ return [ ###> fresh/centrifugo-bundle ### CENTRIFUGO_API_KEY=secret-api-key -CENTRIFUGO_API_ENDPOINT=http://centrifugo:8000/api +CENTRIFUGO_API_ENDPOINT=http://localhost:8000/api CENTRIFUGO_SECRET=secret ###< fresh/centrifugo-bundle ### ``` diff --git a/Resources/config/services.yaml b/Resources/config/services.yaml index 8959507..42017d6 100644 --- a/Resources/config/services.yaml +++ b/Resources/config/services.yaml @@ -6,7 +6,7 @@ services: bind: $centrifugoChannelMaxLength: '%centrifugo.channel_max_length%' $centrifugoJwtTtl: '%centrifugo.jwt.ttl%' - $centrifugoSecret: '%env(CENTRIFUGO_SECRET)%' + $centrifugoSecret: '%centrifugo.secret%' iterable $channelAuthenticators: !tagged_iterator 'centrifugo.channel_authenticator' Fresh\DateTime\DateTimeHelper: diff --git a/Resources/docs/configuration.md b/Resources/docs/configuration.md index 19a6159..3314225 100644 --- a/Resources/docs/configuration.md +++ b/Resources/docs/configuration.md @@ -17,6 +17,9 @@ fresh_centrifugo: # 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 diff --git a/Service/Centrifugo.php b/Service/Centrifugo.php index f47a334..39915c8 100644 --- a/Service/Centrifugo.php +++ b/Service/Centrifugo.php @@ -169,7 +169,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) { diff --git a/Service/ChannelAuthenticator/PrivateChannelAuthenticator.php b/Service/ChannelAuthenticator/PrivateChannelAuthenticator.php index 73db96b..1cf7d8f 100644 --- a/Service/ChannelAuthenticator/PrivateChannelAuthenticator.php +++ b/Service/ChannelAuthenticator/PrivateChannelAuthenticator.php @@ -109,7 +109,7 @@ private function findAppropriateChannelAuthenticator(string $channel): ?ChannelA private function processRequest(Request $request): array { try { - $content = \json_decode((string) $request->getContent(), true, 512, JSON_THROW_ON_ERROR); + $content = \json_decode((string) $request->getContent(), true, 512, \JSON_THROW_ON_ERROR); } catch (\JsonException $e) { throw new BadRequestHttpException('Invalid JSON.'); } catch (\Exception $e) { diff --git a/Service/Jwt/JwtGenerator.php b/Service/Jwt/JwtGenerator.php index c5d5193..86e9415 100644 --- a/Service/Jwt/JwtGenerator.php +++ b/Service/Jwt/JwtGenerator.php @@ -98,7 +98,7 @@ private function buildSignaturePart(string $headerPartDecoded, string $payloadPa */ private function convertArrayToJsonString(array $array): string { - return \json_encode($array, JSON_THROW_ON_ERROR); + return \json_encode($array, \JSON_THROW_ON_ERROR); } /** diff --git a/Service/ResponseProcessor.php b/Service/ResponseProcessor.php index 3fbc6ea..d03bc12 100644 --- a/Service/ResponseProcessor.php +++ b/Service/ResponseProcessor.php @@ -107,7 +107,7 @@ public function processResponse(CommandInterface $command, ResponseInterface $re private function decodeAndProcessResponseResult(CommandInterface $command, string $content): ?array { try { - $data = \json_decode($content, true, 512, JSON_THROW_ON_ERROR); + $data = \json_decode($content, true, 512, \JSON_THROW_ON_ERROR); } catch (\Exception $e) { throw new CentrifugoException('Centrifugo response payload is not a valid JSON'); } diff --git a/Tests/DependencyInjection/Compiler/RegisterCentrifugoPassTest.php b/Tests/DependencyInjection/Compiler/RegisterCentrifugoPassTest.php index e5680c1..4c07665 100644 --- a/Tests/DependencyInjection/Compiler/RegisterCentrifugoPassTest.php +++ b/Tests/DependencyInjection/Compiler/RegisterCentrifugoPassTest.php @@ -53,6 +53,7 @@ public function testProcessFakeCentrifugo(): void $this->containerBuilder ->expects(self::once()) ->method('getParameter') + ->with('centrifugo.fake_mode') ->willReturn(true) ; @@ -70,9 +71,18 @@ public function testProcessFakeCentrifugo(): void public function testProcessCentrifugo(): void { $this->containerBuilder - ->expects(self::once()) + ->expects(self::exactly(3)) ->method('getParameter') - ->willReturn(false) + ->withConsecutive( + ['centrifugo.fake_mode'], + ['centrifugo.api_endpoint'], + ['centrifugo.api_key'], + ) + ->willReturnOnConsecutiveCalls( + false, + '%env(CENTRIFUGO_API_ENDPOINT)%', + '%env(CENTRIFUGO_API_KEY)%' + ) ; $this->containerBuilder diff --git a/Tests/DependencyInjection/FreshCentrifugoExtensionTest.php b/Tests/DependencyInjection/FreshCentrifugoExtensionTest.php index 9a1a37d..6c15ac4 100644 --- a/Tests/DependencyInjection/FreshCentrifugoExtensionTest.php +++ b/Tests/DependencyInjection/FreshCentrifugoExtensionTest.php @@ -65,6 +65,9 @@ public function testLoadExtension(): void self::assertTrue($this->container->hasParameter('centrifugo.jwt.ttl')); self::assertNull($this->container->getParameter('centrifugo.jwt.ttl')); self::assertFalse($this->container->getParameter('centrifugo.fake_mode')); + self::assertNotEmpty($this->container->getParameter('centrifugo.api_key')); + self::assertNotEmpty($this->container->getParameter('centrifugo.api_endpoint')); + self::assertNotEmpty($this->container->getParameter('centrifugo.secret')); $childDefinitions = $this->container->getAutoconfiguredInstanceof(); foreach ($childDefinitions as $childDefinition) {