Use this factory when aggregate type equals to aggregate root class
The factory makes sure that the aggregate root class exists.
public static fromAggregateRootClass ( string $aggregateRootClass ) : |
||
$aggregateRootClass | string | |
return |
protected function setUp() { parent::setUp(); $this->repository = new AggregateRepository($this->eventStore, AggregateType::fromAggregateRootClass('Prooph\\EventStoreTest\\Mock\\User'), new ConfigurableAggregateTranslator()); $this->eventStore->beginTransaction(); $this->eventStore->create(new Stream(new StreamName('event_stream'), [])); $this->eventStore->commit(); }
/** * @param TakeSnapshot $command * @throws Exception\RuntimeException */ public function __invoke(TakeSnapshot $command) { $aggregateType = $command->aggregateType(); if (!isset($this->aggregateRepositories[$aggregateType])) { throw new Exception\RuntimeException(sprintf('No repository for aggregate type %s configured', $command->aggregateType())); } $repository = $this->aggregateRepositories[$aggregateType]; $aggregateRoot = $repository->getAggregateRoot($command->aggregateId()); if (null === $aggregateRoot) { throw new RuntimeException(sprintf('Could not find aggregate root %s with id %s', $aggregateType, $command->aggregateId())); } $this->snapshotStore->save(new Snapshot(AggregateType::fromAggregateRootClass($aggregateType), $command->aggregateId(), $aggregateRoot, $repository->extractAggregateVersion($aggregateRoot), $command->createdAt())); }
/** * @test */ public function an_invokable_plugin_is_loaded_by_plugin_manager_and_attached_to_event_store_by_configuration() { $pluginManager = new ServiceManager(new Config(["invokables" => ["eventlogger" => EventLoggerPlugin::class]])); $eventStore = new EventStore(new InMemoryAdapter(), new ProophActionEventEmitter()); $logger = $pluginManager->get('eventlogger'); $logger->setUp($eventStore); $repository = new AggregateRepository($eventStore, AggregateType::fromAggregateRootClass('ProophTest\\EventStore\\Mock\\User'), new ConfigurableAggregateTranslator(), null, null, true); $eventStore->beginTransaction(); $user = User::create("Alex", "*****@*****.**"); $repository->addAggregateRoot($user); $eventStore->commit(); $loggedStreamEvents = $pluginManager->get("eventlogger")->getLoggedStreamEvents(); $this->assertEquals(1, count($loggedStreamEvents)); }
/** * @param ContainerInterface $container * @throws ConfigurationException * @return AggregateRepository */ public function __invoke(ContainerInterface $container) { $config = $container->get('config'); $config = $this->options($config); $repositoryClass = $config['repository_class']; if (!class_exists($repositoryClass)) { throw ConfigurationException::configurationError(sprintf('Repository class %s cannot be found', $repositoryClass)); } if (!is_subclass_of($repositoryClass, AggregateRepository::class)) { throw ConfigurationException::configurationError(sprintf('Repository class %s must be a sub class of %s', $repositoryClass, AggregateRepository::class)); } $eventStore = $container->get(EventStore::class); $aggregateType = AggregateType::fromAggregateRootClass($config['aggregate_type']); $aggregateTranslator = $container->get($config['aggregate_translator']); $snapshotStore = isset($config['snapshot_store']) ? $container->get($config['snapshot_store']) : null; $streamName = isset($config['stream_name']) ? new StreamName($config['stream_name']) : null; $oneStreamPerAggregate = isset($config['one_stream_per_aggregate']) ? (bool) $config['one_stream_per_aggregate'] : false; return new $repositoryClass($eventStore, $aggregateType, $aggregateTranslator, $snapshotStore, $streamName, $oneStreamPerAggregate); }
public function setUp() { $inMemoryAdapter = new InMemoryAdapter(); $eventEmitter = new ProophActionEventEmitter(); $this->eventStore = new EventStore($inMemoryAdapter, $eventEmitter); $this->repository = new AggregateRepository($this->eventStore, AggregateType::fromAggregateRootClass('ProophTest\\EventStore\\Mock\\User'), new ConfigurableAggregateTranslator()); $this->result = []; $self = $this; $router = new CommandRouter(); $router->route(TakeSnapshot::class)->to(function (TakeSnapshot $command) use($self) { $self->result[] = ['aggregate_type' => $command->aggregateType(), 'aggregate_id' => $command->aggregateId()]; }); $commandBus = new CommandBus(); $commandBus->utilize($router); $plugin = new SnapshotPlugin($commandBus, 2); $plugin->setUp($this->eventStore); $this->eventStore->beginTransaction(); $this->eventStore->create(new Stream(new StreamName('event_stream'), new \ArrayIterator())); $this->eventStore->commit(); }
/** * @test */ public function it_publishes_take_snapshot_commands_for_all_known_aggregates() { $inMemoryAdapter = new InMemoryAdapter(); $eventEmitter = new ProophActionEventEmitter(); $eventStore = new EventStore($inMemoryAdapter, $eventEmitter); $repository = new AggregateRepository($eventStore, AggregateType::fromAggregateRootClass('ProophTest\\EventStore\\Mock\\User'), new ConfigurableAggregateTranslator()); $result = []; $router = new CommandRouter(); $router->route(TakeSnapshot::class)->to(function (TakeSnapshot $command) use(&$result) { $result[] = ['aggregate_type' => $command->aggregateType(), 'aggregate_id' => $command->aggregateId()]; }); $commandBus = new CommandBus(); $commandBus->utilize($router); $plugin = new SnapshotPlugin($commandBus, 2); $plugin->setUp($eventStore); $eventStore->beginTransaction(); $eventStore->create(new Stream(new StreamName('event_stream'), new \ArrayIterator())); $eventStore->commit(); $eventStore->beginTransaction(); $user = User::create('Alex', '*****@*****.**'); $repository->addAggregateRoot($user); $eventStore->commit(); $eventStore->beginTransaction(); $user = $repository->getAggregateRoot($user->getId()->toString()); $user->changeName('John'); $user->changeName('Jane'); $user->changeName('Jim'); $eventStore->commit(); $eventStore->beginTransaction(); $eventWithoutMetadata1 = UsernameChanged::with(['new_name' => 'John Doe'], 5); $eventWithoutMetadata2 = UsernameChanged::with(['new_name' => 'Jane Doe'], 6); $eventStore->appendTo(new StreamName('event_stream'), new \ArrayIterator([$eventWithoutMetadata1, $eventWithoutMetadata2])); $eventStore->commit(); $this->assertCount(2, $result); $this->assertArrayHasKey('aggregate_type', $result[0]); $this->assertArrayHasKey('aggregate_id', $result[0]); $this->assertArrayHasKey('aggregate_type', $result[1]); $this->assertArrayHasKey('aggregate_id', $result[1]); $this->assertEquals(User::class, $result[0]['aggregate_type']); $this->assertEquals(User::class, $result[1]['aggregate_type']); }
/** * @param AggregateType $repositoryAggregateType * @param Message[] $streamEvents * @throws Exception\RuntimeException * @return AggregateType */ public function getAggregateRootType(AggregateType $repositoryAggregateType, array &$streamEvents) { if (count($streamEvents)) { $first = $streamEvents[0]; $metadata = $first->metadata(); if (isset($metadata['aggregate_type'])) { return AggregateType::fromAggregateRootClass($metadata['aggregate_type']); } } throw new Exception\RuntimeException("The aggregate type cannot be detected"); }
/** * @param EventStore $eventStore */ public function __construct(EventStore $eventStore) { parent::__construct($eventStore, new AggregateTranslator(), new SingleStreamStrategy($eventStore, 'link_process_manager_stream'), AggregateType::fromAggregateRootClass('Prooph\\Link\\ProcessManager\\Model\\MessageHandler')); }
/** * @param EventStore $eventStore * @param string $storyLogStreamName * @param SnapshotStore $snapshotStore */ public function __construct(EventStore $eventStore, $storyLogStreamName = 'prooph_story_stream', SnapshotStore $snapshotStore = null) { parent::__construct($eventStore, AggregateType::fromAggregateRootClass(Story::class), new AggregateTranslator(), new SingleStreamStrategy($eventStore, $storyLogStreamName), $snapshotStore); }
/** * @param ContainerInterface $container * @return EventStoreTodoList */ public function __invoke(ContainerInterface $container) { return new EventStoreTodoList($container->get('prooph.event_store'), AggregateType::fromAggregateRootClass(Todo::class), new AggregateTranslator()); }
use Prooph\ProophessorDo\Model\Todo\TodoId; use Symfony\Component\Stopwatch\Stopwatch; chdir(dirname(__DIR__)); // Setup autoloading require 'vendor/autoload.php'; $container = (require 'config/container.php'); array_shift($argv); if (empty($argv)) { echo "[1;31mMissing todo id parameter![0m\n"; exit(1); } $todoId = $argv[0]; try { $todoId = TodoId::fromString($todoId); } catch (\Exception $ex) { echo "[1;31mInvalid todo id given![0m\n"; exit(1); } //Set up todo repository by hand to make sure that no snapshot store is used $eventStore = $container->get(EventStore::class); $todoList = new EventStoreTodoList($eventStore, AggregateType::fromAggregateRootClass(Todo::class), new AggregateTranslator()); $stopWatch = new Stopwatch(); $stopWatch->start('load_todo'); $todo = $todoList->get($todoId); if (null === $todo) { echo "[1;31mTodo could not be found![0m\n"; exit(1); } $loadTodoEvent = $stopWatch->stop('load_todo'); echo "Todo was loaded in [1m" . $loadTodoEvent->getDuration() . "[0m ms\n"; }
use Prooph\ProophessorDo\Model\Todo\TodoId; use Symfony\Component\Stopwatch\Stopwatch; chdir(dirname(__DIR__)); // Setup autoloading require 'vendor/autoload.php'; $container = (require 'config/container.php'); array_shift($argv); if (empty($argv)) { echo "[1;31mMissing todo id parameter![0m\n"; exit(1); } $todoId = $argv[0]; try { $todoId = TodoId::fromString($todoId); } catch (\Exception $ex) { echo "[1;31mInvalid todo id given![0m\n"; exit(1); } //Set up todo repository by hand to make sure that no snapshot store is used $eventStore = $container->get(EventStore::class); $todoList = new EventStoreTodoList($eventStore, AggregateType::fromAggregateRootClass(Todo::class), new AggregateTranslator(), $container->get(SnapshotStore::class)); $stopWatch = new Stopwatch(); $stopWatch->start('load_todo'); $todo = $todoList->get($todoId); if (null === $todo) { echo "[1;31mTodo could not be found![0m\n"; exit(1); } $loadTodoEvent = $stopWatch->stop('load_todo'); echo "Todo was loaded in [1m" . $loadTodoEvent->getDuration() . "[0m ms\n"; }
public function __construct(EventStore $eventStore) { //We inject a Prooph\EventSourcing\EventStoreIntegration\AggregateTranslator that can handle our AggregateRoots parent::__construct($eventStore, AggregateType::fromAggregateRootClass('My\\Model\\User'), new AggregateTranslator(), null, null, true); }
/** * @test */ public function it_delegates_to_string() { $type = AggregateType::fromAggregateRootClass('stdClass'); $this->assertEquals('stdClass', (string) $type); }
protected function prepareSnapshotStoreAggregateRepository() { parent::setUp(); $this->snapshotStore = new SnapshotStore(new InMemoryAdapter()); $this->repository = new AggregateRepository($this->eventStore, AggregateType::fromAggregateRootClass('ProophTest\\EventStore\\Mock\\User'), new ConfigurableAggregateTranslator(), $this->snapshotStore); $this->eventStore->beginTransaction(); $this->eventStore->create(new Stream(new StreamName('event_stream'), new \ArrayIterator())); $this->eventStore->commit(); }
/** * @param EventStore $eventStore */ public function __construct(EventStore $eventStore) { $aggregateType = AggregateType::fromAggregateRootClass('Prooph\\Processing\\Processor\\Process'); parent::__construct($eventStore, new AggregateTranslator(), new SingleStreamStrategy($eventStore, 'prooph_processing_stream'), $aggregateType); }
/** * @param EventStore $eventStore */ public function __construct(EventStore $eventStore) { parent::__construct($eventStore, new AggregateTranslator(), new SingleStreamStrategy($eventStore, 'link_process_manager_stream'), AggregateType::fromAggregateRootClass(Task::class)); }
/** * @test */ public function it_uses_callback_to_convert_message_into_custom_domain_event() { $historyEvent = $this->prophesize(Message::class); $historyEvents = [$historyEvent->reveal()]; $translator = new ConfigurableAggregateTranslator(null, null, null, null, function (Message $message) { return ['custom' => 'domainEvent']; }); $ar = $translator->reconstituteAggregateFromHistory(AggregateType::fromAggregateRootClass(DefaultAggregateRoot::class), $historyEvents); $this->assertEquals([['custom' => 'domainEvent']], $ar->getHistoryEvents()); }
protected function resetRepository() { $this->repository = new AggregateRepository($this->eventStore, AggregateType::fromAggregateRootClass('ProophTest\\EventSourcing\\Mock\\User'), new AggregateTranslator()); }
/** * @param TakeSnapshot $command */ public function __invoke(TakeSnapshot $command) { $repository = $this->aggregateRepositories[$command->aggregateType()]; $aggregateRoot = $repository->getAggregateRoot($command->aggregateId()); $this->snapshotAdapter->add(new Snapshot(AggregateType::fromAggregateRootClass($command->aggregateType()), $command->aggregateId(), $aggregateRoot, $command->version(), $command->createdAt())); }
/** * @param string $aggregateRootClass * @param array $events * @return object */ protected function reconstituteAggregateFromHistory($aggregateRootClass, array $events) { return $this->getAggregateTranslator()->reconstituteAggregateFromHistory(AggregateType::fromAggregateRootClass($aggregateRootClass), $events); }
public function create(string $repositoryClass, EventStore $eventStore, string $aggregateType, AggregateTranslator $aggregateTranslator, SnapshotStore $snapshotStore = null, string $streamName = null, bool $oneStreamPerAggregate = false) { return new $repositoryClass($eventStore, AggregateType::fromAggregateRootClass($aggregateType), $aggregateTranslator, $snapshotStore, $streamName ? new StreamName($streamName) : null, $oneStreamPerAggregate); }