src/Controller/NotificationsController.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Notification;
  4. use App\Repository\NotificationsRepository;
  5. use App\Services\NotificationService;
  6. use Carbon\Carbon;
  7. use Doctrine\ORM\EntityNotFoundException;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  11. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  12. use Symfony\Contracts\Translation\TranslatorInterface;
  13. use Twig\Environment;
  14. class NotificationsController extends DictionaryController
  15. {
  16.     /**
  17.      * @var NotificationService
  18.      */
  19.     protected $notificationService;
  20.     /**
  21.      * @var NotificationsRepository
  22.      */
  23.     protected $notificationsRepository;
  24.     public function __construct(Environment $twig,
  25.                                 TranslatorInterface $translator,
  26.                                 AuthenticationUtils $authenticationUtils,
  27.                                 SessionInterface $session,
  28.                                 NotificationService $notificationService,
  29.                                 NotificationsRepository $notificationsRepository)
  30.     {
  31.         parent::__construct($twig$translator$authenticationUtils$session);
  32.         $this->notificationService $notificationService;
  33.         $this->notificationsRepository $notificationsRepository;
  34.     }
  35.     public function getNotifications(Request $request)
  36.     {
  37.         $customer $this->get('security.token_storage')->getToken()->getUser();
  38.         $notifications $this->notificationsRepository->getNotificationsForUser($customer);
  39.         foreach ($notifications as $key => $notification) {
  40.             $notifications[$key]['timeAgo'] = Carbon::createFromTimeStamp($notification['createdAt']->getTimestamp())->locale('pl_PL')->diffForHumans();
  41.         }
  42.         $count count($notifications);
  43.         if($request->isXmlHttpRequest()) {
  44.             $html $this->getTwig()->render('notifications.html.twig', ['notifications' => $notifications]);
  45.             return new JsonResponse(['html' => $html'countNotifications' => $count]);
  46.         }
  47.         return $this->render('notifications.html.twig', ['notifications' => $notifications]);
  48.     }
  49.     public function markAsRead($idRequest $request)
  50.     {
  51.         try {
  52.             $this->notificationService->markAsRead($id$this->getUser());
  53.         } catch (EntityNotFoundException $e) {
  54.             return new JsonResponse(['status' => 'ERROR''message' => $e->getMessage()]);
  55.         } catch (\Exception $exception) {
  56.             return new JsonResponse(['status' => 'ERROR''message' => $exception->getMessage()]);
  57.         }
  58.         return new JsonResponse(['status' => 'OK''id' => $id], 200);
  59.     }
  60.     public function markAsUnRead($idRequest $request)
  61.     {
  62.         try {
  63.             $this->notificationService->markAsUnRead($id$this->getUser());
  64.         } catch (EntityNotFoundException $e) {
  65.             return new JsonResponse(['status' => 'ERROR''message' => $e->getMessage()]);
  66.         } catch (\Exception $exception) {
  67.             return new JsonResponse(['status' => 'ERROR''message' => $exception->getMessage()]);
  68.         }
  69.         return new JsonResponse(['status' => 'OK''id' => $id], 200);
  70.     }
  71.     public function markAllAsRead()
  72.     {
  73.         try {
  74.             $this->notificationService->markAllAsRead($this->getUser());
  75.         } catch (EntityNotFoundException $e) {
  76.             return new JsonResponse(['status' => 'ERROR''message' => $e->getMessage()]);
  77.         } catch (\Exception $exception) {
  78.             return new JsonResponse(['status' => 'ERROR''message' => $exception->getMessage()]);
  79.         }
  80.         return new JsonResponse(['status' => 'OK''refreshPage' => true], 200);
  81.     }
  82.     public function getConfiguration()
  83.     {
  84.         return [];
  85.     }
  86. }