/**
  * Generate a node with a unique name.
  *
  * @param  NodeType    $nodeType
  * @param  Translation $translation
  * @param  Node|null   $parent
  * @param  Tag|null    $tag
  * @param  boolean     $pushToTop
  *
  * @return RZ\Roadiz\Core\Entities\NodesSources
  */
 public function generate(NodeType $nodeType, Translation $translation, Node $parent = null, Tag $tag = null, $pushToTop = false)
 {
     $name = $nodeType->getDisplayName() . " " . uniqid();
     $node = new Node($nodeType);
     $node->setParent($parent);
     $node->setNodeName($name);
     if (null !== $tag) {
         $node->addTag($tag);
     }
     $this->entityManager->persist($node);
     if ($pushToTop) {
         $node->setPosition(0.5);
     }
     $sourceClass = "GeneratedNodeSources\\" . $nodeType->getSourceEntityClassName();
     $source = new $sourceClass($node, $translation);
     $source->setTitle($name);
     $this->entityManager->persist($source);
     $this->entityManager->flush();
     return $source;
 }
 /**
  * Update current node-type using a new one.
  *
  * Update diff will update only non-critical fields such as :
  *
  * * description
  * * displayName
  *
  * It will only create absent node-type fields won't delete fields
  * not to lose any data.
  *
  * This method does not flush ORM. You'll need to manually call it.
  *
  * @param RZ\Roadiz\Core\Entities\NodeType $newNodeType
  *
  * @throws \RuntimeException If newNodeType param is null
  */
 public function diff(NodeType $newNodeType)
 {
     if (null !== $newNodeType) {
         /*
          * options
          */
         if ("" != $newNodeType->getDisplayName()) {
             $this->nodeType->setDisplayName($newNodeType->getDisplayName());
         }
         if ("" != $newNodeType->getDescription()) {
             $this->nodeType->setDescription($newNodeType->getDescription());
         }
         /*
          * make fields diff
          */
         $existingFieldsNames = $this->nodeType->getFieldsNames();
         foreach ($newNodeType->getFields() as $newField) {
             if (false === in_array($newField->getName(), $existingFieldsNames)) {
                 /*
                  * Field does not exist in type,
                  * creating it.
                  */
                 $newField->setNodeType($this->nodeType);
                 Kernel::getService('em')->persist($newField);
             }
         }
     } else {
         throw new \RuntimeException("New node-type is null", 1);
     }
 }