Prooph\EventStore\Aggregate\AggregateRepository::getAggregateRoot PHP Method

getAggregateRoot() public method

Returns null if no stream events can be found for aggregate root otherwise the reconstituted aggregate root
public getAggregateRoot ( string $aggregateId ) : null | object
$aggregateId string
return null | object
    public function getAggregateRoot($aggregateId)
    {
        Assertion::string($aggregateId, 'AggregateId needs to be string');
        if (isset($this->identityMap[$aggregateId])) {
            return $this->identityMap[$aggregateId];
        }
        if ($this->snapshotStore) {
            $eventSourcedAggregateRoot = $this->loadFromSnapshotStore($aggregateId);
            if ($eventSourcedAggregateRoot) {
                //Cache aggregate root in the identity map
                $this->identityMap[$aggregateId] = $eventSourcedAggregateRoot;
                return $eventSourcedAggregateRoot;
            }
        }
        $streamName = $this->determineStreamName($aggregateId);
        $streamEvents = null;
        if ($this->oneStreamPerAggregate) {
            $streamEvents = $this->eventStore->load($streamName)->streamEvents();
        } else {
            $streamEvents = $this->eventStore->loadEventsByMetadataFrom($streamName, ['aggregate_type' => $this->aggregateType->toString(), 'aggregate_id' => $aggregateId]);
        }
        if (!$streamEvents->valid()) {
            return;
        }
        $eventSourcedAggregateRoot = $this->aggregateTranslator->reconstituteAggregateFromHistory($this->aggregateType, $streamEvents);
        //Cache aggregate root in the identity map but without pending events
        $this->identityMap[$aggregateId] = $eventSourcedAggregateRoot;
        return $eventSourcedAggregateRoot;
    }

Usage Example

コード例 #1
0
 /**
  * @test
  */
 public function it_translates_aggregate_back_and_forth()
 {
     $this->eventStore->beginTransaction();
     $user = User::nameNew('John Doe');
     $this->repository->addAggregateRoot($user);
     $this->eventStore->commit();
     $this->eventStore->beginTransaction();
     //Simulate a normal program flow by fetching the AR before modifying it
     $user = $this->repository->getAggregateRoot($user->id());
     $user->changeName('Max Mustermann');
     $this->eventStore->commit();
     $this->resetRepository();
     $loadedUser = $this->repository->getAggregateRoot($user->id());
     $this->assertEquals('Max Mustermann', $loadedUser->name());
     return $loadedUser;
 }
All Usage Examples Of Prooph\EventStore\Aggregate\AggregateRepository::getAggregateRoot