Exemplo n.º 1
0
 /**
  * Call addNotification when click on a request
  * 
  * @param unknown $template
  * @param unknown $revision
  */
 public function addNotification($templateId, $revision, $environment)
 {
     $out = false;
     try {
         $em = $this->doctrine->getManager();
         /** @var RevisionRepository $repository */
         $repository = $em->getRepository('AppBundle:Template');
         /** @var Template $template */
         $template = $repository->findOneById($templateId);
         if (!$template) {
             throw new NotFoundHttpException('Unknown template');
         }
         $notification = new Notification();
         $notification->setStatus('pending');
         $em = $this->doctrine->getManager();
         /** @var NotificationRepository $repository */
         $repository = $em->getRepository('AppBundle:Notification');
         $alreadyPending = $repository->findBy(['templateId' => $template, 'revisionId' => $revision, 'environmentId' => $environment, 'status' => 'pending']);
         if (!empty($alreadyPending)) {
             /**@var Notification $alreadyPending*/
             $alreadyPending = $alreadyPending[0];
             $this->session->getFlashBag()->add('warning', 'Another notification ' . $template . ' is already pending for ' . $revision . ' in ' . $environment . ' by ' . $alreadyPending->getUsername());
             return;
         }
         $notification->setTemplateId($template);
         $sentTimestamp = new \DateTime();
         $notification->setSentTimestamp($sentTimestamp);
         $notification->setEnvironmentId($environment);
         $notification->setRevisionId($revision);
         $userName = $this->userService->getCurrentUser()->getUserName();
         $notification->setUsername($userName);
         $em->persist($notification);
         $em->flush();
         try {
             $this->sendEmail($notification);
         } catch (\Exception $e) {
         }
         $this->session->getFlashBag()->add('notice', '<i class="' . $template->getIcon() . '"></i> ' . $template->getName() . ' for ' . $revision . ' in ' . $environment->getName());
         $out = true;
     } catch (\Exception $e) {
         $this->session->getFlashBag()->add('error', '<i class="fa fa-ban"></i> An error occured while sending a notification');
         $this->logger->err('An error occured: ' . $e->getMessage());
     }
     return $out;
 }