/** * @param AggregateRootIdentifier $aggregateRootIdentifier * @param int $expectedVersion * * @return AggregateRoot * * @throws AggregateNotFoundException */ public function load(AggregateRootIdentifier $aggregateRootIdentifier, $expectedVersion = null) { $eventStream = $this->eventStore->read($this->aggregateRootType, $aggregateRootIdentifier); if ($eventStream->isEmpty()) { throw new AggregateNotFoundException($aggregateRootIdentifier); } $className = $this->aggregateRootType->className(); $aggregateRoot = $className::loadFromHistory($eventStream); $actualVersion = $aggregateRoot->lastCommittedEventSequenceNumber(); if (null !== $expectedVersion && $actualVersion !== $expectedVersion) { throw new ConflictingAggregateVersionException($aggregateRootIdentifier, $actualVersion, $expectedVersion); } return $this->unitOfWork->registerAggregate($aggregateRoot, $this->eventBus, $this->createSaveAggregateCallback()); }
public function it_can_register_a_new_aggregate_root(AggregateRoot $aggregateRoot, $eventBus, $saveAggregateCallbackFactory, SaveAggregateCallback $saveAggregateCallback, UnitOfWork $unitOfWork) { $this->setSaveAggregateCallbackFactory($saveAggregateCallbackFactory); $aggregateRoot->lastCommittedEventSequenceNumber()->willReturn(0); $saveAggregateCallbackFactory->create()->willReturn($saveAggregateCallback); $unitOfWork->registerAggregate($aggregateRoot, $eventBus, $saveAggregateCallback)->shouldBeCalledTimes(1); $this->add($aggregateRoot); }