public function factory($documentClass, DocumentIdentity $identity = null)
    {
        if (!isset($this->prototypes[$documentClass])) {
            // create document
            $document = new $documentClass();

            // fire create event
            $event = new DocumentEvent($document);
            $this->dispatcher->dispatch(IndexerEvents::CREATE_DOCUMENT, $event);

            // cache prototype
            $this->prototypes[$documentClass] = $document;
        }

        $document = clone $this->prototypes[$documentClass];
        $document->set('_document_class', $documentClass);

        if ($identity) {
            $document->setIdentity($identity);
        }

        return $document;
    }