date-poll-api/src/Controller/DefaultController.php

81 lines
1.7 KiB
PHP
Raw Normal View History

<?php
2020-01-30 11:28:24 +01:00
namespace App\Controller;
2020-01-30 11:28:24 +01:00
use App\Entity\Owner;
2020-04-14 18:09:03 +02:00
use App\Service\MailService;
2020-01-30 11:28:24 +01:00
use FOS\RestBundle\Controller\Annotations\Get;
use FOS\RestBundle\Controller\Annotations\Route;
2020-04-17 12:04:37 +02:00
use JMS\Serializer\Type\Exception\Exception;
2020-01-30 11:28:24 +01:00
use Symfony\Component\HttpFoundation\JsonResponse;
2020-04-17 12:04:37 +02:00
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
2020-01-30 11:28:24 +01:00
/**
* Class DefaultController
* @package App\Controller
* @Route("/api/v1",name="api_")
*/
2020-04-16 17:11:01 +02:00
class DefaultController extends FramadateController {
2020-04-14 18:09:03 +02:00
/**
* @var MailService
*/
2020-04-16 17:11:01 +02:00
protected $mail_service;
2020-01-30 11:28:24 +01:00
/**
* Send a mail with all the data to one user
* @Get(
* path = "/send-polls-to-user/{email}",
* name = "send_user_polls"
* )
*
2020-04-17 12:04:37 +02:00
* @param string $email
2020-01-30 11:28:24 +01:00
*
* @return JsonResponse
*/
2020-04-17 12:04:37 +02:00
public function sendPollsToUserAction( string $email ) {
2020-01-30 11:28:24 +01:00
$repository = $this->getDoctrine()->getRepository( Owner::class );
2020-04-10 18:02:52 +02:00
2020-04-17 12:04:37 +02:00
2020-04-10 18:02:52 +02:00
// find user by email
2020-04-17 12:04:37 +02:00
$owner = $repository->findOneByEmail($email);
2020-01-30 11:28:24 +01:00
2020-04-17 12:04:37 +02:00
if ( $owner ) {
2020-01-30 11:28:24 +01:00
$templateVars = [
2020-04-17 12:04:37 +02:00
'owner' => $owner,
'polls' => $owner->getPolls(),
'title' => 'Mes sondages - ' . $owner->getEmail(),
];
2019-11-28 16:48:35 +01:00
// send email
2020-04-17 12:04:37 +02:00
$mailSent = 0;
try {
$mailSent = $this->sendOwnerPollsAction( $owner );
} catch ( Exception $e ) {
} catch ( TransportExceptionInterface $e ) {
}
if ( $mailSent ) {
2020-04-14 18:13:48 +02:00
return $this->json( [
2020-04-17 12:04:37 +02:00
'message' => 'mail succefully sent to user ' . $owner->getEmail(),
2020-04-14 18:13:48 +02:00
'data' => '',
],
200 );
}
2020-04-17 12:04:37 +02:00
return $this->json( [
'message' => 'no sucess sending email ' . $owner->getEmail(),
'data' => '',
],
400 );
}
2020-04-17 12:04:37 +02:00
return $this->json( [
'message' => 'no user found for email ' . $email,
'data' => '',
],
400 );
2020-01-30 11:28:24 +01:00
}
2019-11-12 11:44:09 +01:00
2020-01-30 11:28:24 +01:00
}