Skip to content

Commit

Permalink
Add FakeCentrifugo (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
fre5h authored Oct 28, 2020
1 parent 0c1714e commit bbfa1fc
Show file tree
Hide file tree
Showing 58 changed files with 609 additions and 172 deletions.
22 changes: 14 additions & 8 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
risky: true
preset: symfony

tab-width: 4
use-tabs: false
enabled:
- combine_consecutive_unsets
- linebreak_after_opening_tag
- ordered_class_elements
- ordered_imports

- alpha_ordered_imports
- declare_strict_types
- phpdoc_order
disabled:
- no_superfluous_phpdoc_tags_symfony
- native_function_invocation_symfony
finder:
exclude:
- "Tests"
name:
- "*.php"
exclude:
- "Tests"
- "vendor"
name:
- "*.php"
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
language: php

sudo: false

cache:
directories:
- $HOME/.composer/cache/files
Expand Down
8 changes: 4 additions & 4 deletions Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace Fresh\CentrifugoBundle\Command;

use Fresh\CentrifugoBundle\Service\Centrifugo;
use Fresh\CentrifugoBundle\Service\CentrifugoInterface;
use Symfony\Component\Console\Command\Command;

/**
Expand All @@ -22,13 +22,13 @@
*/
abstract class AbstractCommand extends Command
{
/** @var Centrifugo */
/** @var CentrifugoInterface */
protected $centrifugo;

/**
* @param Centrifugo $centrifugo
* @param CentrifugoInterface $centrifugo
*/
public function __construct(Centrifugo $centrifugo)
public function __construct(CentrifugoInterface $centrifugo)
{
$this->centrifugo = $centrifugo;

Expand Down
2 changes: 1 addition & 1 deletion Command/ArgumentDataTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
Expand Down
8 changes: 4 additions & 4 deletions Command/BroadcastCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

namespace Fresh\CentrifugoBundle\Command;

use Fresh\CentrifugoBundle\Service\Centrifugo;
use Fresh\CentrifugoBundle\Service\CentrifugoChecker;
use Fresh\CentrifugoBundle\Service\CentrifugoInterface;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
Expand All @@ -39,10 +39,10 @@ final class BroadcastCommand extends AbstractCommand
private $channels;

/**
* @param Centrifugo $centrifugo
* @param CentrifugoChecker $centrifugoChecker
* @param CentrifugoInterface $centrifugo
* @param CentrifugoChecker $centrifugoChecker
*/
public function __construct(Centrifugo $centrifugo, CentrifugoChecker $centrifugoChecker)
public function __construct(CentrifugoInterface $centrifugo, CentrifugoChecker $centrifugoChecker)
{
$this->centrifugoChecker = $centrifugoChecker;

Expand Down
10 changes: 5 additions & 5 deletions Command/HistoryCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

namespace Fresh\CentrifugoBundle\Command;

use Fresh\CentrifugoBundle\Service\Centrifugo;
use Fresh\CentrifugoBundle\Service\CentrifugoChecker;
use Fresh\CentrifugoBundle\Service\CentrifugoInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -32,10 +32,10 @@ final class HistoryCommand extends AbstractCommand
protected static $defaultName = 'centrifugo:history';

/**
* @param Centrifugo $centrifugo
* @param CentrifugoChecker $centrifugoChecker
* @param CentrifugoInterface $centrifugo
* @param CentrifugoChecker $centrifugoChecker
*/
public function __construct(Centrifugo $centrifugo, CentrifugoChecker $centrifugoChecker)
public function __construct(CentrifugoInterface $centrifugo, CentrifugoChecker $centrifugoChecker)
{
$this->centrifugoChecker = $centrifugoChecker;

Expand Down Expand Up @@ -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('<info>------------</info>');
$io->newLine();
Expand Down
8 changes: 4 additions & 4 deletions Command/HistoryRemoveCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

namespace Fresh\CentrifugoBundle\Command;

use Fresh\CentrifugoBundle\Service\Centrifugo;
use Fresh\CentrifugoBundle\Service\CentrifugoChecker;
use Fresh\CentrifugoBundle\Service\CentrifugoInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -32,10 +32,10 @@ final class HistoryRemoveCommand extends AbstractCommand
protected static $defaultName = 'centrifugo:history-remove';

/**
* @param Centrifugo $centrifugo
* @param CentrifugoChecker $centrifugoChecker
* @param CentrifugoInterface $centrifugo
* @param CentrifugoChecker $centrifugoChecker
*/
public function __construct(Centrifugo $centrifugo, CentrifugoChecker $centrifugoChecker)
public function __construct(CentrifugoInterface $centrifugo, CentrifugoChecker $centrifugoChecker)
{
$this->centrifugoChecker = $centrifugoChecker;

Expand Down
2 changes: 1 addition & 1 deletion Command/InfoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
10 changes: 5 additions & 5 deletions Command/PresenceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

namespace Fresh\CentrifugoBundle\Command;

use Fresh\CentrifugoBundle\Service\Centrifugo;
use Fresh\CentrifugoBundle\Service\CentrifugoChecker;
use Fresh\CentrifugoBundle\Service\CentrifugoInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -32,10 +32,10 @@ final class PresenceCommand extends AbstractCommand
protected static $defaultName = 'centrifugo:presence';

/**
* @param Centrifugo $centrifugo
* @param CentrifugoChecker $centrifugoChecker
* @param CentrifugoInterface $centrifugo
* @param CentrifugoChecker $centrifugoChecker
*/
public function __construct(Centrifugo $centrifugo, CentrifugoChecker $centrifugoChecker)
public function __construct(CentrifugoInterface $centrifugo, CentrifugoChecker $centrifugoChecker)
{
$this->centrifugoChecker = $centrifugoChecker;

Expand Down Expand Up @@ -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),
Expand Down
8 changes: 4 additions & 4 deletions Command/PresenceStatsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

namespace Fresh\CentrifugoBundle\Command;

use Fresh\CentrifugoBundle\Service\Centrifugo;
use Fresh\CentrifugoBundle\Service\CentrifugoChecker;
use Fresh\CentrifugoBundle\Service\CentrifugoInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -32,10 +32,10 @@ final class PresenceStatsCommand extends AbstractCommand
protected static $defaultName = 'centrifugo:presence-stats';

/**
* @param Centrifugo $centrifugo
* @param CentrifugoChecker $centrifugoChecker
* @param CentrifugoInterface $centrifugo
* @param CentrifugoChecker $centrifugoChecker
*/
public function __construct(Centrifugo $centrifugo, CentrifugoChecker $centrifugoChecker)
public function __construct(CentrifugoInterface $centrifugo, CentrifugoChecker $centrifugoChecker)
{
$this->centrifugoChecker = $centrifugoChecker;

Expand Down
10 changes: 5 additions & 5 deletions Command/PublishCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

namespace Fresh\CentrifugoBundle\Command;

use Fresh\CentrifugoBundle\Service\Centrifugo;
use Fresh\CentrifugoBundle\Service\CentrifugoChecker;
use Fresh\CentrifugoBundle\Service\CentrifugoInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -27,16 +27,16 @@
*/
final class PublishCommand extends AbstractCommand
{
use ArgumentDataTrait;
use ArgumentChannelTrait;
use ArgumentDataTrait;

protected static $defaultName = 'centrifugo:publish';

/**
* @param Centrifugo $centrifugo
* @param CentrifugoChecker $centrifugoChecker
* @param CentrifugoInterface $centrifugo
* @param CentrifugoChecker $centrifugoChecker
*/
public function __construct(Centrifugo $centrifugo, CentrifugoChecker $centrifugoChecker)
public function __construct(CentrifugoInterface $centrifugo, CentrifugoChecker $centrifugoChecker)
{
$this->centrifugoChecker = $centrifugoChecker;

Expand Down
8 changes: 4 additions & 4 deletions Command/UnsubscribeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

namespace Fresh\CentrifugoBundle\Command;

use Fresh\CentrifugoBundle\Service\Centrifugo;
use Fresh\CentrifugoBundle\Service\CentrifugoChecker;
use Fresh\CentrifugoBundle\Service\CentrifugoInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -33,10 +33,10 @@ final class UnsubscribeCommand extends AbstractCommand
protected static $defaultName = 'centrifugo:unsubscribe';

/**
* @param Centrifugo $centrifugo
* @param CentrifugoChecker $centrifugoChecker
* @param CentrifugoInterface $centrifugo
* @param CentrifugoChecker $centrifugoChecker
*/
public function __construct(Centrifugo $centrifugo, CentrifugoChecker $centrifugoChecker)
public function __construct(CentrifugoInterface $centrifugo, CentrifugoChecker $centrifugoChecker)
{
$this->centrifugoChecker = $centrifugoChecker;

Expand Down
56 changes: 56 additions & 0 deletions DependencyInjection/Compiler/RegisterCentrifugoPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/*
* This file is part of the FreshCentrifugoBundle.
*
* (c) Artem Henvald <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Fresh\CentrifugoBundle\DependencyInjection\Compiler;

use Fresh\CentrifugoBundle\Logger\CommandHistoryLogger;
use Fresh\CentrifugoBundle\Service\Centrifugo;
use Fresh\CentrifugoBundle\Service\CentrifugoChecker;
use Fresh\CentrifugoBundle\Service\CentrifugoInterface;
use Fresh\CentrifugoBundle\Service\FakeCentrifugo;
use Fresh\CentrifugoBundle\Service\ResponseProcessor;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

/**
* RegisterCentrifugoPass.
*
* @author Artem Henvald <[email protected]>
*/
class RegisterCentrifugoPass implements CompilerPassInterface
{
/**
* @param ContainerBuilder $container
*/
public function process(ContainerBuilder $container): void
{
if (true === $container->getParameter('centrifugo.fake_mode')) {
$definition = new Definition(FakeCentrifugo::class, []);
} else {
$definition = new Definition(
Centrifugo::class,
[
$container->resolveEnvPlaceholders('%env(CENTRIFUGO_API_ENDPOINT)%'),
$container->resolveEnvPlaceholders('%env(CENTRIFUGO_API_KEY)%'),
$container->findDefinition('http_client'),
$container->findDefinition(ResponseProcessor::class),
$container->findDefinition(CommandHistoryLogger::class),
$container->findDefinition(CentrifugoChecker::class),
$container->hasDefinition('profiler') ? $container->getDefinition('profiler') : null,
]
);
}

$container->setDefinition(CentrifugoInterface::class, $definition);
}
}
4 changes: 4 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public function getConfigTreeBuilder(): TreeBuilder
->defaultValue(255)
->info('Maximum length of channel name.')
->end()
->booleanNode('fake_mode')
->defaultFalse()
->info('Enables fake mode for Centrifugo client, no real request will be sent.')
->end()
->end()
;

Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/FreshCentrifugoExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function load(array $configs, ContainerBuilder $container): void
$config = $this->processConfiguration($configuration, $configs);
$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->registerForAutoconfiguration(ChannelAuthenticatorInterface::class)->addTag('centrifugo.channel_authenticator');
}
}
11 changes: 11 additions & 0 deletions FreshCentrifugoBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

namespace Fresh\CentrifugoBundle;

use Fresh\CentrifugoBundle\DependencyInjection\Compiler\RegisterCentrifugoPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
Expand All @@ -21,4 +23,13 @@
*/
class FreshCentrifugoBundle extends Bundle
{
/**
* {@inheritdoc}
*/
public function build(ContainerBuilder $container): void
{
parent::build($container);

$container->addCompilerPass(new RegisterCentrifugoPass());
}
}
2 changes: 1 addition & 1 deletion Model/BatchRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
Loading

0 comments on commit bbfa1fc

Please sign in to comment.