/**
  * {@inheritdoc}
  */
 protected function generatePath(PersistEvent $event)
 {
     $document = $event->getDocument();
     $currentPath = '';
     if ($event->hasParentNode()) {
         $currentPath = $event->getParentNode()->getPath();
     }
     $parentName = $this->getParentName($document);
     return sprintf('%s/%s', $currentPath, $parentName);
 }
 /**
  * It should set the parent document.
  */
 public function testSetParentDocument()
 {
     $this->persistEvent->getDocument()->willReturn($this->document->reveal());
     $this->persistEvent->getLocale()->willReturn('fr');
     $this->metadataFactory->getMetadataForClass(get_class($this->document->reveal()))->willReturn($this->metadata->reveal());
     $this->metadata->getAlias()->willReturn('test');
     $this->nodeManager->createPath('/')->willReturn($this->parentNode->reveal());
     $this->persistEvent->hasParentNode()->shouldBeCalled();
     $this->persistEvent->setParentNode($this->parentNode->reveal())->shouldBeCalled();
     $this->documentManager->find('/test', 'fr')->willReturn($this->parentDocument);
     $this->subscriber->handlePersist($this->persistEvent->reveal());
 }
 /**
  * @param PersistEvent $event
  */
 public function handleSetParentNodeFromDocument(PersistEvent $event)
 {
     $document = $event->getDocument();
     if (!$document instanceof ParentBehavior) {
         return;
     }
     if ($event->hasParentNode()) {
         return;
     }
     $parentDocument = $document->getParent();
     if (!$parentDocument) {
         return;
     }
     $parentNode = $this->inspector->getNode($parentDocument);
     $event->setParentNode($parentNode);
 }
 /**
  * @param PersistEvent $event
  *
  * @throws DocumentManagerException
  */
 public function handlePersist(PersistEvent $event)
 {
     $options = $event->getOptions();
     $this->validateOptions($options);
     $document = $event->getDocument();
     $parentPath = null;
     $nodeName = null;
     if ($options['path']) {
         $parentPath = PathHelper::getParentPath($options['path']);
         $nodeName = PathHelper::getNodeName($options['path']);
     }
     if ($options['parent_path']) {
         $parentPath = $options['parent_path'];
     }
     if ($parentPath) {
         $event->setParentNode($this->resolveParent($parentPath, $options));
     }
     if ($options['node_name']) {
         if (!$event->hasParentNode()) {
             throw new DocumentManagerException(sprintf('The "node_name" option can only be used either with the "parent_path" option ' . 'or when a parent node has been established by a previous subscriber. ' . 'When persisting document: %s', DocumentHelper::getDebugTitle($document)));
         }
         $nodeName = $options['node_name'];
     }
     if (!$nodeName) {
         return;
     }
     if ($event->hasNode()) {
         $this->renameNode($event->getNode(), $nodeName);
         return;
     }
     $node = $this->strategy->createNodeForDocument($document, $event->getParentNode(), $nodeName);
     $event->setNode($node);
 }