Skip to content

Commit

Permalink
Merge pull request #50 from codeliner/feature/v5.0
Browse files Browse the repository at this point in the history
Feature/v5.0
  • Loading branch information
codeliner committed Jul 26, 2015
2 parents 132042b + 180a9ab commit e02e5bc
Show file tree
Hide file tree
Showing 48 changed files with 208 additions and 365 deletions.
27 changes: 27 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Copyright (c) 2014-2015, prooph software GmbH
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of the prooph software GmbH nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 changes: 0 additions & 27 deletions LICENSE.txt

This file was deleted.

19 changes: 8 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,26 @@
"php": ">=5.5",
"ramsey/uuid" : "~2.8",
"beberlei/assert": "~2.3",
"prooph/common" : "~2.0"
"prooph/common" : "~3.1",
"container-interop/container-interop" : "~1.1"
},
"require-dev": {
"phpunit/phpunit": "3.7.*",
"satooshi/php-coveralls": "dev-master"
"phpunit/phpunit": "~4.7",
"satooshi/php-coveralls": "dev-master",
"zendframework/zend-servicemanager": "~2.6"
},
"suggest" : {
"prooph/event-sourcing" : "Basic functionality for event sourced aggregates",
"prooph/service-bus" : "Enterprise Service Bus adapter to connect EventStore with messaging systems"
"prooph/service-bus" : "Message bus facade to connect the event store with messaging systems"
},
"autoload": {
"psr-0": {
"psr-4": {
"Prooph\\EventStore\\": "src/"
}
},
"autoload-dev": {
"psr-0": {
"psr-4": {
"Prooph\\EventStoreTest\\": "tests/"
}
},
"extra": {
"branch-alias": {
"dev-master": "0.5-dev"
}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -63,85 +63,90 @@ public function __construct(
) {
$this->eventStore = $eventStore;

$this->eventStore->getActionEventDispatcher()->attachListener('commit.pre', array($this, 'onPreCommit'));
$this->eventStore->getActionEventEmitter()->attachListener('commit.pre', $this);

$this->aggregateTranslator = $aggregateTranslator;
$this->streamStrategy = $streamStrategy;
$this->aggregateType = $aggregateType;
}

/**
* @param object $anEventSourcedAggregateRoot
* Repository acts as listener on EventStore.commit.pre events
* In the listener method the repository checks its identity map for pending events
* and appends the events to the event stream.
*/
public function __invoke()
{
foreach ($this->identityMap as $eventSourcedAggregateRoot) {

$pendingStreamEvents = $this->aggregateTranslator->extractPendingStreamEvents($eventSourcedAggregateRoot);

if (count($pendingStreamEvents)) {

$this->streamStrategy->appendEvents(
$this->aggregateType,
$this->aggregateTranslator->extractAggregateId($eventSourcedAggregateRoot),
$pendingStreamEvents,
$eventSourcedAggregateRoot
);
}
}
}

/**
* @param object $eventSourcedAggregateRoot
* @throws Exception\AggregateTypeException
*/
public function addAggregateRoot($anEventSourcedAggregateRoot)
public function addAggregateRoot($eventSourcedAggregateRoot)
{
if (! is_object($anEventSourcedAggregateRoot)) {
if (! is_object($eventSourcedAggregateRoot)) {
throw new AggregateTypeException(
sprintf(
'Invalid aggregate given. Aggregates need to be of type object but type of %s given',
gettype($anEventSourcedAggregateRoot)
gettype($eventSourcedAggregateRoot)
)
);
}

$aggregateId = $this->aggregateTranslator->extractAggregateId($anEventSourcedAggregateRoot);
$aggregateId = $this->aggregateTranslator->extractAggregateId($eventSourcedAggregateRoot);

$domainEvents = $this->aggregateTranslator->extractPendingStreamEvents($anEventSourcedAggregateRoot);
$domainEvents = $this->aggregateTranslator->extractPendingStreamEvents($eventSourcedAggregateRoot);

$this->streamStrategy->addEventsForNewAggregateRoot($this->aggregateType, $aggregateId, $domainEvents, $anEventSourcedAggregateRoot);
$this->streamStrategy->addEventsForNewAggregateRoot($this->aggregateType, $aggregateId, $domainEvents, $eventSourcedAggregateRoot);

$this->identityMap[$aggregateId] = $anEventSourcedAggregateRoot;
$this->identityMap[$aggregateId] = $eventSourcedAggregateRoot;
}

/**
* Returns null if no stream events can be found for aggregate root otherwise the reconstituted aggregate root
*
* @param string $anAggregateId
* @param string $aggregateId
* @return null|object
*/
public function getAggregateRoot($anAggregateId)
public function getAggregateRoot($aggregateId)
{
Assertion::string($anAggregateId, 'AggregateId needs to be string');
Assertion::string($aggregateId, 'AggregateId needs to be string');

if (isset($this->identityMap[$anAggregateId])) {
return $this->identityMap[$anAggregateId];
if (isset($this->identityMap[$aggregateId])) {
return $this->identityMap[$aggregateId];
}

$streamEvents = $this->streamStrategy->read($this->aggregateType, $anAggregateId);
$streamEvents = $this->streamStrategy->read($this->aggregateType, $aggregateId);

if (count($streamEvents) === 0) {
return null;
}

$aggregateType = $this->streamStrategy->getAggregateRootType($this->aggregateType, $streamEvents);

$anEventSourcedAggregateRoot = $this->aggregateTranslator->reconstituteAggregateFromHistory(
$eventSourcedAggregateRoot = $this->aggregateTranslator->reconstituteAggregateFromHistory(
$aggregateType,
$streamEvents
);

$this->identityMap[$anAggregateId] = $anEventSourcedAggregateRoot;

return $anEventSourcedAggregateRoot;
}

public function onPreCommit()
{
foreach ($this->identityMap as $eventSourcedAggregateRoot) {

$pendingStreamEvents = $this->aggregateTranslator->extractPendingStreamEvents($eventSourcedAggregateRoot);

if (count($pendingStreamEvents)) {
$this->identityMap[$aggregateId] = $eventSourcedAggregateRoot;

$this->streamStrategy->appendEvents(
$this->aggregateType,
$this->aggregateTranslator->extractAggregateId($eventSourcedAggregateRoot),
$pendingStreamEvents,
$eventSourcedAggregateRoot
);
}
}
return $eventSourcedAggregateRoot;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

namespace Prooph\EventStore\Aggregate;

use Prooph\Common\Messaging\DomainEvent;

/**
Expand All @@ -21,10 +22,10 @@
interface AggregateTranslator
{
/**
* @param object $anEventSourcedAggregateRoot
* @param object $eventSourcedAggregateRoot
* @return string
*/
public function extractAggregateId($anEventSourcedAggregateRoot);
public function extractAggregateId($eventSourcedAggregateRoot);

/**
* @param AggregateType $aggregateType
Expand All @@ -34,9 +35,9 @@ public function extractAggregateId($anEventSourcedAggregateRoot);
public function reconstituteAggregateFromHistory(AggregateType $aggregateType, array $historyEvents);

/**
* @param object $anEventSourcedAggregateRoot
* @param object $eventSourcedAggregateRoot
* @return DomainEvent[]
*/
public function extractPendingStreamEvents($anEventSourcedAggregateRoot);
public function extractPendingStreamEvents($eventSourcedAggregateRoot);
}

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
namespace Prooph\EventStore\Aggregate;

/**
* Interface AggregateTypeProviderInterface
* Interface AggregateTypeProvider
*
* @package Prooph\EventStore\EventSourcing
* @package Prooph\EventStore\Aggregate
* @author Alexander Miertsch <kontakt@codeliner.ws>
*/
interface AggregateTypeProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@
class DefaultAggregateTranslator implements AggregateTranslator
{
/**
* @param object $anEventSourcedAggregateRoot
* @param object $eventSourcedAggregateRoot
* @throws Exception\AggregateTranslationFailedException
* @return string
*/
public function extractAggregateId($anEventSourcedAggregateRoot)
public function extractAggregateId($eventSourcedAggregateRoot)
{
if (! method_exists($anEventSourcedAggregateRoot, 'id')) {
if (! method_exists($eventSourcedAggregateRoot, 'id')) {
throw new AggregateTranslationFailedException(
sprintf(
'Required method id does not exist for aggregate %s',
get_class($anEventSourcedAggregateRoot)
get_class($eventSourcedAggregateRoot)
)
);
}

return (string)$anEventSourcedAggregateRoot->id();
return (string)$eventSourcedAggregateRoot->id();
}

/**
Expand Down Expand Up @@ -81,19 +81,19 @@ public function reconstituteAggregateFromHistory(AggregateType $aggregateType, a
}

/**
* @param object $anEventSourcedAggregateRoot
* @param object $eventSourcedAggregateRoot
* @throws Exception\AggregateTranslationFailedException
* @return DomainEvent[]
*/
public function extractPendingStreamEvents($anEventSourcedAggregateRoot)
public function extractPendingStreamEvents($eventSourcedAggregateRoot)
{
$refObj = new \ReflectionClass($anEventSourcedAggregateRoot);
$refObj = new \ReflectionClass($eventSourcedAggregateRoot);

if (! $refObj->hasMethod('popRecordedEvents')) {
throw new AggregateTranslationFailedException(
sprintf(
'Can not extract pending events of aggregate %s. Class is missing a method with name popRecordedEvents!',
get_class($anEventSourcedAggregateRoot)
get_class($eventSourcedAggregateRoot)
)
);
}
Expand All @@ -102,7 +102,7 @@ public function extractPendingStreamEvents($anEventSourcedAggregateRoot)

$popRecordedEventsMethod->setAccessible(true);

$recordedEvents = $popRecordedEventsMethod->invoke($anEventSourcedAggregateRoot);
$recordedEvents = $popRecordedEventsMethod->invoke($eventSourcedAggregateRoot);

return $recordedEvents;
}
Expand Down
Loading

0 comments on commit e02e5bc

Please sign in to comment.