/** * {@inheritdoc} */ public function migrate(EventStream $eventStream) { $events = array(); foreach ($eventStream->events() as $event) { if (!$event instanceof PostTitleWasChanged) { $events[] = $event; } } return new EventStream($eventStream->streamName(), $eventStream->aggregateId(), $events); }
/** * Test Events method. */ public function testEvents() { $this->given($postId = PostId::fromNative(md5(rand())))->and($eventStream = new EventStream('posts', $postId, []))->then()->array($eventStream->events())->isEmpty(); $this->given($postId = PostId::fromNative(md5(rand())))->and($eventStream = new EventStream('posts', $postId, [new PostWasCreated($postId, 'foo', 'bar')]))->then()->array($eventStream->events())->hasSize(1); $this->given($postId = PostId::fromNative(md5(rand())))->then()->exception(function () use($postId) { new EventStream('posts', $postId, ['bar']); })->isInstanceOf(\InvalidArgumentException::class); $this->given($postId = PostId::fromNative(md5(rand())))->then()->exception(function () use($postId) { new EventStream('posts', $postId, [new PostWasCreated(PostId::fromNative(md5(rand())), 'foo', 'bar')]); })->isInstanceOf(\InvalidArgumentException::class); }
/** * {@inheritdoc} */ public function persist(EventStream $eventStream, Version $aggregateVersion, Version $applicationVersion) { $applicationKey = $this->getApplicationKey($applicationVersion); if (!$this->store->containsKey($applicationKey)) { $this->store->set($applicationKey, new ArrayHashMap()); } /** @var ArrayHashMap $applicationCollection */ $applicationCollection = $this->store->get($applicationKey); $streamKey = $this->getStreamKey($eventStream->streamName(), $aggregateVersion); if (!$applicationCollection->containsKey($streamKey)) { $applicationCollection->set($streamKey, new ArrayHashMap()); } /** @var ArrayHashMap $streamCollection */ $streamCollection = $applicationCollection->get($streamKey); $aggregateKey = $eventStream->aggregateId()->toNative(); if (!$streamCollection->containsKey($aggregateKey)) { $streamCollection->set($aggregateKey, new ArrayList()); } /** @var ArrayList $aggregateIdCollection */ $aggregateIdCollection = $streamCollection->get($aggregateKey); foreach ($eventStream->events() as $event) { $aggregateIdCollection->add($event); } }
/** * @param EventStream $history */ public function replay(EventStream $history) { foreach ($history->events() as $event) { $this->applyEvent($event); } }