Skip to content

Commit

Permalink
change how to id is appended into object
Browse files Browse the repository at this point in the history
this responsability was moved to Mapper, because the Mapper must know
how to id is defined.

fix Respect#9
  • Loading branch information
tonicospinelli committed Mar 23, 2016
1 parent 64a9e25 commit e523889
Show file tree
Hide file tree
Showing 15 changed files with 149 additions and 38 deletions.
8 changes: 7 additions & 1 deletion src/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function find($collection, array $query = []);
* @param string $collection
* @param object $document
*
* @return void
* @return string Returns the inserted id for current document.
*/
public function insert($collection, $document);

Expand All @@ -70,4 +70,10 @@ public function remove($collection, $criteria);
* @return array
*/
public function generateQuery(Collection $collection);

/**
* @param string|int $id
* @return string
*/
public function createObjectId($id = null);
}
24 changes: 17 additions & 7 deletions src/Driver/DynamoDb/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Aws\DynamoDb\DynamoDbClient;
use Aws\DynamoDb\Marshaler;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidFactoryInterface;
use Respect\Data\CollectionIterator;
use Respect\Data\Collections\Collection;
Expand Down Expand Up @@ -75,6 +74,14 @@ public function setStyle(Stylable $style)
return $this;
}

public function createObjectId($id = null)
{
if (is_null($id)) {
$id = $this->uuid->uuid4()->toString();
}
return $id;
}

/**
* @param \Iterator $cursor
*
Expand Down Expand Up @@ -154,7 +161,8 @@ protected function getConditionArray(Collection $collection)
$condition = $collection->getCondition();

if (!is_array($condition)) {
$condition = ['_id' => $condition];
$identifier = $this->getStyle()->identifier($collection->getName());
$condition = [$identifier => $condition];
}

$conditions = [];
Expand All @@ -174,8 +182,7 @@ protected function getConditionArray(Collection $collection)
}

/**
* @param Collection $collection
* @param $document
* {@inheritdoc}
*/
public function insert($collection, $document)
{
Expand All @@ -187,12 +194,15 @@ public function insert($collection, $document)
];

$this->getConnection()->putItem($args);

$identifierName = $this->getStyle()->identifier($collection);
return $document->{$identifierName};
}

/**
* @param Collection $collection
* @param $criteria
* @param $document
* @param $criteria
* @param $document
*/
public function update($collection, $criteria, $document)
{
Expand All @@ -207,7 +217,7 @@ public function update($collection, $criteria, $document)

/**
* @param Collection $collection
* @param $criteria
* @param $criteria
*/
public function remove($collection, $criteria)
{
Expand Down
7 changes: 0 additions & 7 deletions src/Driver/MongoDb/AbstractDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ abstract class AbstractDriver implements BaseDriver
*/
abstract public function getDatabase();

/**
* @param int|string $id
*
* @return mixed
*/
abstract public function createObjectId($id);

/**
* {@inheritdoc}
*/
Expand Down
10 changes: 10 additions & 0 deletions src/Driver/MongoDb/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,14 @@ public function remove($collection, $criteria)
{
$this->getConnection()->remove($collection, $criteria);
}

/**
* @param string $id
* @return string
*/
public function createObjectId($id = null)
{
return $this->getConnection()->createObjectId($id);
}

}
9 changes: 3 additions & 6 deletions src/Driver/MongoDb/MongoDbDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function getDatabase()
*
* @return ObjectID
*/
public function createObjectId($id)
public function createObjectId($id = null)
{
return new ObjectID($id);
}
Expand Down Expand Up @@ -88,15 +88,12 @@ public function find($collection, array $query = [])
}

/**
* @param string $collection
* @param $document
*
* @return void
* {@inheritdoc}
*/
public function insert($collection, $document)
{
$result = $this->getDatabase()->selectCollection($collection)->insertOne($document);
$document->_id = $result->getInsertedId();
return $result->getInsertedId();
}

public function update($collection, $criteria, $document)
Expand Down
11 changes: 4 additions & 7 deletions src/Driver/MongoDb/MongoDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Respect\Structural\Driver\MongoDb;

use Respect\Data\Collections\Collection;

