/**
  * @param UploadedFile $file
  * @param Photo        $photo
  */
 public function uploadPhoto(UploadedFile $file, Photo &$photo)
 {
     $fileName = time() . '.' . $file->guessExtension();
     $file->move($this->getUploadDir(), $fileName);
     $photo->setName($fileName);
     $photo->setOriginalName($file->getClientOriginalName());
 }
 /**
  * @param Photo $photo
  * @param array $tagsArray
  */
 public function updateTags(Photo &$photo, array $tagsArray = [])
 {
     /** @var \Doctrine\ORM\EntityRepository $repository */
     $repository = $this->entityManager->getRepository('ApiBundle:Tag');
     /** @var Tag[] $tagsEntities */
     $tagsEntities = $repository->createQueryBuilder('t')->where('t.name IN (:tags)')->setParameter('tags', $tagsArray)->getQuery()->execute();
     foreach ($tagsEntities as $tagEntity) {
         $photo->addTag($tagEntity);
         $keyOnArray = array_search($tagEntity->getName(), $tagsArray);
         unset($tagsArray[$keyOnArray]);
     }
     foreach ($tagsArray as $tag) {
         $newTag = new Tag();
         $newTag->setName($tag);
         $photo->addTag($newTag);
         $this->entityManager->persist($newTag);
     }
 }
 /**
  * Delete an existing Photo entity.
  *
  * @Route("/api/photo/{id}",
  *     name="api_photo_delete",
  *     requirements={"id": "\d+"}
  * )
  * @Method("DELETE")
  *
  * @param Photo $photo
  *
  * @return JsonResponse
  */
 public function deleteAction(Photo $photo)
 {
     $name = $photo->getName();
     try {
         $this->getDoctrine()->getManager()->remove($photo);
         $this->getDoctrine()->getManager()->flush();
         unlink($this->getUploadDir() . '/' . $name);
         return $this->get('app.json_response.handler')->createSuccessResponse();
     } catch (\Exception $exception) {
         return $this->get('app.json_response.handler')->createErrorResponse('Error deleting photo');
     }
 }