create poll with choices to create

This commit is contained in:
Baptiste Lemoine 2019-11-28 16:18:41 +01:00
parent fc40302aa4
commit 6c0ba2ddd6
3 changed files with 84 additions and 52 deletions

View File

@ -81,10 +81,10 @@ class DefaultController extends AbstractController {
$serializer = SerializerBuilder::create()->build();
$newpoll = $serializer->deserialize( $data, 'App\Entity\Poll', 'json' );
$newpoll->setAdminKey( uniqid() );
$newpoll->setCreationDate( new \DateTime() );
$newpoll->setModificationPolicy( 'none' );
$newpoll
->setAdminKey( $newpoll->generateAdminKey() )
->setCreationDate( new \DateTime() )
->setModificationPolicy( 'none' );
$timeStamp = time() + ( 3600 * 24 * 90 ); // 90 days by default
$newpoll->setExpiracyDate( ( new \DateTime() )->setTimestamp( $timeStamp ),
new \DateTimeZone( 'Europe/Paris' ) );
@ -114,13 +114,13 @@ class DefaultController extends AbstractController {
$em->persist( $foundOwner );
// manage choices
$choices = $data[ 'choices' ];
$choices = $data[ 'choices_to_create' ];
foreach ( $choices as $c ) {
var_dump( $c );
$newChoice = new Choice();
$newChoice->setPoll( $newpoll );
// $newpoll->addChoice( $newChoice );
// $em->persist( $newChoice );
$newChoice
->setPoll( $newpoll )
->setName( $c );
$em->persist( $newChoice );
}
$em->persist( $newpoll );
$em->flush();
@ -130,8 +130,9 @@ class DefaultController extends AbstractController {
}
return $this->json( [
'message' => 'you created a poll ' . $precision,
'data' => $newpoll,
'message' => 'you created a poll ' . $precision,
'data' => $newpoll,
'admin_key' => $newpoll->getAdminKey(),
],
201 );
@ -309,24 +310,8 @@ class DefaultController extends AbstractController {
// find poll from choices
$poll->addStackOfVote( $stack );
//
//
// $foundOwner = $em->findByEmail( $data[ 'owner' ][ 'email' ] );
// manage existing or new Owner
// if ( ! $foundOwner ) {
// $foundOwner = new Owner();
// $foundOwner->setPseudo( $data[ 'owner' ][ 'email' ] )
// ->setEmail( $data[ 'owner' ][ 'email' ] )
// ->setModifierToken( uniqid() );
// }
// $comment->setOwner( $foundOwner )
// ->setPoll( $poll );
// $foundOwner->addComment( $comment );
//
$em->persist( $stack );
$em->persist( $poll );
// $em->persist( $comment );
$em->flush();
return $this->json( [

View File

@ -56,8 +56,6 @@ class Choice {
public function setPoll( ?Poll $poll ): self {
$this->poll = $poll;
// $poll->addChoice( $this );
return $this;
}

View File

@ -140,13 +140,17 @@ class Poll {
*/
public $defaultExpiracyDaysFromNow = 60;
public function __construct() {
$this->adminKey = uniqid();
$this->votes = new ArrayCollection();
$this->stacksOfVotes = new ArrayCollection();
$this->choices = new ArrayCollection();
$this->comments = new ArrayCollection();
$this->stacksOfVotes = new ArrayCollection();
$this->initiate();
}
private function initiate() {
$this->adminKey = $this->generateAdminKey();
$this->setCreationDate( new \DateTime() );
$this->setExpiracyDate( $this->addDaysToDate(
new \DateTime(),
@ -155,6 +159,44 @@ class Poll {
$this->setAllowedAnswers( [ 'yes' ] );
}
public function generateAdminKey() {
$rand = random_int( PHP_INT_MIN, PHP_INT_MAX );
return str_shuffle( md5( $rand ) . $rand . $this->random_str() );
}
/**
* Generate a random string, using a cryptographically secure
* pseudorandom number generator (random_int)
*
* This function uses type hints now (PHP 7+ only), but it was originally
* written for PHP 5 as well.
*
* For PHP 7, random_int is a PHP core function
* For PHP 5.x, depends on https://github.com/paragonie/random_compat
*
* @param int $length How many characters do we want?
* @param string $keyspace A string of all possible characters
* to select from
*
* @return string
*/
public function random_str(
int $length = 64,
string $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
): string {
if ( $length < 1 ) {
throw new \RangeException( "Length must be a positive integer" );
}
$pieces = [];
$max = mb_strlen( $keyspace, '8bit' ) - 1;
for ( $i = 0 ; $i < $length ; ++ $i ) {
$pieces [] = $keyspace[ random_int( 0, $max ) ];
}
return implode( '', $pieces );
}
public function findChoiceById( int $id ) {
error_reporting( E_ALL ^ E_NOTICE );
@ -427,26 +469,6 @@ class Poll {
return $this;
}
public function addChoice( Choice $choice ): self {
if ( ! $this->choices->contains( $choice ) ) {
$this->choices[] = $choice;
$choice->setPoll( $this );
}
return $this;
}
public function removeChoice( Choice $choice ): self {
if ( $this->choices->contains( $choice ) ) {
$this->choices->removeElement( $choice );
// set the owning side to null (unless already changed)
if ( $choice->getPoll() === $this ) {
$choice->setPoll( null );
}
}
return $this;
}
public function getAllowedAnswers(): ?array {
return $this->allowedAnswers;
@ -478,4 +500,31 @@ class Poll {
return $this;
}
public function addChoice( Choice $choice ): self {
if ( ! is_null( $this->choices ) ) {
if ( ! $this->choices->contains( $choice ) ) {
$this->choices[] = $choice;
$choice->setPoll( $this );
}
} else {
$this->choices[] = $choice;
$choice->setPoll( $this );
}
return $this;
}
public function removeChoice( Choice $choice ): self {
if ( $this->choices->contains( $choice ) ) {
$this->choices->removeElement( $choice );
// set the owning side to null (unless already changed)
if ( $choice->getPoll() === $this ) {
$choice->setPoll( null );
}
}
return $this;
}
}