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

81 lines
1.7 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 JMS\Serializer\Type\Exception\Exception;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
/**
* 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 string $email
*
* @return JsonResponse
*/
public function sendPollsToUserAction( string $email ) {
$repository = $this->getDoctrine()->getRepository( Owner::class );
// find user by email
$owner = $repository->findOneByEmail($email);
if ( $owner ) {
$templateVars = [
'owner' => $owner,
'polls' => $owner->getPolls(),
'title' => 'Mes sondages - ' . $owner->getEmail(),
];
// send email
$mailSent = 0;
try {
$mailSent = $this->sendOwnerPollsAction( $owner );
} catch ( Exception $e ) {
} catch ( TransportExceptionInterface $e ) {
}
if ( $mailSent ) {
return $this->json( [
'message' => 'mail succefully sent to user ' . $owner->getEmail(),
'data' => '',
],
200 );
}
return $this->json( [
'message' => 'no sucess sending email ' . $owner->getEmail(),
'data' => '',
],
400 );
}
return $this->json( [
'message' => 'no user found for email ' . $email,
'data' => '',
],
400 );
}
}