diff --git a/src/Controller/api/v1/VoteController.php b/src/Controller/api/v1/VoteController.php index b07979a..a2a1830 100644 --- a/src/Controller/api/v1/VoteController.php +++ b/src/Controller/api/v1/VoteController.php @@ -44,17 +44,25 @@ class VoteController extends EmailsController { string $custom_url, Request $request, ChoiceRepository $choice_repository - ) { + ) { + /*** + * checks before persisting + */ $em = $this->getDoctrine()->getManager(); $emPol = $em->getRepository( Poll::class ); $poll = $emPol->findOneByCustomUrl( $custom_url ); + // check : existence of poll if ( ! $poll ) { return $this->json( [ 'message' => 'poll "' . $custom_url . '" not found' ], 404 ); } - + // check : limit of number of participation max + if ( count( $poll->getStacksOfVotes() ) == $poll->getVotesMax() ) { + return $this->json( [ 'message' => 'poll "' . $custom_url . '" not allowed to have more stack of votes than ' . $poll->getVotesMax() ], + 403 ); + } $data = $request->getContent(); $data = json_decode( $data, true ); @@ -63,32 +71,30 @@ class VoteController extends EmailsController { $owner = new Owner(); $owner - ->addPoll( $poll ) - - ; + ->addPoll( $poll ); $newStack = new StackOfVotes(); $newStack - ->setPoll($poll) + ->setPoll( $poll ) ->setIp( $_SERVER[ 'REMOTE_ADDR' ] ) ->setPseudo( $data[ "pseudo" ] ) ->setOwner( $owner ); $owner - ->setPseudo($data['owner'][ "pseudo" ]) - ->setPseudo($data['owner'][ "email" ]) - ->addStackOfVote($newStack); + ->setPseudo( $data[ 'owner' ][ "pseudo" ] ) + ->setPseudo( $data[ 'owner' ][ "email" ] ) + ->addStackOfVote( $newStack ); // TODO manage new comment - $emChoice = $choice_repository; + $emChoice = $choice_repository; $newComment = new Comment(); - $newComment->setPseudo($data ['pseudo']) - ->setPoll($poll) - ->setText($data['comment']); - $owner->addComment($newComment); + $newComment->setPseudo( $data [ 'pseudo' ] ) + ->setPoll( $poll ) + ->setText( $data[ 'comment' ] ); + $owner->addComment( $newComment ); - $em->persist($newComment); + $em->persist( $newComment ); foreach ( $data[ 'votes' ] as $vote ) { - if(!$vote[ 'value' ]){ + if ( ! $vote[ 'value' ] ) { continue; } $newVote = new Vote();