/**
  * @param Request $request
  * @return JsonResponse
  */
 public function indexAction(Request $request, $formatName = "default")
 {
     $payload = file_get_contents('php://input');
     $content = "queryString: " . $request->getQueryString() . "\n payload: " . $payload;
     file_put_contents("/tmp/koalamon/webhook_" . $formatName . ".log", json_encode($content));
     $project = $this->getProject($request->get("api_key"));
     if ($project == null) {
         return $this->getJsonRespone(self::STATUS_FAILURE, "No project with api_key " . $request->get("api_key") . ' found.');
     }
     $rawEvent = $this->getFormatHandler()->run($formatName, $request, $payload);
     if ($rawEvent === false) {
         return $this->getJsonRespone(self::STATUS_SKIPPED);
     }
     $event = new Event();
     $event->setStatus($rawEvent->getStatus());
     $event->setMessage($rawEvent->getMessage());
     $event->setSystem($rawEvent->getSystem());
     $event->setType($rawEvent->getType());
     $event->setUnique($rawEvent->isUnique());
     $event->setUrl($rawEvent->getUrl());
     $event->setValue($rawEvent->getValue());
     $em = $this->getDoctrine()->getManager();
     $identifier = $em->getRepository('BauerIncidentDashboardCoreBundle:EventIdentifier')->findOneBy(array('project' => $project, 'identifier' => $rawEvent->getIdentifier()));
     if (is_null($identifier)) {
         $identifier = new EventIdentifier();
         $identifier->setProject($project);
         $identifier->setIdentifier($rawEvent->getIdentifier());
         $em->persist($identifier);
         $em->flush();
     }
     $event->setEventIdentifier($identifier);
     $translatedEvent = $this->translate($event);
     ProjectHelper::addEvent($this->get("Router"), $em, $translatedEvent);
     return $this->getJsonRespone(self::STATUS_SUCCESS);
 }
 public function closeAction(Event $event)
 {
     $this->assertUserRights(UserRole::ROLE_COLLABORATOR);
     $closeEvent = new Event();
     $closeEvent->setEventIdentifier($event->getEventIdentifier());
     $closeEvent->setSystem($event->getSystem());
     $closeEvent->setStatus(Event::STATUS_SUCCESS);
     $closeEvent->setIsStatusChange(true);
     $closeEvent->setUnique($event->isUnique());
     $closeEvent->setType($event->getType());
     $closeEvent->setMessage('Manually closed by ' . $this->getUser()->getUsername() . '.');
     ProjectHelper::addEvent($this->get("Router"), $this->getDoctrine()->getEntityManager(), $closeEvent);
     return $this->redirect($this->generateUrl("bauer_incident_dashboard_core_homepage", array("project" => $event->getEventIdentifier()->getProject()->getIdentifier())));
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $em = $this->getContainer()->get('doctrine')->getManager();
     $tools = $em->getRepository('BauerIncidentDashboardCoreBundle:Tool')->findBy(["active" => true]);
     foreach ($tools as $tool) {
         if (!is_null($tool->getInterval()) && $tool->getInterval() != 0) {
             $eventIdentifier = $em->getRepository('BauerIncidentDashboardCoreBundle:EventIdentifier')->findOneBy(["project" => $tool->getProject(), "tool" => $tool], ['lastEvent' => 'desc']);
             if (is_null($eventIdentifier)) {
                 continue;
             }
             $event = $eventIdentifier->getLastEvent();
             $interval = $tool->getInterval() + 1;
             $now = new \DateTime();
             $lastEventLimit = $now->sub(new \DateInterval("PT" . $interval . "M"));
             $newEvent = new Event();
             $identifierString = 'koalamon_tool_intervalcheck_' . $tool->getIdentifier();
             $identifier = $em->getRepository('BauerIncidentDashboardCoreBundle:EventIdentifier')->findOneBy(array("project" => $tool->getProject(), "identifier" => $identifierString));
             if (is_null($identifier)) {
                 $identifier = new EventIdentifier();
                 $identifier->setProject($tool->getProject());
                 $identifier->setIdentifier($identifierString);
                 $em->persist($identifier);
                 $em->flush();
             }
             $newEvent->setEventIdentifier($identifier);
             $newEvent->setSystem('Koalamon');
             $newEvent->setType('koalamon_intervalcheck');
             $newEvent->setUnique(false);
             if ($event->getCreated() < $lastEventLimit) {
                 $newEvent->setStatus(Event::STATUS_FAILURE);
                 $message = "The tool '" . $tool->getName() . "' did not send any events since " . $event->getCreated()->format("d.m.Y H:i:m") . ".";
                 $newEvent->setMessage($message);
                 $output->writeln("project_id: " . $event->getEventIdentifier()->getProject()->getId() . " -- " . $message);
             } else {
                 $newEvent->setStatus(Event::STATUS_SUCCESS);
                 $newEvent->setMessage("");
             }
             ProjectHelper::addEvent($this->getContainer()->get("Router"), $em, $newEvent);
         }
     }
 }