Skip to content

Commit

Permalink
Make storage lazy.
Browse files Browse the repository at this point in the history
  • Loading branch information
makasim committed May 14, 2019
1 parent df6134f commit a74d98f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/LocatorRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
52 changes: 22 additions & 30 deletions src/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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');
}
Expand All @@ -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');
}
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);


Expand All @@ -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) {
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit a74d98f

Please sign in to comment.