From 6b4994b35ae6c812a0cbf8c1ff9706dbd0b66b5c Mon Sep 17 00:00:00 2001 From: Baptiste Lemoine Date: Wed, 6 Nov 2019 12:27:46 +0100 Subject: [PATCH] :zap: can create poll --- composer.json | 3 +- composer.lock | 320 ++++++++++++-------- config/bundles.php | 1 + config/packages/sensio_framework_extra.yaml | 3 + src/Controller/DefaultController.php | 43 ++- src/Entity/Owner.php | 138 ++++----- src/Entity/Poll.php | 18 +- symfony.lock | 12 + 8 files changed, 334 insertions(+), 204 deletions(-) create mode 100644 config/packages/sensio_framework_extra.yaml diff --git a/composer.json b/composer.json index 244805f..be90708 100644 --- a/composer.json +++ b/composer.json @@ -8,19 +8,20 @@ "friendsofsymfony/rest-bundle": "^2.6", "jms/serializer-bundle": "^3.4", "nelmio/api-doc-bundle": "^3.4", + "sensio/framework-extra-bundle": "^5.5", "symfony/console": "4.3.*", "symfony/dotenv": "4.3.*", "symfony/flex": "^1.3.1", "symfony/form": "4.3.*", "symfony/framework-bundle": "4.3.*", "symfony/intl": "4.3.*", + "symfony/maker-bundle": "^1.14", "symfony/orm-pack": "^1.0", "symfony/validator": "4.3.*", "symfony/yaml": "4.3.*" }, "require-dev": { "doctrine/doctrine-fixtures-bundle": "^3.2", - "symfony/maker-bundle": "^1.14", "symfony/web-server-bundle": "4.3.*" }, "config": { diff --git a/composer.lock b/composer.lock index 8723016..dd25dd7 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "927a6acf21288c4cb4bc4eb7000dddaf", + "content-hash": "fa70b5946a14ecedaa82c3754b1601e7", "packages": [ { "name": "doctrine/annotations", @@ -2489,6 +2489,58 @@ ], "time": "2019-02-18T16:27:10+00:00" }, + { + "name": "nikic/php-parser", + "version": "v4.2.5", + "source": { + "type": "git", + "url": "https://github.com/nikic/PHP-Parser.git", + "reference": "b76bbc3c51f22c570648de48e8c2d941ed5e2cf2" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/b76bbc3c51f22c570648de48e8c2d941ed5e2cf2", + "reference": "b76bbc3c51f22c570648de48e8c2d941ed5e2cf2", + "shasum": "" + }, + "require": { + "ext-tokenizer": "*", + "php": ">=7.0" + }, + "require-dev": { + "ircmaxell/php-yacc": "0.0.4", + "phpunit/phpunit": "^6.5 || ^7.0 || ^8.0" + }, + "bin": [ + "bin/php-parse" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.2-dev" + } + }, + "autoload": { + "psr-4": { + "PhpParser\\": "lib/PhpParser" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Nikita Popov" + } + ], + "description": "A PHP parser written in PHP", + "keywords": [ + "parser", + "php" + ], + "time": "2019-10-25T18:33:07+00:00" + }, { "name": "ocramius/package-versions", "version": "1.4.0", @@ -2901,6 +2953,84 @@ ], "time": "2019-11-01T11:05:21+00:00" }, + { + "name": "sensio/framework-extra-bundle", + "version": "v5.5.1", + "source": { + "type": "git", + "url": "https://github.com/sensiolabs/SensioFrameworkExtraBundle.git", + "reference": "dfc2c4df9f7d465a65c770e9feb578fe071636f7" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sensiolabs/SensioFrameworkExtraBundle/zipball/dfc2c4df9f7d465a65c770e9feb578fe071636f7", + "reference": "dfc2c4df9f7d465a65c770e9feb578fe071636f7", + "shasum": "" + }, + "require": { + "doctrine/annotations": "^1.0", + "php": ">=7.1.3", + "symfony/config": "^4.3|^5.0", + "symfony/dependency-injection": "^4.3|^5.0", + "symfony/framework-bundle": "^4.3|^5.0", + "symfony/http-kernel": "^4.3|^5.0" + }, + "conflict": { + "doctrine/doctrine-cache-bundle": "<1.3.1" + }, + "require-dev": { + "doctrine/doctrine-bundle": "^1.11|^2.0", + "doctrine/orm": "^2.5", + "nyholm/psr7": "^1.1", + "symfony/browser-kit": "^4.3|^5.0", + "symfony/dom-crawler": "^4.3|^5.0", + "symfony/expression-language": "^4.3|^5.0", + "symfony/finder": "^4.3|^5.0", + "symfony/monolog-bridge": "^4.0|^5.0", + "symfony/monolog-bundle": "^3.2", + "symfony/phpunit-bridge": "^4.3.5|^5.0", + "symfony/psr-http-message-bridge": "^1.1", + "symfony/security-bundle": "^4.3|^5.0", + "symfony/twig-bundle": "^4.3|^5.0", + "symfony/yaml": "^4.3|^5.0", + "twig/twig": "^1.34|^2.4|^3.0" + }, + "suggest": { + "symfony/expression-language": "", + "symfony/psr-http-message-bridge": "To use the PSR-7 converters", + "symfony/security-bundle": "" + }, + "type": "symfony-bundle", + "extra": { + "branch-alias": { + "dev-master": "5.5.x-dev" + } + }, + "autoload": { + "psr-4": { + "Sensio\\Bundle\\FrameworkExtraBundle\\": "src/" + }, + "exclude-from-classmap": [ + "/tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + } + ], + "description": "This bundle provides a way to configure your controllers with annotations", + "keywords": [ + "annotations", + "controllers" + ], + "time": "2019-10-16T18:54:45+00:00" + }, { "name": "symfony/cache", "version": "v4.3.6", @@ -4215,6 +4345,74 @@ ], "time": "2019-10-04T21:18:34+00:00" }, + { + "name": "symfony/maker-bundle", + "version": "v1.14.2", + "source": { + "type": "git", + "url": "https://github.com/symfony/maker-bundle.git", + "reference": "5dfcbc400b168943f15a21d4283ad848605d9bc2" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/maker-bundle/zipball/5dfcbc400b168943f15a21d4283ad848605d9bc2", + "reference": "5dfcbc400b168943f15a21d4283ad848605d9bc2", + "shasum": "" + }, + "require": { + "doctrine/inflector": "^1.2", + "nikic/php-parser": "^4.0", + "php": "^7.0.8", + "symfony/config": "^3.4|^4.0|^5.0", + "symfony/console": "^3.4|^4.0|^5.0", + "symfony/dependency-injection": "^3.4|^4.0|^5.0", + "symfony/filesystem": "^3.4|^4.0|^5.0", + "symfony/finder": "^3.4|^4.0|^5.0", + "symfony/framework-bundle": "^3.4|^4.0|^5.0", + "symfony/http-kernel": "^3.4|^4.0|^5.0" + }, + "require-dev": { + "doctrine/doctrine-bundle": "^1.8|^2.0", + "doctrine/orm": "^2.3", + "friendsofphp/php-cs-fixer": "^2.8", + "friendsoftwig/twigcs": "^3.1.2", + "symfony/http-client": "^4.3|^5.0", + "symfony/phpunit-bridge": "^4.3|^5.0", + "symfony/process": "^3.4|^4.0|^5.0", + "symfony/security-core": "^3.4|^4.0|^5.0", + "symfony/yaml": "^3.4|^4.0|^5.0" + }, + "type": "symfony-bundle", + "extra": { + "branch-alias": { + "dev-master": "1.0-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Bundle\\MakerBundle\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code.", + "homepage": "https://symfony.com/doc/current/bundles/SymfonyMakerBundle/index.html", + "keywords": [ + "code generator", + "generator", + "scaffold", + "scaffolding" + ], + "time": "2019-11-04T13:50:13+00:00" + }, { "name": "symfony/mime", "version": "v4.3.6", @@ -5753,126 +5951,6 @@ ], "time": "2019-06-12T12:03:37+00:00" }, - { - "name": "nikic/php-parser", - "version": "v4.2.5", - "source": { - "type": "git", - "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "b76bbc3c51f22c570648de48e8c2d941ed5e2cf2" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/b76bbc3c51f22c570648de48e8c2d941ed5e2cf2", - "reference": "b76bbc3c51f22c570648de48e8c2d941ed5e2cf2", - "shasum": "" - }, - "require": { - "ext-tokenizer": "*", - "php": ">=7.0" - }, - "require-dev": { - "ircmaxell/php-yacc": "0.0.4", - "phpunit/phpunit": "^6.5 || ^7.0 || ^8.0" - }, - "bin": [ - "bin/php-parse" - ], - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "4.2-dev" - } - }, - "autoload": { - "psr-4": { - "PhpParser\\": "lib/PhpParser" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Nikita Popov" - } - ], - "description": "A PHP parser written in PHP", - "keywords": [ - "parser", - "php" - ], - "time": "2019-10-25T18:33:07+00:00" - }, - { - "name": "symfony/maker-bundle", - "version": "v1.14.2", - "source": { - "type": "git", - "url": "https://github.com/symfony/maker-bundle.git", - "reference": "5dfcbc400b168943f15a21d4283ad848605d9bc2" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/maker-bundle/zipball/5dfcbc400b168943f15a21d4283ad848605d9bc2", - "reference": "5dfcbc400b168943f15a21d4283ad848605d9bc2", - "shasum": "" - }, - "require": { - "doctrine/inflector": "^1.2", - "nikic/php-parser": "^4.0", - "php": "^7.0.8", - "symfony/config": "^3.4|^4.0|^5.0", - "symfony/console": "^3.4|^4.0|^5.0", - "symfony/dependency-injection": "^3.4|^4.0|^5.0", - "symfony/filesystem": "^3.4|^4.0|^5.0", - "symfony/finder": "^3.4|^4.0|^5.0", - "symfony/framework-bundle": "^3.4|^4.0|^5.0", - "symfony/http-kernel": "^3.4|^4.0|^5.0" - }, - "require-dev": { - "doctrine/doctrine-bundle": "^1.8|^2.0", - "doctrine/orm": "^2.3", - "friendsofphp/php-cs-fixer": "^2.8", - "friendsoftwig/twigcs": "^3.1.2", - "symfony/http-client": "^4.3|^5.0", - "symfony/phpunit-bridge": "^4.3|^5.0", - "symfony/process": "^3.4|^4.0|^5.0", - "symfony/security-core": "^3.4|^4.0|^5.0", - "symfony/yaml": "^3.4|^4.0|^5.0" - }, - "type": "symfony-bundle", - "extra": { - "branch-alias": { - "dev-master": "1.0-dev" - } - }, - "autoload": { - "psr-4": { - "Symfony\\Bundle\\MakerBundle\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code.", - "homepage": "https://symfony.com/doc/current/bundles/SymfonyMakerBundle/index.html", - "keywords": [ - "code generator", - "generator", - "scaffold", - "scaffolding" - ], - "time": "2019-11-04T13:50:13+00:00" - }, { "name": "symfony/process", "version": "v4.3.6", diff --git a/config/bundles.php b/config/bundles.php index 7b2c26d..943d432 100644 --- a/config/bundles.php +++ b/config/bundles.php @@ -11,4 +11,5 @@ return [ Symfony\Bundle\WebServerBundle\WebServerBundle::class => ['dev' => true], Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['dev' => true, 'test' => true], Nelmio\ApiDocBundle\NelmioApiDocBundle::class => ['all' => true], + Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle::class => ['all' => true], ]; diff --git a/config/packages/sensio_framework_extra.yaml b/config/packages/sensio_framework_extra.yaml new file mode 100644 index 0000000..1821ccc --- /dev/null +++ b/config/packages/sensio_framework_extra.yaml @@ -0,0 +1,3 @@ +sensio_framework_extra: + router: + annotations: false diff --git a/src/Controller/DefaultController.php b/src/Controller/DefaultController.php index 0e99f1e..8968c99 100644 --- a/src/Controller/DefaultController.php +++ b/src/Controller/DefaultController.php @@ -2,10 +2,12 @@ namespace App\Controller; +use App\Entity\Owner; use App\Entity\Poll; use FOS\RestBundle\Controller\Annotations\Get; use FOS\RestBundle\Controller\Annotations\Post; use FOS\RestBundle\Controller\Annotations\Route; +use JMS\Serializer\SerializerBuilder; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; @@ -83,15 +85,46 @@ class DefaultController extends AbstractController { public function newPollAction( Request $request ) { $data = $request->getContent(); - $poll = $this->get( 'jms_serializer' )->deserialize( $data, Poll::class, 'json' ); - $em = $this->getDoctrine()->getManager(); - $em->persist( $poll ); + $serializer = SerializerBuilder::create()->build(); + $newpoll = $serializer->deserialize( $data, 'App\Entity\Poll', 'json' ); + + $newpoll->setAdminKey( uniqid() ); + $newpoll->setCreationDate( new \DateTime() ); + $newpoll->setModificationPolicy( 'none' ); + $timeStamp = time() + ( 3600 * 24 * 90 ); // 90 days by default + $newpoll->setExpiracyDate( ( new \DateTime() )->setTimestamp( $timeStamp ), + new \DateTimeZone( 'Europe/Paris' ) ); + $data = json_decode( $data, true ); +// die( $data ); + $em = $this->getDoctrine()->getRepository( Owner::class ); + $foundOwner = $em->findByEmail( $data[ 'owner' ][ 'email' ] ); + die( $foundOwner ); + $userWasFound = false; + if ( ! $foundOwner ) { + //create a new owner + $owner = new Owner(); + + $owner->setPseudo( $data[ 'owner' ][ 'pseudo' ] ); + $owner->setEmail( $data[ 'owner' ][ 'email' ] ); + $foundOwner = $owner; + } else { + $userWasFound = true; + } + // link the owner and the poll + $newpoll->setOwner( $foundOwner ); + $foundOwner->addPoll( $newpoll ); + $em->persist( $newpoll ); $em->flush(); + $precision = ''; + if ( $userWasFound ) { + $precision = 'from an existing user : ' . $foundOwner->getEmail(); + } return $this->json( [ - 'message' => 'you created a poll', - 'data' => $poll, + 'message' => 'you created a poll ' . $precision, +// 'data' => $jsonContent, + ], 203 ); diff --git a/src/Entity/Owner.php b/src/Entity/Owner.php index 0a4b1f1..f669c44 100644 --- a/src/Entity/Owner.php +++ b/src/Entity/Owner.php @@ -5,96 +5,90 @@ namespace App\Entity; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; +use JMS\Serializer\Annotation as Serializer; /** * @ORM\Entity(repositoryClass="App\Repository\OwnerRepository") */ -class Owner -{ - /** - * @ORM\Id() - * @ORM\GeneratedValue() - * @ORM\Column(type="integer") - */ - private $id; +class Owner { + /** + * @ORM\Id() + * @ORM\GeneratedValue() + * @ORM\Column(type="integer") + */ + private $id; - /** - * @ORM\Column(type="string", length=255) - */ - private $email; + /** + * @ORM\Column(type="string", length=255) + * @Serializer\Type("string") + */ + private $email; - /** - * @ORM\Column(type="string", length=255) - */ - private $pseudo; + /** + * @ORM\Column(type="string", length=255) + * @Serializer\Type("string") + */ + private $pseudo; - /** - * @ORM\OneToMany(targetEntity="App\Entity\Poll", mappedBy="owner") - */ - private $polls; + /** + * @ORM\OneToMany(targetEntity="App\Entity\Poll", mappedBy="owner",cascade={"persist"}) + * @Serializer\Type("App\Entity\Poll") + */ + private $polls; - public function __construct() - { - $this->polls = new ArrayCollection(); - } + public function __construct() { + $this->polls = new ArrayCollection(); + } - public function getId(): ?int - { - return $this->id; - } + public function getId(): ?int { + return $this->id; + } - public function getEmail(): ?string - { - return $this->email; - } + public function getEmail(): ?string { + return $this->email; + } - public function setEmail(string $email): self - { - $this->email = $email; + public function setEmail( string $email ): self { + $this->email = $email; - return $this; - } + return $this; + } - public function getPseudo(): ?string - { - return $this->pseudo; - } + public function getPseudo(): ?string { + return $this->pseudo; + } - public function setPseudo(string $pseudo): self - { - $this->pseudo = $pseudo; + public function setPseudo( string $pseudo ): self { + $this->pseudo = $pseudo; - return $this; - } + return $this; + } - /** - * @return Collection|Poll[] - */ - public function getPolls(): Collection - { - return $this->polls; - } + /** + * @return Collection|Poll[] + */ + public function getPolls(): Collection { + return $this->polls; + } - public function addPoll(Poll $poll): self - { - if (!$this->polls->contains($poll)) { - $this->polls[] = $poll; - $poll->setOwner($this); - } + public function addPoll( Poll $poll ): self { + if ( ! $this->polls->contains( $poll ) ) { + $this->polls[] = $poll; + $poll->setOwner( $this ); + } - return $this; - } + return $this; + } - public function removePoll(Poll $poll): self - { - if ($this->polls->contains($poll)) { - $this->polls->removeElement($poll); - // set the owning side to null (unless already changed) - if ($poll->getOwner() === $this) { - $poll->setOwner(null); - } - } + public function removePoll( Poll $poll ): self { + if ( $this->polls->contains( $poll ) ) { + $this->polls->removeElement( $poll ); + // set the owning side to null (unless already changed) + if ( $poll->getOwner() === $this ) { + $poll->setOwner( null ); + } + } - return $this; - } + return $this; + } } diff --git a/src/Entity/Poll.php b/src/Entity/Poll.php index 16b98b5..7e281d4 100644 --- a/src/Entity/Poll.php +++ b/src/Entity/Poll.php @@ -9,7 +9,6 @@ use JMS\Serializer\Annotation as Serializer; /** * @ORM\Entity(repositoryClass="App\Repository\PollRepository") - * @Serializer\ExclusionPolicy("ALL") */ class Poll { /** @@ -17,17 +16,20 @@ class Poll { * @ORM\GeneratedValue() * @ORM\Column(type="integer") * @Serializer\Expose() + * @Serializer\Type("integer") */ public $id; /** * @ORM\Column(type="string", length=255) * @Serializer\Expose() + * @Serializer\Type("string") */ public $title; /** * @ORM\Column(type="string", length=255, nullable=true) * @Serializer\Expose() + * @Serializer\Type("string") */ private $customUrl; @@ -40,6 +42,7 @@ class Poll { /** * @ORM\Column(type="string", length=1000) * @Serializer\Expose() + * @Serializer\Type("string") */ private $description; @@ -55,21 +58,25 @@ class Poll { private $expiracyDate; /** - * @ORM\ManyToOne(targetEntity="App\Entity\Owner", inversedBy="polls") + * @ORM\ManyToOne(targetEntity="App\Entity\Owner", inversedBy="polls",cascade={"persist"}) * @ORM\JoinColumn(nullable=false) + * @Serializer\Type("App\Entity\Owner") */ private $owner; /** * used to allow administration * @ORM\Column(type="string", length=255) + * @Serializer\Type("string") */ private $adminKey; /** * kind of poll, text or date * @ORM\Column(type="string", length=255) + * @Serializer\Type("string") + * @Serializer\Expose() */ - private $kind; + public $kind; /** * kind of way the people can modify the poll * everybody - can modify votes @@ -77,7 +84,7 @@ class Poll { * nobody - no one can modify the votes (excepted admin), pray to have it right at first * @ORM\Column(type="string", length=255) */ - private $modificationPolicy; + public $modificationPolicy; /** * send a mail on a new comment * @ORM\Column(type="boolean", nullable=true) @@ -100,7 +107,8 @@ class Poll { private $showResultEvenIfPasswords; /** - * @ORM\OneToMany(targetEntity="App\Entity\Vote", mappedBy="poll", orphanRemoval=true) + * @ORM\OneToMany(targetEntity="App\Entity\Vote", mappedBy="poll", orphanRemoval=true,cascade={"persist"}) + * @Serializer\Type("App\Entity\Vote") */ private $votes; diff --git a/symfony.lock b/symfony.lock index 2dd74f1..ecf99f5 100644 --- a/symfony.lock +++ b/symfony.lock @@ -210,6 +210,18 @@ "psr/log": { "version": "1.1.0" }, + "sensio/framework-extra-bundle": { + "version": "5.2", + "recipe": { + "repo": "github.com/symfony/recipes", + "branch": "master", + "version": "5.2", + "ref": "fb7e19da7f013d0d422fa9bce16f5c510e27609b" + }, + "files": [ + "config/packages/sensio_framework_extra.yaml" + ] + }, "symfony/cache": { "version": "v4.3.5" },