1
0
mirror of https://framagit.org/tykayn/date-poll-api synced 2023-08-25 08:23:11 +02:00
date-poll-api/src/Controller/DefaultController.php

73 lines
1.5 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 Swift_Mailer;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* Class DefaultController
* @package App\Controller
* @Route("/api/v1",name="api_")
*/
class DefaultController extends FramadateController {
/**
* @var MailService
*/
protected $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 );
}
}
}
}