Skip to content

Commit

Permalink
Validate that mongodb_uri is not set with doctrine_connection
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed May 30, 2024
1 parent 581ee74 commit dcd874d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
4 changes: 2 additions & 2 deletions docs/6-gridfs.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ flysystem:
bucket: 'fs'
```
## Full configuration
## With a Full Configuration
To initialize the GridFS bucket from configuration, set the `mongodb_uri` and `database` options, others are optional.

Expand Down Expand Up @@ -55,7 +55,7 @@ MONGODB_URI=mongodb://127.0.0.1:27017/
MONGODB_DB=flysystem
```

## With bucket service
## With a Bucket Service

For a more advanced configuration, create a service for
[`MongoDB\GridFS\Bucket`](https://www.mongodb.com/docs/php-library/current/tutorial/gridfs/):
Expand Down
21 changes: 13 additions & 8 deletions src/Adapter/Builder/GridFSAdapterDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,34 @@ protected function configureOptions(OptionsResolver $resolver): void
protected function configureDefinition(Definition $definition, array $options, ?string $defaultVisibilityForDirectories): void
{
if (isset($options['doctrine_connection'])) {
$bucketDefinition = new Definition(Bucket::class);
$bucketDefinition->setFactory([self::class, 'initializeBucketFromDocumentManager']);
$bucketDefinition->setArguments([
if (isset($options['mongodb_uri'])) {
throw new InvalidArgumentException('In GridFS configuration, "doctrine_connection" and "mongodb_uri" options cannot be set together.');
}
$bucket = new Definition(Bucket::class);
$bucket->setFactory([self::class, 'initializeBucketFromDocumentManager']);
$bucket->setArguments([
new Reference(sprintf('doctrine_mongodb.odm.%s_document_manager', $options['doctrine_connection'])),
$options['database'],
$options['bucket'],
]);
} elseif (isset($options['mongodb_uri'])) {
$bucketDefinition = new Definition(Bucket::class);
$bucketDefinition->setFactory([self::class, 'initializeBucketFromConfig']);
$bucketDefinition->setArguments([
$bucket = new Definition(Bucket::class);
$bucket->setFactory([self::class, 'initializeBucketFromConfig']);
$bucket->setArguments([
$options['mongodb_uri'],
$options['mongodb_uri_options'],
$options['mongodb_driver_options'],
$options['database'] ?? throw new InvalidArgumentException('MongoDB "database" name is required for Flysystem GridFS configuration'),
$options['bucket'],
]);
} elseif (!$options['bucket']) {
} elseif ($options['bucket']) {
$bucket = new Reference($options['bucket']);
} else {
throw new InvalidArgumentException('Flysystem GridFS configuration requires a "bucket" service name, a "mongodb_uri" or a "doctrine_connection" name');
}

$definition->setClass(GridFSAdapter::class);
$definition->setArgument(0, $bucketDefinition ?? new Reference($options['bucket']));
$definition->setArgument(0, $bucket);
$definition->setArgument(1, $options['prefix']);
}

Expand Down
32 changes: 32 additions & 0 deletions tests/Adapter/Builder/GridFSAdapterDefinitionBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use MongoDB\Client;
use MongoDB\GridFS\Bucket;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;

class GridFSAdapterDefinitionBuilderTest extends TestCase
{
Expand Down Expand Up @@ -64,6 +65,37 @@ public function testCreateDefinition($options)
$this->assertSame(GridFSAdapter::class, $this->createBuilder()->createDefinition($options, null)->getClass());
}

public function provideInvalidOptions(): \Generator
{
yield 'empty' => [
[],
'Flysystem GridFS configuration requires a "bucket" service name, a "mongodb_uri" or a "doctrine_connection" name',
];

yield 'no database with mongodb_uri' => [
['mongodb_uri' => 'mongodb://127.0.0.1:27017/'],
'MongoDB "database" name is required for Flysystem GridFS configuration'
];

yield 'both doctrine_connection and mongodb_uri' => [
['doctrine_connection' => 'default', 'mongodb_uri' => 'mongodb://127.0.0.1:27017/'],
'In GridFS configuration, "doctrine_connection" and "mongodb_uri" options cannot be set together.'
];
}

/**
* @dataProvider provideInvalidOptions
*/
public function testInvalidOptions(array $options, string $message)
{
$builder = $this->createBuilder();

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage($message);

$builder->createDefinition($options, null);
}

public function testInitializeBucketFromDocumentManager()
{
$client = new Client();
Expand Down

0 comments on commit dcd874d

Please sign in to comment.