aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJeremy Benoist <jeremy.benoist@gmail.com>2017-06-28 08:15:06 +0200
committerJeremy Benoist <jeremy.benoist@gmail.com>2017-06-28 08:15:06 +0200
commit39ffaba3232b6378f47883615c58a9ffba668af3 (patch)
tree503e5d360ba4b5e3845a30a814737ae9e8003467
parent18696f77fdc76f7055d84460d459051a6bb7253a (diff)
downloadwallabag-39ffaba3232b6378f47883615c58a9ffba668af3.tar.gz
wallabag-39ffaba3232b6378f47883615c58a9ffba668af3.tar.zst
wallabag-39ffaba3232b6378f47883615c58a9ffba668af3.zip
Return null instead of false
-rw-r--r--src/Wallabag/ApiBundle/Controller/EntryRestController.php30
-rw-r--r--tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php6
2 files changed, 30 insertions, 6 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
index 793b91ba..9786c4d3 100644
--- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php
+++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
@@ -4,6 +4,7 @@ namespace Wallabag\ApiBundle\Controller;
4 4
5use Hateoas\Configuration\Route; 5use Hateoas\Configuration\Route;
6use Hateoas\Representation\Factory\PagerfantaFactory; 6use Hateoas\Representation\Factory\PagerfantaFactory;
7use JMS\Serializer\SerializationContext;
7use Nelmio\ApiDocBundle\Annotation\ApiDoc; 8use Nelmio\ApiDocBundle\Annotation\ApiDoc;
8use Symfony\Component\HttpKernel\Exception\HttpException; 9use Symfony\Component\HttpKernel\Exception\HttpException;
9use Symfony\Component\HttpFoundation\Request; 10use Symfony\Component\HttpFoundation\Request;
@@ -21,6 +22,8 @@ class EntryRestController extends WallabagRestController
21 * Return ID if entry(ies) exist (and if you give the return_id parameter). 22 * Return ID if entry(ies) exist (and if you give the return_id parameter).
22 * Otherwise it returns false. 23 * Otherwise it returns false.
23 * 24 *
25 * @todo Remove that `return_id` in the next major release
26 *
24 * @ApiDoc( 27 * @ApiDoc(
25 * parameters={ 28 * parameters={
26 * {"name"="return_id", "dataType"="string", "required"=false, "format"="1 or 0", "description"="Set 1 if you want to retrieve ID in case entry(ies) exists, 0 by default"}, 29 * {"name"="return_id", "dataType"="string", "required"=false, "format"="1 or 0", "description"="Set 1 if you want to retrieve ID in case entry(ies) exists, 0 by default"},
@@ -46,7 +49,7 @@ class EntryRestController extends WallabagRestController
46 ->getRepository('WallabagCoreBundle:Entry') 49 ->getRepository('WallabagCoreBundle:Entry')
47 ->findByUrlAndUserId($url, $this->getUser()->getId()); 50 ->findByUrlAndUserId($url, $this->getUser()->getId());
48 51
49 $results[$url] = $res instanceof Entry ? ($returnId ? $res->getId() : true) : false; 52 $results[$url] = $this->returnExistInformation($res, $returnId);
50 } 53 }
51 54
52 return $this->sendResponse($results); 55 return $this->sendResponse($results);
@@ -63,7 +66,7 @@ class EntryRestController extends WallabagRestController
63 ->getRepository('WallabagCoreBundle:Entry') 66 ->getRepository('WallabagCoreBundle:Entry')
64 ->findByUrlAndUserId($url, $this->getUser()->getId()); 67 ->findByUrlAndUserId($url, $this->getUser()->getId());
65 68
66 $exists = $res instanceof Entry ? ($returnId ? $res->getId() : true) : false; 69 $exists = $this->returnExistInformation($res, $returnId);
67 70
68 return $this->sendResponse(['exists' => $exists]); 71 return $this->sendResponse(['exists' => $exists]);
69 } 72 }
@@ -621,7 +624,11 @@ class EntryRestController extends WallabagRestController
621 */ 624 */
622 private function sendResponse($data) 625 private function sendResponse($data)
623 { 626 {
624 $json = $this->get('serializer')->serialize($data, 'json'); 627 // https://github.com/schmittjoh/JMSSerializerBundle/issues/293
628 $context = new SerializationContext();
629 $context->setSerializeNull(true);
630
631 $json = $this->get('serializer')->serialize($data, 'json', $context);
625 632
626 return (new JsonResponse())->setJson($json); 633 return (new JsonResponse())->setJson($json);
627 } 634 }
@@ -698,4 +705,21 @@ class EntryRestController extends WallabagRestController
698 // entry saved, dispatch event about it! 705 // entry saved, dispatch event about it!
699 $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); 706 $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
700 } 707 }
708
709 /**
710 * Return information about the entry if it exist and depending on the id or not.
711 *
712 * @param Entry|null $entry
713 * @param bool $returnId
714 *
715 * @return bool|int
716 */
717 private function returnExistInformation($entry, $returnId)
718 {
719 if ($returnId) {
720 return $entry instanceof Entry ? $entry->getId() : null;
721 }
722
723 return $entry instanceof Entry ? true : false;
724 }
701} 725}
diff --git a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
index dc21bd32..c0391d87 100644
--- a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
+++ b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
@@ -800,7 +800,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
800 $this->assertArrayHasKey($url1, $content); 800 $this->assertArrayHasKey($url1, $content);
801 $this->assertArrayHasKey($url2, $content); 801 $this->assertArrayHasKey($url2, $content);
802 $this->assertSame(2, $content[$url1]); 802 $this->assertSame(2, $content[$url1]);
803 $this->assertSame(false, $content[$url2]); 803 $this->assertNull($content[$url2]);
804 } 804 }
805 805
806 public function testGetEntriesExistsWithManyUrlsReturnBool() 806 public function testGetEntriesExistsWithManyUrlsReturnBool()
@@ -815,8 +815,8 @@ class EntryRestControllerTest extends WallabagApiTestCase
815 815
816 $this->assertArrayHasKey($url1, $content); 816 $this->assertArrayHasKey($url1, $content);
817 $this->assertArrayHasKey($url2, $content); 817 $this->assertArrayHasKey($url2, $content);
818 $this->assertEquals(true, $content[$url1]); 818 $this->assertTrue($content[$url1]);
819 $this->assertEquals(false, $content[$url2]); 819 $this->assertFalse($content[$url2]);
820 } 820 }
821 821
822 public function testGetEntriesExistsWhichDoesNotExists() 822 public function testGetEntriesExistsWhichDoesNotExists()