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

76 lines
1.6 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;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
2020-01-30 11:28:24 +01:00
/**
* Class DefaultController
* @package App\Controller
* @Route("/api/v1",name="api_")
*/
class DefaultController extends AbstractController {
2020-04-14 18:09:03 +02:00
/**
* @var MailService
*/
private $mail_service;
2020-04-14 18:13:48 +02:00
public function __construct( MailService $mail_service ) {
2020-04-14 18:09:03 +02:00
$this->mail_service = $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"
* )
*
* @param $email
* @param \Swift_Mailer $mailer
*
* @return JsonResponse
*/
public function sendPollsToUser( $email, \Swift_Mailer $mailer ) {
$repository = $this->getDoctrine()->getRepository( Owner::class );
2020-04-10 18:02:52 +02:00
// find user by email
2020-04-14 18:09:03 +02:00
$founduser = $repository->findOneBy( [ 'email' => $email ] );
2020-01-30 11:28:24 +01:00
if ( $founduser ) {
$polls = $founduser->getPolls();
$templateVars = [
'owner' => $founduser,
'polls' => $polls,
2020-04-14 18:09:03 +02:00
'title' => 'Mes sondages - ' . $email,
];
2019-11-28 16:48:35 +01:00
// send email
2020-04-14 18:13:48 +02:00
if ( $this->mail_service->sendOwnerPollsAction( $founduser ) ) {
return $this->json( [
'message' => 'mail succefully sent to user ' . $email,
'data' => '',
],
200 );
} else { // user not found case
return $this->json( [
'message' => 'no user found for email ' . $email,
'data' => '',
],
400 );
}
2020-04-14 18:09:03 +02:00
}
2019-11-27 21:46:35 +01:00
2020-01-30 11:28:24 +01:00
}
2019-11-12 11:44:09 +01:00
2020-01-30 11:28:24 +01:00
}