public function commit(EventStream $stream)
    {
        $newEvents = $stream->newEvents();

        if (count($newEvents) === 0) {
            return new Transaction($stream, $newEvents);
        }

        $id = (string)$stream->getUuid();
        $currentVersion = $stream->getVersion();
        $nextVersion = $currentVersion + count($newEvents);

        $eventData = isset($this->eventsData[$id])
            ? $this->eventsData[$id]
            : array();

        foreach ($newEvents as $newEvent) {
            $eventData[] = $this->serializer->toArray($newEvent);
        }

        $this->storage->store($id, $stream->getClassName(), $eventData, $nextVersion, $currentVersion);

        $stream->markNewEventsProcessed($nextVersion);

        return new Transaction($stream, $newEvents);
    }