diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 36aac30..91c02f8 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -1,5 +1,5 @@ src - + diff --git a/src/Driver/MongoDB/Client.php b/src/Driver/MongoDB/Client.php index 63b2ef1..a966059 100644 --- a/src/Driver/MongoDB/Client.php +++ b/src/Driver/MongoDB/Client.php @@ -3,7 +3,6 @@ namespace PcComponentes\Transaction\Driver\MongoDB; -use MongoDB\Driver\ReadPreference; use MongoDB\Driver\Session; final class Client extends \MongoDB\Client @@ -13,26 +12,11 @@ final class Client extends \MongoDB\Client public function __construct( string $uri = 'mongodb://127.0.0.1/', array $uriOptions = [], - array $driverOptions = [], - array $transactionOptions = [] + array $driverOptions = [] ) { parent::__construct($uri, $uriOptions, $driverOptions); - $this->session = $this->startSession( - [ - 'defaultTransactionOptions' => \array_merge( - $transactionOptions, - $this->defaultTransactionOptions(), - ), - ] - ); - } - - private function defaultTransactionOptions(): array - { - return [ - 'readPreference' => new ReadPreference(ReadPreference::RP_PRIMARY), - ]; + $this->session = $this->startSession(); } public function selectDatabase($databaseName, array $options = []): Database diff --git a/src/Driver/MongoDB/Collection.php b/src/Driver/MongoDB/Collection.php index 37dbc5b..bea4ea3 100644 --- a/src/Driver/MongoDB/Collection.php +++ b/src/Driver/MongoDB/Collection.php @@ -3,37 +3,30 @@ namespace PcComponentes\Transaction\Driver\MongoDB; +use MongoDB\BSON\JavascriptInterface; use MongoDB\Driver\Manager; use MongoDB\Driver\Session; +use MongoDB\Operation\Explainable; final class Collection extends \MongoDB\Collection { private Session $session; - public function __construct( - Manager $manager, - $databaseName, - $collectionName, - Session $session, - array $options = [] - ) { + public function __construct(Manager $manager, $databaseName, $collectionName, Session $session, array $options = []) + { parent::__construct( $manager, $databaseName, $collectionName, - $options + $options, ); $this->session = $session; } - private function addSession(array $options): array + public function aggregate(array $pipeline, array $options = []) { - if (false === isset($options['session'])) { - $options['session'] = $this->session; - } - - return $options; + return parent::aggregate($pipeline, $this->addSession($options)); } public function bulkWrite(array $operations, array $options = []) @@ -41,6 +34,16 @@ public function bulkWrite(array $operations, array $options = []) return parent::bulkWrite($operations, $this->addSession($options)); } + public function count($filter = [], array $options = []) + { + return parent::count($filter, $this->addSession($options)); + } + + public function countDocuments($filter = [], array $options = []) + { + return parent::countDocuments($filter, $this->addSession($options)); + } + public function createIndex($key, array $options = []) { return parent::createIndex($key, $this->addSession($options)); @@ -61,9 +64,14 @@ public function deleteOne($filter, array $options = []) return parent::deleteOne($filter, $this->addSession($options)); } + public function distinct($fieldName, $filter = [], array $options = []) + { + return parent::distinct($fieldName, $filter, $this->addSession($options)); + } + public function drop(array $options = []) { - return parent::drop($this->addSession($options)); + return parent::drop($options); } public function dropIndex($indexName, array $options = []) @@ -73,7 +81,27 @@ public function dropIndex($indexName, array $options = []) public function dropIndexes(array $options = []) { - return parent::dropIndexes($this->addSession($options)); + return parent::dropIndexes($options); + } + + public function estimatedDocumentCount(array $options = []) + { + return parent::estimatedDocumentCount($options); + } + + public function explain(Explainable $explainable, array $options = []) + { + return parent::explain($explainable, $this->addSession($options)); + } + + public function find($filter = [], array $options = []) + { + return parent::find($filter, $this->addSession($options)); + } + + public function findOne($filter = [], array $options = []) + { + return parent::findOne($filter, $this->addSession($options)); } public function findOneAndDelete($filter, array $options = []) @@ -91,6 +119,46 @@ public function findOneAndUpdate($filter, $update, array $options = []) return parent::findOneAndUpdate($filter, $update, $this->addSession($options)); } + public function getCollectionName() + { + return parent::getCollectionName(); + } + + public function getDatabaseName() + { + return parent::getDatabaseName(); + } + + public function getManager() + { + return parent::getManager(); + } + + public function getNamespace() + { + return parent::getNamespace(); + } + + public function getReadConcern() + { + return parent::getReadConcern(); + } + + public function getReadPreference() + { + return parent::getReadPreference(); + } + + public function getTypeMap() + { + return parent::getTypeMap(); + } + + public function getWriteConcern() + { + return parent::getWriteConcern(); + } + public function insertMany(array $documents, array $options = []) { return parent::insertMany($documents, $this->addSession($options)); @@ -101,6 +169,16 @@ public function insertOne($document, array $options = []) return parent::insertOne($document, $this->addSession($options)); } + public function listIndexes(array $options = []) + { + return parent::listIndexes($options); + } + + public function mapReduce(JavascriptInterface $map, JavascriptInterface $reduce, $out, array $options = []) + { + return parent::mapReduce($map, $reduce, $out, $this->addSession($options)); + } + public function replaceOne($filter, $replacement, array $options = []) { return parent::replaceOne($filter, $replacement, $this->addSession($options)); @@ -115,4 +193,23 @@ public function updateOne($filter, $update, array $options = []) { return parent::updateOne($filter, $update, $this->addSession($options)); } + + public function watch(array $pipeline = [], array $options = []) + { + return parent::watch($pipeline, $this->addSession($options)); + } + + public function withOptions(array $options = []) + { + return parent::withOptions($options); + } + + private function addSession(array $options): array + { + if (false === isset($options['session'])) { + $options['session'] = $this->session; + } + + return $options; + } }