/**
  * Creates a closure capable of finalizing state a cloned proxy
  *
  * @param ApiMetadata  $classMetadata
  * @param ApiPersister $persister
  *
  * @return \Closure
  * @throws FetchException
  */
 private function createCloner(ApiMetadata $classMetadata, ApiPersister $persister)
 {
     return function (Proxy $proxy) use($classMetadata, $persister) {
         if ($proxy->__isInitialized()) {
             return;
         }
         $proxy->__setInitialized(true);
         $proxy->__setInitializer(null);
         /** @var EntityMetadata $class */
         $class = $persister->getClassMetadata();
         $identifier = $classMetadata->getIdentifierValues($proxy);
         $original = $persister->loadById($identifier);
         if (null === $original) {
             throw FetchException::notFound();
         }
         foreach ($class->getReflectionClass()->getProperties() as $property) {
             if (!$class->hasField($property->name) && !$class->hasAssociation($property->name)) {
                 continue;
             }
             $property->setAccessible(true);
             $property->setValue($proxy, $property->getValue($original));
         }
     };
 }