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); }
/** * @param Command $command * @param UnitOfWork $unitOfWork * @param InterceptorChain $interceptorChain * * @return mixed The result of the command handler, if any. */ public function handle(Command $command, UnitOfWork $unitOfWork, InterceptorChain $interceptorChain) { $auditListener = new AuditUnitOfWorkListener($command, $this->auditDataProvider, $this->auditLogger); $unitOfWork->registerListener($auditListener); $result = $interceptorChain->proceed(); $auditListener->setResult($result); return $result; }
/** * @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()); }
/** * @param Command $command * @param CommandCallback $callback * * @throws \Exception */ public function dispatch(Command $command, CommandCallback $callback = null) { $handler = $this->handlerRegistry->findCommandHandlerFor($command); $this->unitOfWork->start(); try { $result = $handler->handle($command); if (null !== $callback) { $callback->onSuccess($result); } } catch (\Exception $exception) { $this->unitOfWork->rollback($exception); if (null !== $callback) { $callback->onFailure($exception); } // Todo: remove this ? throw $exception; } $this->unitOfWork->commit(); }
/** * @param Command $command * @param CommandCallback $callback * * @throws \Exception */ public function dispatch(Command $command, CommandCallback $callback = null) { $command = $this->intercept($command); $handler = $this->handlerRegistry->findCommandHandlerFor($command); $this->unitOfWork->start(); $interceptorChain = new DefaultInterceptorChain($command, $this->unitOfWork, $handler, ...$this->handlerInterceptors); try { $result = $interceptorChain->proceed(); if (null !== $callback) { $callback->onSuccess($result); } } catch (\Exception $exception) { $this->unitOfWork->rollback($exception); if (null !== $callback) { $callback->onFailure($exception); } throw $exception; } $this->unitOfWork->commit(); return $result; }
/** * @param Event $event * * @return Event */ public function onEventRegistration(Event $event) { $this->unitOfWork->publishEvent($event, $this->eventBus); return $this->unitOfWork->invokeEventRegistrationListeners($event); }