Skip to content

Instantly share code, notes, and snippets.

@lpj145
Created August 25, 2017 14:02
Show Gist options
  • Save lpj145/8d50e9a0a495edcd06381eda0f617f02 to your computer and use it in GitHub Desktop.
Save lpj145/8d50e9a0a495edcd06381eda0f617f02 to your computer and use it in GitHub Desktop.
<?php
namespace FiremonPHP\Database\Connection;
final class Connection implements ConnectionInterface
{
/**
* @var string
*/
private $_alias;
/**
* @var \MongoDB\Driver\Manager
*/
private $_manager;
public function __construct(string $urlCon, string $alias, array $optionsCon = [])
{
$this->_manager = new \MongoDB\Driver\Manager($urlCon, $optionsCon);
$this->_alias = $alias;
}
/**
* @param string $type
* @param string $collectionName
* @param $query
* @return \MongoDB\Driver\Cursor|\MongoDB\Driver\WriteResult
* @throws \ErrorException
*/
public function executeQuery(string $type, string $collectionName, $query)
{
return $this->{'_'.$type}($collectionName, $query);
}
/**
* @param string $collectionName
* @param \MongoDB\Driver\BulkWrite $bulkWrite
* @return \MongoDB\Driver\WriteResult
*/
private function _write(string $collectionName, \MongoDB\Driver\BulkWrite $bulkWrite)
{
$namespace = $this->_alias.'.'.$collectionName;
return $this->_manager->executeBulkWrite($namespace, $bulkWrite);
}
/**
* @param string $collectionName
* @param \MongoDB\Driver\Query $query
* @return \MongoDB\Driver\Cursor
*/
private function _read(string $collectionName, \MongoDB\Driver\Query $query)
{
$namespace = $this->_alias.'.'.$collectionName;
return $this->_manager->executeQuery( $namespace, $query);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment