private function transform($item, $key)
    {
        $transformed = ($this->transform)($item, $key, $this);

        if (!$transformed instanceof \Generator) {
            throw new EtlException('The transformer must return a generator.');
        }

        // Traverse generator to trigger events
        try {
            $transformed = \iterator_to_array($transformed);
            $this->eventDispatcher->dispatch(new ItemEvent(EtlEvents::TRANSFORM, $item, $key, $this));
        } catch (\Exception $e) {
            /** @var ItemExceptionEvent $event */
            $event = $this->eventDispatcher->dispatch(new ItemExceptionEvent(EtlEvents::TRANSFORM_EXCEPTION, $item, $key, $this, $e));
            if ($event->shouldThrowException()) {
                throw $e;
            }
        }

        return function () use ($transformed) {
            yield from $transformed;
        };
    }