private function createAKeyword($label, KeyWord $parent = null)
 {
     $keyword = new KeyWord();
     $keyword->setKeyWord($label);
     if ($parent instanceof KeyWord) {
         $keyword->setParent($parent);
     }
     $this->em->persist($keyword);
     $this->em->flush($keyword);
     return $keyword;
 }
 /**
  * Create a keyword object
  * and if a parent is provided add the keyword as its last child.
  *
  * @param KeyWord $keyword
  *
  * @Rest\RequestParam(name="keyword", description="Keyword value", requirements={
  *   @Assert\NotBlank()
  * })
  * @Rest\ParamConverter(
  *   name="parent", id_name="parent_uid", id_source="request", class="BackBee\NestedNode\KeyWord", required=false
  * )
  * @Rest\Security("is_fully_authenticated() & has_role('ROLE_API_USER')")
  */
 public function postAction(Request $request, $parent = null)
 {
     try {
         $keyWordLabel = trim($request->request->get('keyword'));
         $uid = $request->request->get('uid', null);
         if (null !== $uid) {
             $keywordItem = $this->getKeywordRepository()->find($uid);
             $keywordItem->setKeyWord($keyWordLabel);
         } else {
             $keywordItem = new KeyWord();
             $keywordItem->setKeyWord($keyWordLabel);
             if (null === $parent) {
                 $parent = $this->getKeywordRepository()->getRoot();
             }
             if ($this->keywordAlreadyExists($keyWordLabel)) {
                 throw new BadRequestHttpException('KEYWORD_ALREADY_EXISTS');
             }
             $keywordItem->setParent($parent);
             $this->getKeywordRepository()->insertNodeAsLastChildOf($keywordItem, $parent);
         }
         $this->getEntityManager()->persist($keywordItem);
         $this->getEntityManager()->flush();
         $response = $this->createJsonResponse(null, 201, ['BB-RESOURCE-UID' => $keywordItem->getUid(), 'Location' => $this->getApplication()->getRouting()->getUrlByRouteName('bb.rest.keyword.get', ['version' => $request->attributes->get('version'), 'uid' => $keywordItem->getUid()], '', false)]);
     } catch (\Exception $e) {
         $response = $this->createErrorResponse($e);
     }
     return $response;
 }