src/AppBundle/Controller/Dashboard/DashboardController.php line 28

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Controller\Dashboard;
  3. use AppBundle\Controller\Controller;
  4. use AppBundle\Form\Model\Dashboard\DashboardFilters;
  5. use AppBundle\Form\Type\Dashboard\DashboardFiltersType;
  6. use DomainBundle\Query\Insurer\GetInsurerInsights;
  7. use DomainBundle\Query\Operator\GetOperatorInsights;
  8. use DomainBundle\Query\Reporting\GetReviewInsights;
  9. use DomainBundle\Repository\Reporting\ReviewFilters;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use ValueObject\DateTime\DateRange;
  14. /**
  15.  * Controller for the admin insights dashboard.
  16.  */
  17. class DashboardController extends Controller
  18. {
  19.     /**
  20.      * @Route("/", name="app_dashboard")
  21.      *
  22.      * @Security("is_granted('ROLE_USER')")
  23.      */
  24.     public function dashboardAction(Request $request) {
  25.         return $this->redirectToRoute('app_ticket_list');
  26.         if (!$this->isGranted('ROLE_ADMIN')) {
  27.             return $this->redirectToRoute('app_ticket_list');
  28.         }
  29.         $filters $this->getFilterManager()->getFilters(DashboardFilters::class, $request);
  30.         $form $this->createForm(DashboardFiltersType::class, $filters, array(
  31.             'method' => 'GET',
  32.             'action' => $this->generateUrl('app_dashboard'),
  33.         ));
  34.         $form->handleRequest($request);
  35.         $filters $this->getFilterManager()->normalizeFilters($filters);
  36.         $formView $form->createView();
  37.         $formView->children['view']->vars['attr']['mtl-init'] = 'dashboardViewSelect';
  38.         $dateSegments $this->getFilterDateSegments($filters->dateRange);
  39.         return $this->render('@App/dashboard/dashboard.html.twig', array(
  40.             'filters_form' => $formView,
  41.             'filters_args' => $this->getFilterManager()->saveFilters($filters),
  42.             'view_type'    => $this->getViewType($filters),
  43.             'view_data'    => $this->getViewData($filters),
  44.             'segments'     => $dateSegments,
  45.         ));
  46.     }
  47.     /**
  48.      * Gets the chosen report type (operator, insurer).
  49.      */
  50.     public function getViewType(DashboardFilters $filters) {
  51.         return $filters->view;
  52.     }
  53.     /**
  54.      * Gets view data for the given report type.
  55.      */
  56.     public function getViewData(DashboardFilters $filters) {
  57.         $type $this->getViewType($filters);
  58.         switch ($type) {
  59.             case 'operator':
  60.                 return $this->getOperatorInsights($filters);
  61.             case 'insurer':
  62.                 return $this->getInsurerInsights($filters);
  63.             case 'reviews':
  64.                 return $this->getReviewsInsights($filters);
  65.         }
  66.         throw $this->createNotFoundException(vsprintf('Dashboard view type "%s" is not recognised', array(
  67.             $type,
  68.         )));
  69.     }
  70.     /**
  71.      * Gets view data for the operator insights report.
  72.      */
  73.     public function getOperatorInsights(DashboardFilters $filters) {
  74.         $query = new GetOperatorInsights($filters->dateRange$filters->orderBy$filters->orderDir);
  75.         return $this->handleQuery($query);
  76.     }
  77.     /**
  78.      * Gets view data for the insurer insights report.
  79.      */
  80.     public function getInsurerInsights(DashboardFilters $filters) {
  81.         $query = new GetInsurerInsights($filters->dateRange$filters->orderBy$filters->orderDir);
  82.         return $this->handleQuery($query);
  83.     }
  84.     /**
  85.      * Gets view data for the reviews insights report.
  86.      */
  87.     public function getReviewsInsights(DashboardFilters $filters) {
  88.         $reviewFilters = new ReviewFilters();
  89.         $reviewFilters->dateRange $filters->dateRange;
  90.         $reviewFilters->orderBy   $filters->orderBy;
  91.         $reviewFilters->orderDir  $filters->orderDir;
  92.         $query = new GetReviewInsights($reviewFilters);
  93.         return $this->handleQuery($query);
  94.     }
  95.     /**
  96.      * Gets date segments for insurer insights.
  97.      *
  98.      * This functionality matches that found in the GetInsurerInsights query, but in this case
  99.      * is used to pre-populate the ticket filters when clicking on an individual cell.
  100.      */
  101.     public function getFilterDateSegments(DateRange $dateRange) {
  102.         $startDate $dateRange->getStartDate();
  103.         $endDate $dateRange->getEndDate();
  104.         $segmentEndDate date_create('9999-12-31'); // the first 30d segment has an arbitrarily-large upper bound
  105.         $out = array();
  106.         foreach (array(
  107.             'p30d' => new \DateInterval('P30D'),
  108.             'p60d' => new \DateInterval('P60D'),
  109.             'p90d' => new \DateInterval('P90D'),
  110.             'rest' => new \DateInterval('P99Y'),
  111.         ) as $k => $interval) {
  112.             $segmentStartDate date_create('now')->sub($interval);
  113.             $out[$k] = DateRange::from(
  114.                 max($startDatemin($segmentStartDate$endDate)),
  115.                 max($startDatemin($segmentEndDate$endDate))
  116.             );
  117.             $segmentEndDate $segmentStartDate// use this segment's start date as the next segment's end date
  118.         }
  119.         return $out;
  120.     }
  121. }