Skip to content

Commit

Permalink
remove deps on persistable interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
makasim committed Feb 10, 2016
1 parent a200bbd commit a9821ae
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 148 deletions.
21 changes: 8 additions & 13 deletions src/Hydrator.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php
namespace Makasim\Yadm;

use MongoDB\BSON\Persistable;

class Hydrator
{
/**
Expand All @@ -24,7 +22,7 @@ public function __construct($modelClass)
}

/**
* @return Persistable|object
* @return object
*/
public function create()
{
Expand All @@ -38,24 +36,21 @@ public function create()
}

/**
* @param array|object $bson
* @param array $values
*
* @param Persistable|null $model
* @param object|null $model
*
* @return Persistable|object
* @return object
*/
public function hydrate(array $bson, Persistable $model = null)
public function hydrate(array $values, $model = null)
{
$model = $model ?: $this->create();
if (false == $model instanceof Persistable) {
throw new \LogicException(sprintf('The model %s must implement %s interface', $this->modelClass, Persistable::class));
}

if (isset($bson['_id'])) {
$bson['_id'] = (string) $bson['_id'];
if (isset($values['_id'])) {
$values['_id'] = (string) $values['_id'];
}

$model->bsonUnserialize($bson);
set_values($model, $values);

return $model;
}
Expand Down
82 changes: 0 additions & 82 deletions src/Iterator.php

This file was deleted.

18 changes: 0 additions & 18 deletions src/PersistableTrait.php

This file was deleted.

67 changes: 32 additions & 35 deletions src/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
namespace Makasim\Yadm;

use MongoDB\BSON\ObjectID;
use MongoDB\BSON\Persistable;
use MongoDB\Collection;

class Storage
Expand Down Expand Up @@ -35,49 +34,51 @@ public function __construct(Collection $collection, Hydrator $hydrator, Pessimis
}

/**
* @return Persistable
* @return object
*/
public function create()
{
return $this->hydrator->create();
}

/**
* @param Persistable $model
* @param array $options
* @param object $model
* @param array $options
*
* @return \MongoDB\InsertOneResult
*/
public function insert(Persistable $model, array $options = [])
public function insert($model, array $options = [])
{
$bson = $model->bsonSerialize();
$values = get_values($model);

$result = $this->collection->insertOne($bson, $options);
$result = $this->collection->insertOne($values, $options);
if (false == $result->isAcknowledged()) {
throw new \LogicException('Operation is not acknowledged');
}

$bson['_id'] = (string) $result->getInsertedId();

$this->hydrator->hydrate($bson, $model);
$this->hydrator->hydrate($values, $model);
set_object_id($model, $result->getInsertedId());

return $result;
}

/**
* @param Persistable $model
* @param array $options
* @param object $model
* @param null|array $filter
* @param array $options
*
* @return \MongoDB\UpdateResult
*/
public function update(Persistable $model, array $options = [])
public function update($model, $filter = null, array $options = [])
{
$bson = $model->bsonSerialize();
if (null === $filter) {
$filter = ['_id' => new ObjectID(get_object_id($model))];
}

$modelId = $bson['_id'];
unset($bson['_id']);
$values = get_values($model);
unset($values['_id']);

$result = $this->collection->updateOne(['_id' => new ObjectID($modelId)], ['$set' => $bson], $options);
$result = $this->collection->updateOne($filter, ['$set' => $values], $options);
if (false == $result->isAcknowledged()) {
throw new \LogicException('Operation is not acknowledged');
}
Expand All @@ -86,22 +87,16 @@ public function update(Persistable $model, array $options = [])
}

/**
* @param Persistable $model
* @param array $options
* @param object $model
* @param array $options
*
* @return \MongoDB\DeleteResult
*/
public function delete(Persistable $model, array $options = [])
public function delete($model, array $options = [])
{
$bson = $model->bsonSerialize();

if (is_array($bson)) {
$modelId = $bson['_id'];
unset($bson['_id']);
} else {
$modelId = $bson->_id;
unset($bson->_id);
}
$modelId = get_object_id($model);
$values = get_values($model);
unset($values['_id']);

$result = $this->collection->deleteOne(['_id' => new ObjectID($modelId)], $options);
if (false == $result->isAcknowledged()) {
Expand All @@ -115,29 +110,31 @@ public function delete(Persistable $model, array $options = [])
* @param array $filter
* @param array $options
*
* @return Persistable
* @return object
*/
public function findOne(array $filter = [], array $options = [])
{
$options['typeMap'] = ['root' => 'array', 'document' => 'array', 'array' => 'array'];

if ($bson = $this->collection->findOne($filter, $options)) {
return $this->hydrator->hydrate($bson);
if ($values = $this->collection->findOne($filter, $options)) {
return $this->hydrator->hydrate($values);
}
}

/**
* @param array $filter
* @param array $options
*
* @return Iterator
* @return \Traversable
*/
public function find(array $filter = [], array $options = [])
{
$cursor = $this->collection->find($filter, $options);
$cursor->setTypeMap(['root' => 'array', 'document' => 'array', 'array' => 'array']);

return new Iterator($cursor, $this->hydrator);
foreach ($cursor as $values) {
yield $this->hydrator->hydrate($values);
}
}

/**
Expand All @@ -152,7 +149,7 @@ public function lock($id, callable $lockCallback)

$this->pessimisticLock->lock($id);
try {
if ($model = $this->findOne(['_id' => (string) $id])) {
if ($model = $this->findOne(['_id' => new ObjectID((string) $id)])) {
call_user_func($lockCallback, $model, $this);
}
} finally {
Expand Down
28 changes: 28 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
namespace Makasim\Yadm;
use MongoDB\BSON\ObjectID;

/**
* @param object $object
Expand Down Expand Up @@ -91,4 +92,31 @@ function clone_object($object)
$values = get_values($object);

return build_object(get_class($object), $values);
}

/**
* @param object $object
*
* @return string
*/
function get_object_id($object)
{
$function = \Closure::bind(function ($object) {
return (string) isset($object->values['_id']) ? $object->values['_id'] : null;
}, null, $object);

return $function($object);
}

/**
* @param object $object
* @param ObjectID|string $objectId
*/
function set_object_id($object, $objectId)
{
$function = \Closure::bind(function ($object) use ($objectId) {
$object->values['_id'] = (string) $objectId;
}, null, $object);

return $function($object);
}

0 comments on commit a9821ae

Please sign in to comment.