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

76 lines
1.6 KiB
PHP
Executable File

<?php
namespace App\Controller;
use App\Entity\Owner;
use App\Service\MailService;
use FOS\RestBundle\Controller\Annotations\Get;
use FOS\RestBundle\Controller\Annotations\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* Class DefaultController
* @package App\Controller
* @Route("/api/v1",name="api_")
*/
class DefaultController extends AbstractController {
/**
* @var MailService
*/
private $mail_service;
public function __construct( MailService $mail_service ) {
$this->mail_service = $mail_service;
}
/**
* 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 );
// find user by email
$founduser = $repository->findOneBy( [ 'email' => $email ] );
if ( $founduser ) {
$polls = $founduser->getPolls();
$templateVars = [
'owner' => $founduser,
'polls' => $polls,
'title' => 'Mes sondages - ' . $email,
];
// send email
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 );
}
}
}
}