class MongoDriver extends AbstractDriver
{
/**
Expand Down Expand Up @@ -73,7 +71,7 @@ public function find($collection, array $query = [])
*
* @return \MongoId|\MongoInt32
*/
public function createObjectId($id)
public function createObjectId($id = null)
{
if (is_int($id)) {
return new \MongoInt32($id);
Expand All @@ -83,14 +81,13 @@ public function createObjectId($id)
}

/**
* @param Collection $collection
* @param $document
*
* @return void
* {@inheritdoc}
*/
public function insert($collection, $document)
{
$this->getDatabase()->selectCollection($collection)->insert($document);
$identifierName = $this->getStyle()->identifier($collection);
return $document->{$identifierName};
}

public function update($collection, $criteria, $document)
Expand Down
49 changes: 48 additions & 1 deletion src/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Respect\Structural;

use Exception;
use Ramsey\Uuid\UuidFactoryInterface;
use Respect\Data\AbstractMapper;
use Respect\Data\CollectionIterator;
use Respect\Data\Collections as c;
Expand All @@ -15,20 +16,30 @@ class Mapper extends AbstractMapper implements
c\Mixable,
c\Typable
{
const ATTRIBUTTE_AUTO_GENERATE_ID = 'auto_generate_id';
const ATTRIBUTTE_AUTO_IDENTIFIER_HANDLER = 'auto_identifier_handler';

/** @var \Respect\Structural\Driver Holds our connector* */
protected $driver;

/** @var string Namespace to look for entities * */
public $entityNamespace = '\\';

/**
* @var array
*/
protected $options;

/**
* @param Driver $driver
* @param array $options
*/
public function __construct(Driver $driver)
public function __construct(Driver $driver, array $options = [])
{
parent::__construct();

$this->driver = $driver;
$this->options = $options;
}

/**
Expand Down Expand Up @@ -157,6 +168,12 @@ protected function rawUpdate(array $columns, Collection $collection)
protected function rawInsert(Collection $collection, $entity = null)
{
$name = $collection->getName();

$identifier = $this->getStyle()->identifier($name);

$generatedId = $this->driver->createObjectId();
$entity->{$identifier} = $generatedId;

$this->driver->insert($name, $entity);
}

Expand Down Expand Up @@ -395,4 +412,34 @@ public function getStyle()
{
return $this->driver->getStyle();
}

/**
* @return bool
*/
protected function isIdentifierAutoGenerated()
{
if (isset($this->options[self::ATTRIBUTTE_AUTO_GENERATE_ID])) {
return (bool) $this->options[self::ATTRIBUTTE_AUTO_GENERATE_ID];
}
return false;
}

/**
* @return UuidFactoryInterface
* @throws Mapper\Exception
*/
protected function getIdentifierHandler()
{
$name = self::ATTRIBUTTE_AUTO_IDENTIFIER_HANDLER;

if (!$this->isIdentifierAutoGenerated()) {
throw \Respect\Structural\Mapper\Exception::disabledGenerateIdentifier();
}

if (!$this->options[$name] instanceof UuidFactoryInterface) {
throw \Respect\Structural\Mapper\Exception::notValidIdGenerator(get_class($this->options[$name]));
}

return $this->options[$name];
}
}
20 changes: 20 additions & 0 deletions src/Mapper/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Respect\Structural\Mapper;

use Ramsey\Uuid\UuidFactoryInterface;
use Respect\Structural\Mapper;

class Exception extends \Exception
{
public static function disabledGenerateIdentifier()
{
$name = Mapper::ATTRIBUTTE_AUTO_GENERATE_ID;
return new self("The id generator is disabled. Add a {$name} in options.");
}

public static function notValidIdGenerator($className)
{
return new self("The id generator is not and instance of " . UuidFactoryInterface::class . ", given a {$className}");
}
}
7 changes: 7 additions & 0 deletions tests/Driver/DynamoDb/DriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,11 @@ public function provideGenerateQueryShouldUsePartialResultSets()
]
];
}

public function provideDataToInsert()
{
return [
'data with id' => [(object) ['name' => 'Test', '_id' => uniqid()]],
];
}
}
4 changes: 2 additions & 2 deletions tests/Driver/DynamoDb/StyleTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace Respect\Structural\tests\Driver\DynamoDb;
namespace Respect\Structural\Tests\Driver\DynamoDb;

use Respect\Structural\Driver\DynamoDb\Style;

class StyleTest extends \PHPUnit_Framework_TestCase
{
public function testShouldRetrieveIdenfier()
public function testShouldRetrieveIdentifier()
{
$this->assertEquals('_id', (new Style())->identifier('id'));
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Driver/MongoDb/DriverTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Respect\Structural\tests\Driver\MongoDb;
namespace Respect\Structural\Tests\Driver\MongoDb;

use Respect\Data\Collections\Collection;
use Respect\Structural\Driver\MongoDb\Driver;
Expand Down
7 changes: 7 additions & 0 deletions tests/Driver/MongoDb/MongoDbDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,11 @@ public function provideCollectionAndSearchShouldRetrieveFilledResult()
],
];
}
public function provideDataToInsert()
{
return [
'data without id' => [(object) ['name' => 'Test']],
];
}

}
8 changes: 8 additions & 0 deletions tests/Driver/MongoDb/MongoDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,12 @@ public function provideCollectionAndSearchShouldRetrieveFilledResult()
],
];
}

public function provideDataToInsert()
{
return [
'data without id' => [(object) ['name' => 'Test']],
];
}

}
18 changes: 12 additions & 6 deletions tests/Driver/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ abstract public function provideGenerateQueryShouldUsePartialResultSets();
*/
abstract public function provideCollectionAndSearchShouldRetrieveFilledResult();

/**
* @return array
*/
abstract public function provideDataToInsert();

/**
* @param string $method
* @param mixed $result
Expand Down Expand Up @@ -185,14 +190,15 @@ public function testGenerateQueryShouldUsePartialResultSets(Collection $mappedCo
$this->assertEquals($expectedResult, $result);
}

public function testInsertDataShouldRetrieveId()
/**
* @param $document
* @dataProvider provideDataToInsert
*/
public function testInsertDataShouldRetrieveId($document)
{
$data = new \stdClass();
$data->name = 'Test';

$this->createDriver($this->getMockConnectionInsertOne())->insert('author', $data);
$id = $this->createDriver($this->getMockConnectionInsertOne())->insert('author', $document);

$this->assertObjectHasAttribute('_id', $data);
$this->assertNotEmpty($id);
}

public function testUpdateDataShouldWithSuccess()
Expand Down
3 changes: 3 additions & 0 deletions tests/MapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ class MapperTest extends \PHPUnit_Framework_TestCase
public function testInsertANewDocument()
{
$style = $this->getMockForAbstractClass(Stylable::class);
$style->expects($this->once())->method('identifier')->willReturn('id');

$driver = $this->getMockForAbstractClass(Driver::class);
$driver->expects($this->once())->method('insert')->willReturnCallback(function ($collection, $document) {
$document->id = 1;
});
$driver->expects($this->once())->method('getStyle')->willReturn($style);


$mapper = new Mapper($driver);

Expand Down

0 comments on commit e523889

Please sign in to comment.