-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from makasim/fetch-database-from-dsn
Fetch database from mongo dsn.
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |