Select Git revision
NotificationController.php
NotificationController.php 4.12 KiB
<?php
/**
* Created by PhpStorm.
* User: jeremyguiselin
* Date: 23/09/2016
* Time: 08:22
*/
namespace BackendBundle\Controller;
use BackendBundle\Entity\Notification;
use Doctrine\ORM\Tools\Pagination\Paginator;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Exception\ClientException;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
class NotificationController extends Controller
{
const NOTIFICATION_API_URL = 'https://fcm.googleapis.com/fcm/send';
const SERVER_KEY = "AIzaSyCFdGq-rjzznFOEzDifZGsRoeWT8LFAc9s";
const SENDER_ID = "1006878207346";
const DEFAULT_PAGE_RESULT = 20;
/**
* @param Request $request
* @return Response
*
* @Route("/admin/notification/create")
*/
public function createAction(Request $request)
{
$notification = new Notification();
$form = $this->createForm('BackendBundle\\Form\\NotificationType', $notification);
if ($request->getMethod() === Request::METHOD_POST) {
$form->handleRequest($request);
if ($form->isValid()) {
$message = [
'notification' => [
'title' => $notification->getTitle(),
'body' => $notification->getContent()
],
'to' => 'cNgyx3ZgOX4:APA91bF5eigDuJLjj8wI8SC4l5KIHATuxi-2G5whpWI1n1jWpWWwRAwbEXec4fQ2S7HW8EdUPaREXJ5fVd_TZV8rZq_eIJIEhEadeH9wpsbYmUUM7E8H8Y0Qst9KmBuHWodCFFJPJ0vh'
];
$client = new Client();
$response = $client->request('POST', self::NOTIFICATION_API_URL, [
'body' => json_encode($message),
'headers'=> [
'Authorization' => 'key='.self::SERVER_KEY,
'Content-Type' => 'application/json',
]
]);
$response = (array) \GuzzleHttp\json_decode($response->getBody()->getContents());
if (!$response['failure']) {
$notification->setDate(new \DateTime());
$devices = $this->getDoctrine()->getRepository('BackendBundle:Device')->findAll();
$em = $this->getDoctrine()->getEntityManager();
$em->persist($notification);
$this->getDoctrine()->getRepository('BackendBundle:NotificationOnDevice')->updateStatusOnAllDevices($devices, $notification);
$this->addFlash('success', 'La notification a bien été envoyée');
return $this->redirectToRoute('backend_notification_list', ['page' => 1]);
}
$this->addFlash('danger', $response['results'][0]->error);
}
}
return $this->render('@Backend/notification/create.html.twig',[
'form' => $form->createView()
]);
}
/**
* @param Request $request
* @param int $page
* @return Response
*
* @Route("/admin/notification/list/{page}")
*/
public function listAction(Request $request, int $page)
{
$em = $this->getDoctrine()->getManager();
if (!isset($page)) {
$page = 1;
}
$repository = $em->getRepository('BackendBundle:Notification');
$query = $repository->getAll();
$firstResult = $page === 1 ? 0 : self::DEFAULT_PAGE_RESULT * ($page-1) - 1;
$pagination = new Paginator($query);
$totalItems = count($pagination);
$pagesCount = ceil($totalItems / self::DEFAULT_PAGE_RESULT) >= 1 ? ceil($totalItems / self::DEFAULT_PAGE_RESULT) : 1;
$pagination->getQuery()
->setMaxResults(self::DEFAULT_PAGE_RESULT)
->setFirstResult($firstResult)
;
return $this->render(
'BackendBundle:notification:list.html.twig',
[
'elements' => $pagination,
'countPages' => $pagesCount,
'page' => $page
]
);
}
}