/** * @return UserId */ public function todoId() { if (!$this->todoId) { $this->todoId = TodoId::fromString($this->payload['todo_id']); } return $this->todoId; }
/** * @return TodoId */ public function todoId() { if (is_null($this->todoId)) { $this->todoId = TodoId::fromString($this->aggregateId()); } return $this->todoId; }
/** * @test */ public function it_adds_a_deadline_to_todo() { $todoId = TodoId::generate(); $userId = UserId::generate(); $deadline = TodoDeadline::fromString('2047-12-31 12:00:00', '2047-12-01 12:00:00'); $todo = Todo::post('Do something tomorrow', $userId, $todoId); $this->assertNull($todo->deadline()); $todo->addDeadline($userId, $deadline); $events = $this->popRecordedEvent($todo); $this->assertEquals(2, count($events)); $this->assertInstanceOf(DeadlineWasAddedToTodo::class, $events[1]); $expectedPayload = ['todo_id' => $todoId->toString(), 'user_id' => $userId->toString(), 'deadline' => $deadline->toString()]; $this->assertEquals($expectedPayload, $events[1]->payload()); return $todo; }
function get_todo_version(Todo $todo) { $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);
/** * @return TodoId */ public function todoId() { return TodoId::fromString($this->payload['todo_id']); }
<?php /** * This script looks for todos with open reminders and reminds the assignees */ namespace { use Prooph\ProophessorDo\Model\Todo\Command\RemindTodoAssignee; use Prooph\ProophessorDo\Model\Todo\TodoId; use Prooph\ProophessorDo\Model\Todo\TodoReminder; use Prooph\ProophessorDo\Projection\Todo\TodoReminderFinder; use Prooph\ServiceBus\CommandBus; chdir(dirname(__DIR__)); // Setup autoloading require 'vendor/autoload.php'; $container = (require 'config/container.php'); $commandBus = $container->get(CommandBus::class); $todoReminderFinder = $container->get(TodoReminderFinder::class); $todoReminder = $todoReminderFinder->findOpen(); if (!$todoReminder) { echo "Nothing to do. Exiting.\n"; exit; } foreach ($todoReminder as $reminder) { echo "Send reminder for Todo with id {$reminder->todo_id}.\n"; $commandBus->dispatch(RemindTodoAssignee::forTodo(TodoId::fromString($reminder->todo_id), TodoReminder::fromString($reminder->reminder, $reminder->status))); } echo "Done!\n"; }
/** * @test */ public function it_throws_an_exception_when_marking_a_completed_todo_as_expired() { $todoId = TodoId::generate(); $userId = UserId::generate(); $deadline = TodoDeadline::fromString('2047-12-31 12:00:00'); $todo = Todo::post('Do something fun', $userId, $todoId); $todo->addDeadline($userId, $deadline); $todo->markAsDone(); $this->setExpectedException(TodoNotOpen::class, 'Tried to expire todo with status done.'); $todo->markAsExpired(); }
/** * @return string representation of the unique identifier of the aggregate root */ protected function aggregateId() { return $this->todoId->toString(); }
public function getEvent($todoId) { return TodoWasMarkedAsExpired::fromStatus(TodoId::fromString($todoId), TodoStatus::fromString(TodoStatus::OPEN), TodoStatus::fromString(TodoStatus::EXPIRED)); }
/** * @test * @return Todo */ public function it_adds_a_reminder_to_todo() { $todoId = TodoId::generate(); $userId = UserId::generate(); $reminder = TodoReminder::fromString('2047-12-31 12:00:00'); $todo = Todo::post('Do something tomorrow', $userId, $todoId); $this->popRecordedEvent($todo); $this->assertNull($todo->reminder()); $todo->addReminder($userId, $reminder); $events = $this->popRecordedEvent($todo); $this->assertCount(1, $events); $this->assertInstanceOf(ReminderWasAddedToTodo::class, $events[0]); $expectedPayload = ['todo_id' => $todoId->toString(), 'user_id' => $userId->toString(), 'reminder' => $reminder->toString()]; $this->assertEquals($expectedPayload, $events[0]->payload()); return $todo; }
{ $commandCount = NUMBER_OF_USERS * NUMBER_OF_TODO_PER_USER + NUMBER_OF_USERS; return $duration / $commandCount; } $container = (require 'config/container.php'); $commandBus = $container->get(\Prooph\ServiceBus\CommandBus::class); $openTodos = []; $stopWatch = new Stopwatch(); $stopWatch->start('generate_model'); for ($i = 1; $i <= NUMBER_OF_USERS; $i++) { $userId = UserId::generate(); $username = getUserName($i); $email = $username . '@acme.com'; $commandBus->dispatch(RegisterUser::withData($userId->toString(), $username, $email)); for ($j = 0; $j < NUMBER_OF_TODO_PER_USER; $j++) { $todoId = TodoId::generate(); $commandBus->dispatch(PostTodo::forUser($userId->toString(), randTodoText(), $todoId->toString())); $openTodos[] = $todoId->toString(); } echo "User: "******"(" . $userId->toString() . ") was registered\n"; } $generatedEvent = $stopWatch->stop('generate_model'); echo "\nModel generated in: " . $generatedEvent->getDuration() . " ms\n"; echo "Command execution average: " . calcCommandAverage($generatedEvent->getDuration()) . " ms\n"; echo "\nGoing to close todo randomly now\n"; foreach ($openTodos as $openTodoId) { $close = rand(0, 1); if ($close) { $commandBus->dispatch(MarkTodoAsDone::forTodo($openTodoId)); } }
/** * @param TodoId $todoId * @return TodoNotFound */ public static function withTodoId(TodoId $todoId) { return new self(sprintf('Todo with id %s cannot be found.', $todoId->toString())); }
/** * @param TodoId $todoId * @return Todo */ public function get(TodoId $todoId) { return $this->getAggregateRoot($todoId->toString()); }
/** * @param TodoId $other * @return bool */ public function sameValueAs(TodoId $other) { return $this->toString() === $other->toString(); }