aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ApiBundle
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/ApiBundle')
-rw-r--r--src/Wallabag/ApiBundle/Controller/WallabagRestController.php28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
index 6dd03c1b..ed31c536 100644
--- a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
+++ b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
@@ -420,6 +420,8 @@ class WallabagRestController extends FOSRestController
420 ->getRepository('WallabagCoreBundle:Entry') 420 ->getRepository('WallabagCoreBundle:Entry')
421 ->removeTag($this->getUser()->getId(), $tag); 421 ->removeTag($this->getUser()->getId(), $tag);
422 422
423 $this->cleanOrphanTag($tag);
424
423 $json = $this->get('serializer')->serialize($tag, 'json'); 425 $json = $this->get('serializer')->serialize($tag, 'json');
424 426
425 return (new JsonResponse())->setJson($json); 427 return (new JsonResponse())->setJson($json);
@@ -460,6 +462,8 @@ class WallabagRestController extends FOSRestController
460 ->getRepository('WallabagCoreBundle:Entry') 462 ->getRepository('WallabagCoreBundle:Entry')
461 ->removeTags($this->getUser()->getId(), $tags); 463 ->removeTags($this->getUser()->getId(), $tags);
462 464
465 $this->cleanOrphanTag($tags);
466
463 $json = $this->get('serializer')->serialize($tags, 'json'); 467 $json = $this->get('serializer')->serialize($tags, 'json');
464 468
465 return (new JsonResponse())->setJson($json); 469 return (new JsonResponse())->setJson($json);
@@ -484,6 +488,8 @@ class WallabagRestController extends FOSRestController
484 ->getRepository('WallabagCoreBundle:Entry') 488 ->getRepository('WallabagCoreBundle:Entry')
485 ->removeTag($this->getUser()->getId(), $tag); 489 ->removeTag($this->getUser()->getId(), $tag);
486 490
491 $this->cleanOrphanTag($tag);
492
487 $json = $this->get('serializer')->serialize($tag, 'json'); 493 $json = $this->get('serializer')->serialize($tag, 'json');
488 494
489 return (new JsonResponse())->setJson($json); 495 return (new JsonResponse())->setJson($json);
@@ -506,6 +512,28 @@ class WallabagRestController extends FOSRestController
506 } 512 }
507 513
508 /** 514 /**
515 * Remove orphan tag in case no entries are associated to it.
516 *
517 * @param Tag|array $tags
518 */
519 private function cleanOrphanTag($tags)
520 {
521 if (!is_array($tags)) {
522 $tags = [$tags];
523 }
524
525 $em = $this->getDoctrine()->getManager();
526
527 foreach ($tags as $tag) {
528 if (count($tag->getEntries()) === 0) {
529 $em->remove($tag);
530 }
531 }
532
533 $em->flush();
534 }
535
536 /**
509 * Validate that the first id is equal to the second one. 537 * Validate that the first id is equal to the second one.
510 * If not, throw exception. It means a user try to access information from an other user. 538 * If not, throw exception. It means a user try to access information from an other user.
511 * 539 *