diff --git a/composer.json b/composer.json index ffa88b4..ef14257 100644 --- a/composer.json +++ b/composer.json @@ -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" } } diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index cd03c05..3ee2da1 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -13,27 +13,27 @@ */ 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') @@ -41,7 +41,7 @@ private function addDataCollection(NodeBuilder $builder) ->info('Disables Data Collection if needed'); } - private function addClients(NodeBuilder $builder) + private static function addClients(NodeBuilder $builder): void { $clientsBuilder = $builder ->arrayNode('clients') @@ -51,7 +51,7 @@ private function addClients(NodeBuilder $builder) ->prototype('array') ->children(); - $this->addClientsHosts($clientsBuilder); + self::addClientsHosts($clientsBuilder); $clientsBuilder ->scalarNode('uri') @@ -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') @@ -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') diff --git a/src/Models/ClientConfiguration.php b/src/Models/ClientConfiguration.php index 4c60410..8381071 100644 --- a/src/Models/ClientConfiguration.php +++ b/src/Models/ClientConfiguration.php @@ -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); } ); diff --git a/src/Services/ClientRegistry.php b/src/Services/ClientRegistry.php index 2dcd638..8707fbc 100644 --- a/src/Services/ClientRegistry.php +++ b/src/Services/ClientRegistry.php @@ -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, @@ -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 * @@ -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'] = []; @@ -103,17 +84,12 @@ 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 @@ -121,32 +97,17 @@ function (array $host) { ); } - /** - * @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; diff --git a/tests/Functional/Capsule/CollectionTest.php b/tests/Functional/Capsule/CollectionTest.php index eee420f..1aeff8f 100644 --- a/tests/Functional/Capsule/CollectionTest.php +++ b/tests/Functional/Capsule/CollectionTest.php @@ -1,6 +1,6 @@ -