/** * @param mixed $id * * @return Contact */ public function findById($id) { $events = $this->store->fetchEventsById($id); $entity = $this->eventity->replay($events); Assertion::isInstanceOf($entity, Contact::class); return $entity; }
use Generic\WebFramework\Request; use Generic\Database\SQLDriver; use Eventity\Eventity; use Eventity\EventStoreListener; use EventStore\EventStore; use AddressBook\Denormaliser\Denormaliser; use AddressBook\Repository\ContactRepository; use AddressBook\Query\ListContacts; use AddressBook\Query\ViewContact; use AddressBook\Command\CreateContact; use AddressBook\Command\ChangeContactEmail; use AddressBook\Command\DeleteContact; $app = new Application(); $queryDB = new SQLDriver(); $eventStore = new EventStore(); $eventity = Eventity::getInstance(); $denormaliser = new Denormaliser($queryDB); $eventity->registerListener(new EventStoreListener($eventStore)); $eventity->registerListener($denormaliser); $contactRepository = new ContactRepository($eventStore, $eventity); $app->get('/contacts', function () use($queryDB) { $query = new ListContacts($queryDB); return $query->execute(); }); $app->get('/contacts/:id', function ($id) use($queryDB) { $query = new ViewContact($queryDB, $id); return $query->execute(); }); $app->post('/contacts', function (Request $request) use($eventity) { $command = new CreateContact($eventity->getFactoryFor(Contact::class), $request->getPost('name'), $request->getPost('email')); return $command->execute();