From 087fbafd6aafaa76e671fd866c31297601477f76 Mon Sep 17 00:00:00 2001 From: Baptiste Lemoine Date: Thu, 28 Nov 2019 17:00:17 +0100 Subject: [PATCH] :bug: fix vote stack adding --- src/Controller/DefaultController.php | 17 ++++++++--------- src/Entity/Poll.php | 11 ++++++++--- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/Controller/DefaultController.php b/src/Controller/DefaultController.php index c698d44..2b52c1b 100644 --- a/src/Controller/DefaultController.php +++ b/src/Controller/DefaultController.php @@ -329,10 +329,16 @@ class DefaultController extends AbstractController { $stack ->setPseudo( $data[ 'pseudo' ] ) ->setPoll( $poll ); - foreach ( $data[ 'votes' ] as $voteInfo ) { $vote = new Vote(); $foundChoice = $poll->findChoiceById( $voteInfo[ 'choice_id' ] ); + if ( ! $foundChoice ) { + return $this->json( [ + 'message' => 'choice ' . $voteInfo[ 'choice_id' ] . ' was not found', + 'vote_stack' => $stack, + ], + 404 ); + } $vote->setPoll( $poll ) ->setChoice( $foundChoice ) ->setValue( $voteInfo[ 'value' ] ); @@ -350,20 +356,13 @@ class DefaultController extends AbstractController { $em->flush(); return $this->json( [ - 'message' => 'you created a vote', + 'message' => 'you created a vote stack', 'vote_stack' => $stack, 'json_you_sent' => $data, ], 201 ); } - function newVoteAction( Poll $poll ) { - return $this->json( [ - 'message' => 'you voted on the poll', - ], - 201 ); - } - /** * @Delete( diff --git a/src/Entity/Poll.php b/src/Entity/Poll.php index 6de0ad6..442f450 100644 --- a/src/Entity/Poll.php +++ b/src/Entity/Poll.php @@ -143,6 +143,7 @@ class Poll { * @var int */ public $defaultExpiracyDaysFromNow = 60; + private $maxChoicesLimit = 25; public function __construct() { $this->votes = new ArrayCollection(); @@ -203,17 +204,21 @@ class Poll { public function findChoiceById( int $id ) { - error_reporting( E_ALL ^ E_NOTICE ); $choices = $this->getChoices(); - var_dump( count( $choices ) ); + $counter = 0; // there must be something cleaner than this in Doctrine ArrayCollection foreach ( $choices as $choice ) { + $counter ++; + if ( $counter > $this->maxChoicesLimit ) { + throw new \ErrorException( "max number of choices reached for this poll" ); + } if ( $choice && $choice->getId() == $id ) { return $choice; } - return null; } + + return null; } public function addDaysToDate( \DateTime $date, int $days ) {