diff --git a/src/LocatorRegistry.php b/src/LocatorRegistry.php index 3436996..579eb7b 100644 --- a/src/LocatorRegistry.php +++ b/src/LocatorRegistry.php @@ -40,7 +40,7 @@ public function getStorages(): array public function getStorage(string $name): Storage { if (false == $this->container->has($name)) { - throw new \InvalidArgumentException(sprintf('The storage with name "%s" does not exist', $id)); + throw new \InvalidArgumentException(sprintf('The storage with name "%s" does not exist', $name)); } return $this->container->get($name); diff --git a/src/Storage.php b/src/Storage.php index b64c1f4..abd5c1d 100644 --- a/src/Storage.php +++ b/src/Storage.php @@ -9,45 +9,33 @@ class Storage { - /** - * @var Collection - */ - private $collection; + private $collectionName; + + private $collectionFactory; - /** - * @var Hydrator - */ private $hydrator; - /** - * @var ChangesCollector - */ private $changesCollector; - /** - * @var PessimisticLock - */ private $pessimisticLock; - /** - * @var ConvertValues - */ private $convertValues; - /** - * @var StorageMetaInterface - */ private $storageMeta; + private $collection; + public function __construct( - Collection $collection, + string $collectionName, + CollectionFactory $collectionFactory, Hydrator $hydrator, ChangesCollector $changesCollector = null, PessimisticLock $pessimisticLock = null, ConvertValues $convertValues = null, StorageMetaInterface $storageMeta = null ) { - $this->collection = $collection; + $this->collectionName = $collectionName; + $this->collectionFactory = $collectionFactory; $this->hydrator = $hydrator; $this->pessimisticLock = $pessimisticLock; @@ -81,7 +69,7 @@ public function insert($model, array $options = []) { $values = $this->convertValues->convertToMongoValues(get_values($model), []); - $result = $this->collection->insertOne($values, $options); + $result = $this->getCollection()->insertOne($values, $options); if (false == $result->isAcknowledged()) { throw new \LogicException('Operation is not acknowledged'); } @@ -105,7 +93,7 @@ public function insertMany(array $models, array $options = []) $data[$key] = get_values($model, false); } - $result = $this->collection->insertMany($data, $options); + $result = $this->getCollection()->insertMany($data, $options); if (false == $result->isAcknowledged()) { throw new \LogicException('Operation is not acknowledged'); } @@ -168,13 +156,13 @@ public function update($model, $filter = null, array $options = []) $pushUpdate['$push'] = $update['$push']; unset($update['$push']); - $this->collection->updateOne($filter, $pushUpdate, $options); + $this->getCollection()->updateOne($filter, $pushUpdate, $options); if ($update) { - $result = $this->collection->updateOne($filter, $update, $options); + $result = $this->getCollection()->updateOne($filter, $update, $options); } } else { - $result = $this->collection->updateOne($filter, $update, $options); + $result = $this->getCollection()->updateOne($filter, $update, $options); } if ($useOptimisticLock && 0 === $result->getModifiedCount()) { @@ -216,7 +204,7 @@ public function replace($model, array $options = []) */ public function delete($model, array $options = []) { - return $this->collection->deleteOne(['_id' => get_object_id($model)], $options); + return $this->getCollection()->deleteOne(['_id' => get_object_id($model)], $options); } /** @@ -229,7 +217,7 @@ public function findOne(array $filter = [], array $options = []) { $options['typeMap'] = ['root' => 'array', 'document' => 'array', 'array' => 'array']; - if ($originalValues = $this->collection->findOne($filter, $options)) { + if ($originalValues = $this->getCollection()->findOne($filter, $options)) { $values = $this->convertValues->convertToPHPValues($originalValues); @@ -249,7 +237,7 @@ public function findOne(array $filter = [], array $options = []) */ public function find(array $filter = [], array $options = []) { - $cursor = $this->collection->find($filter, $options); + $cursor = $this->getCollection()->find($filter, $options); $cursor->setTypeMap(['root' => 'array', 'document' => 'array', 'array' => 'array']); foreach ($cursor as $originalValues) { @@ -276,7 +264,7 @@ public function register($object, array $originalValues) */ public function count(array $filter = [], array $options = []) { - return $this->collection->count($filter, $options); + return $this->getCollection()->count($filter, $options); } /** @@ -326,6 +314,10 @@ public function getMeta(): StorageMetaInterface */ public function getCollection(): Collection { + if (null === $this->collection) { + $this->collection = $this->collectionFactory->create($this->collectionName); + } + return $this->collection; }