Created
September 9, 2017 02:34
-
-
Save lpj145/68a7140151a3f6ac2797939b9a9fe53b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?php | |
namespace FiremonPHP\Manager; | |
use FiremonPHP\Manager\Expression\ConditionsExpression; | |
use MongoDB\Driver\BulkWrite; | |
use MongoDB\Driver\Cursor; | |
use MongoDB\Driver\Query; | |
use MongoDB\Driver\WriteResult; | |
class Manager | |
{ | |
/** | |
* @var \MongoDB\Driver\Manager | |
*/ | |
private $_manager; | |
/** | |
* @var string | |
*/ | |
private $_alias; | |
/** | |
* @var \MongoDB\Driver\BulkWrite[] | |
*/ | |
private $_bulks = []; | |
/** | |
* @var WriteResult[] | |
*/ | |
private $writeResults = []; | |
public function __construct(\MongoDB\Driver\Manager $manager, string $databaseName) | |
{ | |
$this->_manager = $manager; | |
$this->_alias = $databaseName; | |
} | |
/** | |
* Execute all bulk queries | |
* @return WriteResult[] | |
*/ | |
public function execute() | |
{ | |
$self = $this; | |
array_walk($this->_bulks, function(BulkWrite $write, $collectionName) use(&$self) { | |
$self->writeResults[$collectionName] = $self->_manager->executeBulkWrite($self->_alias.'.'.$collectionName, $write); | |
}); | |
return $self->writeResults; | |
} | |
/** | |
* @param string $collection | |
* @param array $data | |
*/ | |
public function insert(string $collection, array $data) | |
{ | |
$this->getWrites($collection) | |
->insert($data); | |
} | |
/** | |
* @param string $collection | |
* @param array $data | |
* @param $conditions | |
* @param array $options | |
*/ | |
public function update(string $collection, array $data, $conditions, array $options = []) | |
{ | |
if ($this->isCallable($conditions)) { | |
$conditions = (array)$conditions(new ConditionsExpression()); | |
} | |
$this->getWrites($collection) | |
->update($conditions, ['$set' => $data], $options); | |
} | |
/** | |
* @param string $collection | |
* @param $conditions | |
* @param array $options | |
*/ | |
public function delete(string $collection, $conditions, array $options = ['limit' => 1]) | |
{ | |
if ($this->isCallable($conditions)) { | |
$conditions = (array)$conditions(new ConditionsExpression()); | |
} | |
$this->getWrites($collection) | |
->delete($conditions, $options); | |
} | |
/** | |
* @param string $collection | |
* @param $conditions | |
* @param array $options | |
* @return \MongoDB\Driver\Cursor | |
*/ | |
public function find(string $collection, $conditions, array $options = []): Cursor | |
{ | |
if ($this->isCallable($conditions)) { | |
$conditions = (array)$conditions(new ConditionsExpression()); | |
} | |
$query = new Query($conditions, $options); | |
return $this->_manager->executeQuery($this->_alias.'.'.$collection, $query); | |
} | |
/** | |
* @param string $collectionName | |
* @return BulkWrite | |
*/ | |
private function getWrites(string $collectionName) | |
{ | |
return $this->_bulks[$collectionName] ?? $this->_bulks[$collectionName] = new BulkWrite(); | |
} | |
/** | |
* @param $c | |
* @return bool | |
*/ | |
private function isCallable($c) | |
{ | |
return is_callable($c); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment