<?php
namespace App\Monitor\Controller;
use App\Monitor\Entity\Environment;
use App\Monitor\Entity\Project;
use App\Monitor\Entity\Result;
use App\Monitor\Service\LicenseManager;
use App\UI\Controller\AbstractUIController;
use DateTime;
use Doctrine\Common\Collections\Criteria;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class DashboardController extends AbstractUIController
{
/**
* @Route("/")
*/
public function list(Request $request, LicenseManager $licenseManager):Response
{
$projectRep = $this->entityManager->getRepository(Project::class);
$environmentRep = $this->entityManager->getRepository(Environment::class);
$resultRep = $this->entityManager->getRepository(Result::class);
$licenseForm = $this->createFormBuilder()
->add('appSecret')
->add('licensedTill', DateType::class, ['data' => (new DateTime())->modify('+ 365 days')])
->add('save', SubmitType::class, ['label' => 'Generate license key'])
->getForm();
$licenseForm->handleRequest($request);
if ($licenseForm->isSubmitted()) {
$data = $licenseForm->getData();
$customKey = $licenseManager->generateKey($data['appSecret'], $data['licensedTill']);
$this->addFlash('success', 'Generated key <b>' . $customKey . '</b>');
return $this->redirectToRoute('app_monitor_dashboard_list');
}
return $this->render(
'monitor/dashboard.html.twig', [
'totalProjects' => $projectRep->count([]),
'totalEnvironments' => $environmentRep->count([]),
'totalMonitorActiveEnvironments' => $environmentRep->count(['monitorActive' => true]),
'totalResults' => $resultRep->count([]),
'projects' => $projectRep->findBy([], ['clientName' => Criteria::ASC]),
'licenseForm' => $licenseForm->createView()
]);
}
}