Prooph\EventStore\Aggregate\AggregateRepository::addAggregateRoot PHP Метод

addAggregateRoot() публичный Метод

public addAggregateRoot ( object $eventSourcedAggregateRoot )
$eventSourcedAggregateRoot object
    public function addAggregateRoot($eventSourcedAggregateRoot)
    {
        $this->assertAggregateType($eventSourcedAggregateRoot);
        $domainEvents = $this->aggregateTranslator->extractPendingStreamEvents($eventSourcedAggregateRoot);
        $aggregateId = $this->aggregateTranslator->extractAggregateId($eventSourcedAggregateRoot);
        $streamName = $this->determineStreamName($aggregateId);
        $enrichedEvents = [];
        foreach ($domainEvents as $event) {
            $enrichedEvents[] = $this->enrichEventMetadata($event, $aggregateId);
        }
        if ($this->oneStreamPerAggregate) {
            $stream = new Stream($streamName, new ArrayIterator($enrichedEvents));
            $this->eventStore->create($stream);
        } else {
            $this->eventStore->appendTo($streamName, new ArrayIterator($enrichedEvents));
        }
    }

Usage Example

 /**
  * @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::addAggregateRoot