Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
DashboardController
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 3
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 indexAction
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 1
42
 exportCsvAction
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3/**
4 * This file is part of the MADIS - RGPD Management application.
5 *
6 * @copyright Copyright (c) 2018-2019 Soluris - Solutions Numériques Territoriales Innovantes
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22declare(strict_types=1);
23
24namespace App\Domain\Reporting\Controller;
25
26use App\Application\Interfaces\CollectivityRelated;
27use App\Domain\Registry\Repository\Mesurement;
28use App\Domain\Reporting\Handler\ExportCsvHandler;
29use App\Domain\Reporting\Handler\MetricsHandler;
30use App\Infrastructure\ORM\Maturity\Repository\Referentiel;
31use App\Infrastructure\ORM\Maturity\Repository\Survey;
32use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
33use Symfony\Component\HttpFoundation\Request;
34use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
35
36class DashboardController extends AbstractController
37{
38    /**
39     * @var MetricsHandler
40     */
41    private $metricsHandler;
42
43    /**
44     * @var ExportCsvHandler
45     */
46    private $exportCsvHandler;
47
48    private Mesurement $repository;
49
50    private Referentiel $referentielRepository;
51    private Survey $surveyRepository;
52
53    public function __construct(
54        MetricsHandler $metricsHandler,
55        ExportCsvHandler $exportCsvHandler,
56        Mesurement $repository,
57        Referentiel $referentielRepository,
58        Survey $surveyRepository,
59    ) {
60        $this->repository            = $repository;
61        $this->metricsHandler        = $metricsHandler;
62        $this->exportCsvHandler      = $exportCsvHandler;
63        $this->referentielRepository = $referentielRepository;
64        $this->surveyRepository      = $surveyRepository;
65    }
66
67    /**
68     * Get dashboard index page.
69     * Compute every metrics to display.
70     *
71     * @return \Symfony\Component\HttpFoundation\Response
72     */
73    public function indexAction(Request $request)
74    {
75        $metrics      = $this->metricsHandler->getHandler();
76        $actions      = [];
77        $referentiels = $this->referentielRepository->findAll();
78        if (!$this->isGranted('ROLE_REFERENT')) {
79            $user         = $this->getUser();
80            $collectivity = $user instanceof CollectivityRelated ? $user->getCollectivity() : null;
81            $actions      = $this->repository->getPlanifiedActionsDashBoard($this->getParameter('APP_USER_DASHBOARD_ACTION_PLAN_LIMIT'), $collectivity);
82            $referentiels = array_reduce(
83                array_map(
84                    function (\App\Domain\Maturity\Model\Survey $survey) {
85                        return $survey->getReferentiel();
86                    }, $this->surveyRepository->findAllByCollectivity($collectivity, ['createdAt' => 'DESC'])
87                ),
88                function (array $result, \App\Domain\Maturity\Model\Referentiel $referentiel) {
89                    if (!isset($result[$referentiel->getId()->toString()])) {
90                        $result[$referentiel->getId()->toString()] = $referentiel;
91                    }
92
93                    return $result;
94                },
95                []);
96
97            $referentiels = array_values($referentiels);
98        }
99        $selectedRef = $referentiels[0] ?? null;
100        if ($request->get('referentiel')) {
101            $selRefs = array_values(array_filter($referentiels, function (\App\Domain\Maturity\Model\Referentiel $referentiel) use ($request) {
102                return $referentiel->getId()->toString() === $request->get('referentiel');
103            }));
104
105            if (count($selRefs) > 0) {
106                $selectedRef = $selRefs[0];
107            }
108        }
109
110        return $this->render($metrics->getTemplateViewName(), [
111            'data'         => $metrics->getData($selectedRef),
112            'actions'      => $actions,
113            'referentiels' => $referentiels,
114            'selected_ref' => $selectedRef,
115        ]);
116    }
117
118    /**
119     * Generate CSV file for collectivity or treatment.
120     *
121     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
122     */
123    public function exportCsvAction(string $exportType)
124    {
125        if (!$this->isGranted('ROLE_REFERENT')) {
126            throw new AccessDeniedHttpException('You can\'t access to csv export');
127        }
128
129        return $this->exportCsvHandler->generateCsv($exportType);
130    }
131}