Skip to content

Instantly share code, notes, and snippets.

@lpj145
Created August 24, 2017 03:23
Show Gist options
  • Save lpj145/acf8ae3a0c0b45758f6a1e7ec21fa9cc to your computer and use it in GitHub Desktop.
Save lpj145/acf8ae3a0c0b45758f6a1e7ec21fa9cc to your computer and use it in GitHub Desktop.
Class connection
<?php
namespace FiremonPHP\Database;
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)
{
if ($type === 'Read') {
return $this->_read($collectionName, $query);
} elseif ($type === 'Write') {
return $this->_write($collectionName, $query);
} else {
throw new \ErrorException('Type of query not defined!');
}
}
/**
* @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