From 4176922500bf075db92e88530d42bb174d27088f Mon Sep 17 00:00:00 2001 From: tykayn Date: Fri, 17 Apr 2020 12:04:37 +0200 Subject: [PATCH] update send to user --- composer.json | 1 + composer.lock | 53 ++++++++++++++++++++- config/packages/sensio_framework_extra.yaml | 2 + src/Controller/DefaultController.php | 52 +++++++++++--------- src/Controller/FramadateController.php | 2 + src/Controller/IndexController.php | 4 +- symfony.lock | 3 ++ 7 files changed, 93 insertions(+), 24 deletions(-) diff --git a/composer.json b/composer.json index f2e2d5f..f13b676 100755 --- a/composer.json +++ b/composer.json @@ -16,6 +16,7 @@ "symfony/console": "4.3.*", "symfony/dependency-injection": "4.3.*", "symfony/dotenv": "4.3.*", + "symfony/expression-language": "4.3.*", "symfony/flex": "^1.3.1", "symfony/form": "4.3.*", "symfony/framework-bundle": "4.3.*", diff --git a/composer.lock b/composer.lock index 462194d..4ad4089 100755 --- 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": "3131df97f0b3bc59550652b3fbef5a6c", + "content-hash": "7dc6a4d93376a4f198327681d2f6fae2", "packages": [ { "name": "doctrine/annotations", @@ -3825,6 +3825,57 @@ ], "time": "2019-09-17T09:54:03+00:00" }, + { + "name": "symfony/expression-language", + "version": "v4.3.11", + "source": { + "type": "git", + "url": "https://github.com/symfony/expression-language.git", + "reference": "fcc3e2085e4832f52bed94d72663962f650dfb23" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/expression-language/zipball/fcc3e2085e4832f52bed94d72663962f650dfb23", + "reference": "fcc3e2085e4832f52bed94d72663962f650dfb23", + "shasum": "" + }, + "require": { + "php": "^7.1.3", + "symfony/cache": "~3.4|~4.0", + "symfony/service-contracts": "^1.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.3-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\ExpressionLanguage\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony ExpressionLanguage Component", + "homepage": "https://symfony.com", + "time": "2020-01-04T12:24:57+00:00" + }, { "name": "symfony/filesystem", "version": "v4.3.10", diff --git a/config/packages/sensio_framework_extra.yaml b/config/packages/sensio_framework_extra.yaml index 1821ccc..2288040 100755 --- a/config/packages/sensio_framework_extra.yaml +++ b/config/packages/sensio_framework_extra.yaml @@ -1,3 +1,5 @@ sensio_framework_extra: router: annotations: false + request: + converters: true diff --git a/src/Controller/DefaultController.php b/src/Controller/DefaultController.php index 6a1b85d..40278c2 100755 --- a/src/Controller/DefaultController.php +++ b/src/Controller/DefaultController.php @@ -6,8 +6,9 @@ use App\Entity\Owner; use App\Service\MailService; use FOS\RestBundle\Controller\Annotations\Get; use FOS\RestBundle\Controller\Annotations\Route; -use Swift_Mailer; +use JMS\Serializer\Type\Exception\Exception; use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\Mailer\Exception\TransportExceptionInterface; /** * Class DefaultController @@ -28,43 +29,50 @@ class DefaultController extends FramadateController { * name = "send_user_polls" * ) * - * @param $email - * @param Swift_Mailer $mailer + * @param string $email * * @return JsonResponse */ - public function sendPollsToUser( $email, Swift_Mailer $mailer ) { + public function sendPollsToUserAction( string $email ) { $repository = $this->getDoctrine()->getRepository( Owner::class ); - // find user by email - $founduser = $repository->findOneBy( [ 'email' => $email ] ); - if ( $founduser ) { - $polls = $founduser->getPolls(); + // find user by email + $owner = $repository->findOneByEmail($email); + + if ( $owner ) { $templateVars = [ - 'owner' => $founduser, - 'polls' => $polls, - 'title' => 'Mes sondages - ' . $email, + 'owner' => $owner, + 'polls' => $owner->getPolls(), + 'title' => 'Mes sondages - ' . $owner->getEmail(), ]; // send email - if ( $this->mail_service->sendOwnerPollsAction( $founduser ) ) { + $mailSent = 0; + try { + $mailSent = $this->sendOwnerPollsAction( $owner ); + } catch ( Exception $e ) { + } catch ( TransportExceptionInterface $e ) { + } + + if ( $mailSent ) { return $this->json( [ - 'message' => 'mail succefully sent to user ' . $email, + 'message' => 'mail succefully sent to user ' . $owner->getEmail(), 'data' => '', ], 200 ); - } else { // user not found case - return $this->json( [ - 'message' => 'no user found for email ' . $email, - 'data' => '', - ], - 400 ); } - - + return $this->json( [ + 'message' => 'no sucess sending email ' . $owner->getEmail(), + 'data' => '', + ], + 400 ); } - + return $this->json( [ + 'message' => 'no user found for email ' . $email, + 'data' => '', + ], + 400 ); } diff --git a/src/Controller/FramadateController.php b/src/Controller/FramadateController.php index eccb20b..e7fa2eb 100644 --- a/src/Controller/FramadateController.php +++ b/src/Controller/FramadateController.php @@ -10,6 +10,8 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class FramadateController extends AbstractController { + private $mail_service; + public function __construct( \Swift_Mailer $mailer ) { $this->mail_service = $mailer; } diff --git a/src/Controller/IndexController.php b/src/Controller/IndexController.php index 3d92b6c..45a81c5 100755 --- a/src/Controller/IndexController.php +++ b/src/Controller/IndexController.php @@ -16,6 +16,8 @@ class IndexController extends FramadateController { * name = "get_default") */ public function indexAction() { - return $this->render( 'index.html.twig', [] ); + + return $this->json( ["message"=>"welcome to the framadate api, ask /api/v1/doc.json for endpoints"],200); +// return $this->render( 'index.html.twig', [] ); } } diff --git a/symfony.lock b/symfony.lock index d100fb2..e9a8466 100755 --- a/symfony.lock +++ b/symfony.lock @@ -280,6 +280,9 @@ "symfony/event-dispatcher-contracts": { "version": "v1.1.7" }, + "symfony/expression-language": { + "version": "v4.3.11" + }, "symfony/filesystem": { "version": "v4.3.5" },