Skip to content

Commit

Permalink
Merge pull request #8 from makasim/fetch-database-from-dsn
Browse files Browse the repository at this point in the history
Fetch database from mongo dsn.
  • Loading branch information
makasim authored Feb 2, 2018
2 parents 2dd5ed4 + 8fdf974 commit bcf728c
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/CollectionFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
namespace Makasim\Yadm;

use MongoDB\Client;
use MongoDB\Collection;

class CollectionFactory
{
/**
* @var Client
*/
private $mongodb;

/**
* @var string
*/
private $mongoDsn;

public function __construct(Client $mongodb, string $mongoDsn)
{
$this->mongodb = $mongodb;
$this->mongoDsn = $mongoDsn;
}

public function create(string $collectionName, string $databaseName = null, array $options = []): Collection
{
if (false == $databaseName) {
$databaseName = parse_url($this->mongoDsn, PHP_URL_PATH);
}

if (false == $databaseName) {
throw new \LogicException('Failed to guess database name, neither mongo DSN nor argument have it.');
}

$this->mongodb->selectCollection($databaseName, $collectionName, $options);
}
}

0 comments on commit bcf728c

Please sign in to comment.