Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
19.05% covered (danger)
19.05%
4 / 21
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
SurveyExtension
19.05% covered (danger)
19.05%
4 / 21
33.33% covered (danger)
33.33%
1 / 3
32.99
0.00% covered (danger)
0.00%
0 / 1
 getFunctions
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 orderByDomain
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 orderAnswersByQuestionNameAsc
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
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\Maturity\Twig\Extension;
25
26use App\Domain\Maturity\Model\Answer;
27use Symfony\Component\Form\FormView;
28use Twig\Extension\AbstractExtension;
29use Twig\TwigFunction;
30
31class SurveyExtension extends AbstractExtension
32{
33    /**
34     * @return array|TwigFunction[]
35     */
36    public function getFunctions()
37    {
38        return [
39            new TwigFunction('orderByDomain', [$this, 'orderByDomain']),
40            new TwigFunction('orderAnswersByQuestionNameAsc', [$this, 'orderAnswersByQuestionNameAsc']),
41        ];
42    }
43
44    /**
45     * Order formView answers by domain.
46     * Then, every domains must be ordered by position.
47     */
48    public function orderByDomain(FormView $formView): array
49    {
50        $answersByDomain = [];
51        foreach ($formView as $answer) {
52            $object = $answer->vars['value'];
53            if (!\array_key_exists($object->getQuestion()->getDomain()->getPosition(), $answersByDomain)) {
54                $answersByDomain[$object->getQuestion()->getDomain()->getPosition()]['name']  = $object->getQuestion()->getDomain()->getName();
55                $answersByDomain[$object->getQuestion()->getDomain()->getPosition()]['color'] = $object->getQuestion()->getDomain()->getColor();
56            }
57            $answersByDomain[$object->getQuestion()->getDomain()->getPosition()]['answers'][] = $answer;
58        }
59
60        \ksort($answersByDomain);
61
62        return $answersByDomain;
63    }
64
65    /**
66     * Order answers by question name asc.
67     *
68     * @param FormView[] $formViews
69     */
70    public function orderAnswersByQuestionNameAsc(array $formViews): array
71    {
72        $ordered = [];
73        foreach ($formViews as $formView) {
74            $answer = $formView->vars['value'];
75            if (!$answer instanceof Answer) {
76                continue;
77            }
78
79            $ordered[$answer->getQuestion()->getName()] = $formView;
80        }
81
82        \ksort($ordered);
83
84        return $ordered;
85    }
86}