Skip to content

Commit

Permalink
safe refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ilario-pierbattista committed Nov 30, 2020
1 parent 61b24cc commit 1ef451d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 75 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
},
"scripts": {
"cs-check": "php-cs-fixer fix --dry-run --diff --config=.php-cs.php",
"cs-fix": "php-cs-fixer fix --diff --config=.php-cs.php"
"cs-fix": "php-cs-fixer fix --diff --config=.php-cs.php",
"test": "bin/phpunit tests"
}
}
39 changes: 13 additions & 26 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,35 @@
*/
final class Configuration implements ConfigurationInterface
{
const READ_PREFERENCE_VALID_OPTIONS = ['primary', 'primaryPreferred', 'secondary', 'secondaryPreferred', 'nearest'];
private const READ_PREFERENCE_VALID_OPTIONS = ['primary', 'primaryPreferred', 'secondary', 'secondaryPreferred', 'nearest'];

/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('mongo_db_bundle');
$rootBuilder = \method_exists(TreeBuilder::class, 'getRootNode')
? $treeBuilder->getRootNode()
: $treeBuilder->root('mongo_db_bundle');

$this->addDataCollection($rootBuilder->children());
$this->addClients($rootBuilder->children());
$this->addConnections($rootBuilder->children());
$this->addDriversOptionFactory($rootBuilder->children());
self::addDataCollection($rootBuilder->children());
self::addClients($rootBuilder->children());
self::addConnections($rootBuilder->children());
self::addDriversOption($rootBuilder->children());

return $treeBuilder;
}

private function addDataCollection(NodeBuilder $builder)
private static function addDataCollection(NodeBuilder $builder): void
{
$builder
->booleanNode('data_collection')
->defaultTrue()
->info('Disables Data Collection if needed');
}

private function addClients(NodeBuilder $builder)
private static function addClients(NodeBuilder $builder): void
{
$clientsBuilder = $builder
->arrayNode('clients')
Expand All @@ -51,7 +51,7 @@ private function addClients(NodeBuilder $builder)
->prototype('array')
->children();

$this->addClientsHosts($clientsBuilder);
self::addClientsHosts($clientsBuilder);

$clientsBuilder
->scalarNode('uri')
Expand Down Expand Up @@ -91,7 +91,7 @@ private function addClients(NodeBuilder $builder)
->defaultNull();
}

private function addClientsHosts(NodeBuilder $builder)
private static function addClientsHosts(NodeBuilder $builder): void
{
$hostsBuilder = $builder
->arrayNode('hosts')
Expand All @@ -108,26 +108,13 @@ private function addClientsHosts(NodeBuilder $builder)
->defaultValue(27017);
}

private function addDriverOptions(NodeBuilder $builder)
private static function addDriversOption(NodeBuilder $builder): void
{
$driverOptionsBuilder = $builder
->arrayNode('driverOptions')
->info('Available options for driver')
->children();

$driverOptionsBuilder
->variableNode('context')
->defaultValue([])
->info('Service id name');
}

private function addDriversOptionFactory(NodeBuilder $builder)
{
$connectionBuilder = $builder
$builder
->scalarNode('driverOptions');
}

private function addConnections(NodeBuilder $builder)
private static function addConnections(NodeBuilder $builder): void
{
$connectionBuilder = $builder
->arrayNode('connections')
Expand Down
2 changes: 1 addition & 1 deletion src/Models/ClientConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private function cleanOptions(array $options): array
{
return array_filter(
$options,
function ($value) {
function ($value): bool {
return ! empty($value) || \is_int($value) || \is_bool($value) || \is_float($value);
}
);
Expand Down
51 changes: 6 additions & 45 deletions src/Services/ClientRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@ final class ClientRegistry
/** @var DriverOptionsInterface */
private $driverOptionsService;

/**
* ClientRegistry constructor.
*
* @param EventDispatcherInterface $eventDispatcher
* @param bool $debug
* @param DriverOptionsInterface|null $driverOptionsService
*/
public function __construct(
EventDispatcherInterface $eventDispatcher,
bool $debug,
Expand All @@ -53,25 +46,13 @@ public function __construct(
$this->driverOptionsService = $driverOptionsService;
}

/**
* @param array $configurations
*/
public function addClientsConfigurations(array $configurations)
public function addClientsConfigurations(array $configurations): void
{
foreach ($configurations as $name => $conf) {
$this->addClientConfiguration($name, $conf);
$this->configurations[$name] = $this->buildClientConfiguration($conf);
}
}

/**
* @param string $name
* @param array $conf
*/
private function addClientConfiguration(string $name, array $conf)
{
$this->configurations[$name] = $this->buildClientConfiguration($conf);
}

/**
* @param array $conf
*
Expand All @@ -80,7 +61,7 @@ private function addClientConfiguration(string $name, array $conf)
private function buildClientConfiguration(array $conf): ClientConfiguration
{
if (! $conf['uri']) {
$conf['uri'] = $this->buildConnectionUri($conf['hosts']);
$conf['uri'] = self::buildConnectionUri($conf['hosts']);
}

$conf['driverOptions'] = [];
Expand All @@ -103,50 +84,30 @@ private function buildClientConfiguration(array $conf): ClientConfiguration
);
}

/**
* @param array $hosts
*
* @return string
*/
private function buildConnectionUri(array $hosts): string
private static function buildConnectionUri(array $hosts): string
{
return 'mongodb://' . implode(
',',
array_map(
function (array $host) {
static function (array $host): string {
return sprintf('%s:%d', $host['host'], $host['port']);
},
$hosts
)
);
}

/**
* @param string $name
* @param string $databaseName
*
* @return Client
*/
public function getClientForDatabase(string $name, string $databaseName): Client
{
return $this->getClient($name, $databaseName);
}

/**
* @return array
*/
public function getClientNames(): array
{
return array_keys($this->clients);
}

/**
* @param string $name
* @param string $databaseName
*
* @return Client
*/
public function getClient(string $name, string $databaseName = null): Client
public function getClient(string $name, ?string $databaseName = null): Client
{
$clientKey = null !== $databaseName ? $name . '.' . $databaseName : $name;

Expand Down
4 changes: 2 additions & 2 deletions tests/Functional/Capsule/CollectionTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
<?php declare(strict_types=1);

declare(strict_types=1);
namespace Facile\MongoDbBundle\Tests\Functional\Capsule;

use Facile\MongoDbBundle\Capsule\Collection;
use Facile\MongoDbBundle\Event\QueryEvent;
Expand Down

0 comments on commit 1ef451d

Please sign in to comment.