<?php
namespace App\Controller;
use App\Model\Target\Report as TargetReport;
use App\Entity\Scan;
use App\Model\Tag;
use DateTime;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
/**
* Projects controller.
*/
class TargetsController extends AbstractController
{
const DATETIME_FORMAT = 'Y-m-d';
/**
* Show all projects.
*
* @Route(path="", name="targets_index", methods={"GET"})
*
* @return Response
*/
public function indexAction(): Response
{
return $this->render('targets/list.html.twig', [
'trafficType' => Scan::FLAG_TRAFFIC_LABELS,
'statusLabels' => Scan::STATUS_LABELS,
'tagTypes' => Tag::TYPE_LABELS,
]);
}
/**
* Gets targets list
*
* @Route(path="/targets/list", name="targets_list", methods={"GET"})
*
* @param Request $request
* @param TargetReport $targetReport
* @return JsonResponse
*/
public function listAction(Request $request, TargetReport $targetReport): JsonResponse
{
$from = $request->query->get('from', date("Y-m-d"));
$to = $request->query->get('to', date("Y-m-d"));
$trafficType = $request->query->get('trafficType', '');
$lastScanStatus = $request->query->get('lastScanStatus', '');
$tags = $request->query->get('tags', '');
$from = DateTime::createFromFormat(self::DATETIME_FORMAT, $from);
$to = DateTime::createFromFormat(self::DATETIME_FORMAT, $to);
$trafficType = !empty($trafficType) ? explode(',', $trafficType) : [];
$lastScanStatus = !empty($lastScanStatus) ? explode(',', $lastScanStatus) : [];
$tags = !empty($tags) ? explode(',', $tags) : [];
$targetDTOs = $targetReport->getTargetsWithLastScanResult($from, $to, $trafficType, $lastScanStatus, $tags);
return $this->json($targetDTOs);
}
}