Skip to content

Commit

Permalink
Updated DataMapper in order to trigger events for database operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Zizaco committed Apr 9, 2016
1 parent 278b795 commit a085dff
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 10 deletions.
75 changes: 65 additions & 10 deletions src/Mongolid/DataMapper/DataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Mongolid\Container\Ioc;
use Mongolid\Connection\Pool;
use Mongolid\Cursor\Cursor;
use Mongolid\Event\EventTriggerService;
use MongoDB\Collection;
use MongoDB\BSON\ObjectID;

Expand Down Expand Up @@ -36,6 +37,12 @@ class DataMapper
*/
protected $connPool;

/**
* In order to dispatch events when necessary
* @var EventTriggerService
*/
protected $eventService;

/**
* @param Pool $connPool The connections that are going to be used to interact with the database.
*/
Expand All @@ -54,6 +61,13 @@ public function __construct(Pool $connPool)
*/
public function save($object)
{
// If the "saving" event returns false we'll bail out of the save and return
// false, indicating that the save failed. This gives an opportunities to
// listeners to cancel save operations if validations fail or whatever.
if ($this->fireEvent('saving', $object, true) === false) {
return false;
}

$data = $this->parseToDocument($object);

$result = $this->getCollection()->updateOne(
Expand All @@ -62,7 +76,13 @@ public function save($object)
['upsert' => true]
);

return (bool) ($result->getModifiedCount() + $result->getUpsertedCount());
$result = (bool) ($result->getModifiedCount() + $result->getUpsertedCount());

if ($result) {
$this->fireEvent('saved', $object);
}

return $result;
}

/**
Expand All @@ -76,8 +96,8 @@ public function save($object)
*/
public function insert($object): bool
{
if (! $object->_id) {
$object->_id = new ObjectID;
if ($this->fireEvent('inserting', $object, true) === false) {
return false;
}

$data = $this->parseToDocument($object);
Expand All @@ -86,7 +106,13 @@ public function insert($object): bool
$data
);

return (bool) $result->getInsertedCount();
$result = (bool) $result->getInsertedCount();

if ($result) {
$this->fireEvent('inserted', $object);
}

return $result;
}

/**
Expand All @@ -100,8 +126,8 @@ public function insert($object): bool
*/
public function update($object)
{
if (! $object->_id) {
$object->_id = new ObjectID;
if ($this->fireEvent('updating', $object, true) === false) {
return false;
}

$data = $this->parseToDocument($object);
Expand All @@ -111,7 +137,13 @@ public function update($object)
['$set' => $data]
);

return (bool) $result->getModifiedCount();
$result = (bool) $result->getModifiedCount();

if ($result) {
$this->fireEvent('updated', $object);
}

return $result;
}

/**
Expand All @@ -123,8 +155,8 @@ public function update($object)
*/
public function delete($object)
{
if (! $object->_id) {
$object->_id = new ObjectID;
if ($this->fireEvent('deleting', $object, true) === false) {
return false;
}

$data = $this->parseToDocument($object);
Expand All @@ -133,7 +165,13 @@ public function delete($object)
['_id' => $data['_id']]
);

return (bool) $result->getDeletedCount();
$result = (bool) $result->getDeletedCount();

if ($result) {
$this->fireEvent('deleted', $object);
}

return $result;
}

/**
Expand Down Expand Up @@ -284,4 +322,21 @@ protected function prepareValueQuery($value): array

return ['_id' => $value];
}

/**
* Triggers an event. May return if that event had success.
*
* @param string $event Identification of the event.
* @param mixed $entity Event payload.
* @param boolean $halt True if the return of the event handler will be used in a coditional.
*
* @return mixed Event handler return.
*/
protected function fireEvent(string $event, $entity, bool $halt = false)
{
$event = "mongolid.{$event}." . get_class($entity);

return ($this->eventService ? $this->eventService : $this->eventService = Ioc::make(EventTriggerService::class))
->fire($event, $entity, $halt);
}
}
29 changes: 29 additions & 0 deletions tests/Mongolid/DataMapper/DataMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Mongolid\Container\Ioc;
use Mongolid\Cursor\Cursor;
use Mongolid\Schema;
use Mongolid\Event\EventTriggerService;
use MongoDB\BSON\ObjectID;
use TestCase;
use stdClass;
Expand All @@ -17,6 +18,7 @@ class DataMapperTest extends TestCase
{
public function tearDown()
{
$this->eventService = null;
parent::tearDown();
m::close();
}
Expand Down Expand Up @@ -68,6 +70,9 @@ public function testShouldSave()
->once()
->andReturn(1);

$this->expectEvent('saving', $object, true);
$this->expectEvent('saved', $object, false);

// Assert
$this->assertTrue($mapper->save($object));
}
Expand Down Expand Up @@ -106,6 +111,9 @@ public function testShouldInsert()
->once()
->andReturn(1);

$this->expectEvent('inserting', $object, true);
$this->expectEvent('inserted', $object, false);

// Assert
$this->assertTrue($mapper->insert($object));
}
Expand Down Expand Up @@ -146,6 +154,9 @@ public function testShouldUpdate()
->once()
->andReturn(1);

$this->expectEvent('updating', $object, true);
$this->expectEvent('updated', $object, false);

// Assert
$this->assertTrue($mapper->update($object));
}
Expand Down Expand Up @@ -184,6 +195,9 @@ public function testShouldDelete()
->once()
->andReturn(1);

$this->expectEvent('deleting', $object, true);
$this->expectEvent('deleted', $object, false);

// Assert
$this->assertTrue($mapper->delete($object));
}
Expand Down Expand Up @@ -466,6 +480,21 @@ public function testShouldPrepareQueryValue($value, $expectation)
);
}

protected function expectEvent($event, $entity, bool $halt, $return = true)
{
if (! ($this->eventService ?? false)) {
$this->eventService = m::mock(EventTriggerService::class);
Ioc::instance(EventTriggerService::class, $this->eventService);
}

$event = 'mongolid.'.$event.'.'.get_class($entity);

$this->eventService->shouldReceive('fire')
->with($event, $entity, $halt)
->atLeast()->once()
->andReturn($return);
}

public function queryValueScenarios()
{
return [
Expand Down

0 comments on commit a085dff

Please sign in to comment.