src/Controller/AlertsController.php line 36

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Scan;
  4. use App\Model\Scan\Report as ScanReport;
  5. use App\Model\Tag;
  6. use DateTime;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  14. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  15. /**
  16.  * Alerts Controller.
  17.  */
  18. class AlertsController extends AbstractController
  19. {
  20.     const DATETIME_FORMAT 'Y-m-d';
  21.     /**
  22.      * Alerts Index.
  23.      *
  24.      * @Route(path="/alerts", name="alerts_index", methods={"GET"})
  25.      *
  26.      * @return Response
  27.      */
  28.     public function indexAction(): Response
  29.     {
  30.         return $this->render('alerts/list.html.twig', [
  31.             'trafficType'  => Scan::FLAG_TRAFFIC_LABELS,
  32.             'tagTypes' => Tag::TYPE_LABELS,
  33.         ]);
  34.     }
  35.     /**
  36.      * Gets Alerts list
  37.      *
  38.      * @Route(path="/alerts/list", name="alerts_list", methods={"GET"})
  39.      *
  40.      * @param Request    $request
  41.      * @param ScanReport $report
  42.      * @return JsonResponse
  43.      */
  44.     public function listAction(Request $requestScanReport $report): JsonResponse
  45.     {
  46.         $from        $request->query->get('from'date("Y-m-d"));
  47.         $to          $request->query->get('to'date("Y-m-d"));
  48.         $showAll     $request->query->get('showAll'0);
  49.         $trafficType $request->query->get('trafficType''');
  50.         $tags        $request->query->get('tags''');
  51.         $status      = [Scan::STATUS_LANDING_ISSUE];
  52.         $trafficType = (!empty($trafficType) || $trafficType === "0") ? explode(','$trafficType) : [];
  53.         $tags        = (!empty($tags) || $tags === "0") ? explode(','$tags) : [];
  54.         $from        DateTime::createFromFormat(self::DATETIME_FORMAT$from);
  55.         $to          DateTime::createFromFormat(self::DATETIME_FORMAT$to);
  56.         $alerts $report->getAlerts($from$to$status$showAll$trafficType$tags);
  57.         return $this->json($alerts);
  58.     }
  59.     /**
  60.      * Update Resolution
  61.      *
  62.      * @Route(path="/alerts/resolution/update", name="resolution_update", methods={"POST"})
  63.      *
  64.      * @param Request                $request
  65.      * @param EntityManagerInterface $entityManager
  66.      * @return JsonResponse
  67.      */
  68.     public function updateResolution(Request $requestEntityManagerInterface $entityManager): JsonResponse
  69.     {
  70.         $resolution $request->get('resolution');
  71.         if (!isset(Scan::STATUS_RESOLUTION_LABELS[$resolution])) {
  72.             throw new BadRequestHttpException(sprintf("Scan with id %s , Error : Invalid resolution"$resolution));
  73.         }
  74.         $scanId $request->get('scanId');
  75.         $scan   $entityManager->find(Scan::class, $scanId);
  76.         if (empty($scan)) {
  77.             throw new NotFoundHttpException('Scan not found!');
  78.         }
  79.         $response = [
  80.             'status'  => true,
  81.             'message' => "Resolution of Scan has been updated",
  82.         ];
  83.         try {
  84.             $scan->setResolution($resolution);
  85.             $entityManager->flush();
  86.         } catch (\Exception $e) {
  87.             $response = [
  88.                 'status'  => false,
  89.                 'message' => $e->getMessage(),
  90.             ];
  91.         }
  92.         return $this->json($response);
  93.     }
  94. }