Use this factory when aggregate type should be detected based on given aggregate root
public static fromAggregateRoot ( mixed $eventSourcedAggregateRoot ) : |
||
$eventSourcedAggregateRoot | mixed | |
return |
/** * @test */ public function it_delegates_on_creating_from_aggregate_root_when_it_implements_aggregate_type_provider() { $expected = new \stdClass(); $aggregateRoot = $this->prophesize(AggregateTypeProvider::class); $aggregateRoot->aggregateType()->willReturn($expected)->shouldBeCalled(); $this->assertSame($expected, AggregateType::fromAggregateRoot($aggregateRoot->reveal())); }
/** * @param AggregateType $repositoryAggregateType * @param string $aggregateId * @param Message[] $streamEvents * @param object $aggregateRoot * @throws Exception\InvalidArgumentException * @return void */ public function appendEvents(AggregateType $repositoryAggregateType, $aggregateId, array $streamEvents, $aggregateRoot) { $arType = AggregateType::fromAggregateRoot($aggregateRoot); if (!$repositoryAggregateType->equals($arType)) { throw new Exception\InvalidArgumentException(sprintf('aggregate root mismatch between repository type %s and object type %s', $repositoryAggregateType->toString(), $arType->toString())); } $this->eventStore->appendTo($this->buildStreamName($repositoryAggregateType, $aggregateId), $streamEvents); }
/** * @param AggregateType $repositoryAggregateType * @param string $aggregateId * @param Message[] $streamEvents * @param object $aggregateRoot * @throws Exception\InvalidArgumentException * @return void */ public function appendEvents(AggregateType $repositoryAggregateType, $aggregateId, array $streamEvents, $aggregateRoot) { $arType = AggregateType::fromAggregateRoot($aggregateRoot); if (!$repositoryAggregateType->equals($arType)) { throw new Exception\InvalidArgumentException(sprintf('aggregate root mismatch between repository type %s and object type %s', $repositoryAggregateType->toString(), $arType->toString())); } $streamName = $this->buildStreamName($repositoryAggregateType); Assertion::string($aggregateId, 'AggregateId needs to be string'); foreach ($streamEvents as &$streamEvent) { $streamEvent = $streamEvent->withAddedMetadata('aggregate_id', $aggregateId); } $this->eventStore->appendTo($streamName, $streamEvents); }
$todoReflected = new \ReflectionClass($todo); $versionProp = $todoReflected->getProperty('version'); $versionProp->setAccessible(true); return $versionProp->getValue($todo); } $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); } /** @var $todoList TodoList */ $todoList = $container->get(TodoList::class); $todo = $todoList->get($todoId); if (null === $todo) { echo "[1;31mTodo could not be found![0m\n"; exit(1); } /** @var $snapshotStore SnapshotStore */ $snapshotStore = $container->get(SnapshotStore::class); $snapshot = new Snapshot(AggregateType::fromAggregateRoot($todo), $todoId->toString(), $todo, get_todo_version($todo), new \DateTimeImmutable("now", new \DateTimeZone('UTC'))); $snapshotStore->save($snapshot); echo "Snapshot was taken!\n"; }
/** * @test * @expectedException \InvalidArgumentException */ public function it_does_not_allow_to_add_stream_events_with_wrong_repository_aggregate_type() { $this->eventStore->beginTransaction(); $user = User::create("John Doe", "*****@*****.**"); $aggregateType = AggregateType::fromAggregateRoot($user); $aggregateId = Uuid::uuid4()->toString(); $streamEvents = [UserCreated::with(['user_id' => $aggregateId], 1)]; $this->strategy->addEventsForNewAggregateRoot($aggregateType, $aggregateId, $streamEvents, $user); $this->eventStore->commit(); $this->eventStore->beginTransaction(); $streamEvents = [UsernameChanged::with(['name' => 'John Doe'], 2)]; $this->strategy->appendEvents(AggregateType::fromString("Product"), $aggregateId, $streamEvents, $user); }