FiCoreControllerTrait   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 236
Duplicated Lines 0 %

Test Coverage

Coverage 98.01%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 120
dl 0
loc 236
ccs 148
cts 151
cp 0.9801
rs 10
c 1
b 0
f 0
wmc 19

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getParametroIndexDettaglio() 0 9 2
A lista() 0 18 3
A getEntityClassNotation() 0 4 1
A getCrudTemplate() 0 11 3
A getThisFunctionName() 0 8 1
A getTabellaTemplate() 0 11 3
A getEntityClassName() 0 5 1
A index() 0 70 2
A indexDettaglio() 0 60 3
1
<?php
2
3
namespace Cdf\BiCoreBundle\Controller;
4
5
use Cdf\BiCoreBundle\Utils\Arrays\ArrayUtils;
6
use Cdf\BiCoreBundle\Utils\Entity\EntityUtils;
7
use Cdf\BiCoreBundle\Utils\Entity\Finder;
8
use Cdf\BiCoreBundle\Utils\Tabella\ParametriTabella;
9
use Symfony\Component\Asset\Packages;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\HttpFoundation\JsonResponse;
13
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
14
15
trait FiCoreControllerTrait
16
{
17
18
    /**
19
     * Lists all tables entities.
20
     */
21 6
    public function index(Request $request, Packages $assetsmanager): Response
22
    {
23 6
        $bundle = $this->getBundle();
0 ignored issues
show
Bug introduced by
It seems like getBundle() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

23
        /** @scrutinizer ignore-call */ 
24
        $bundle = $this->getBundle();
Loading history...
24 6
        $controller = $this->getController();
0 ignored issues
show
Bug introduced by
It seems like getController() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
        /** @scrutinizer ignore-call */ 
25
        $controller = $this->getController();
Loading history...
25 6
        $idpassato = $request->get('id');
26
27 6
        if (!$this->getPermessi()->canRead($this->getController())) {
0 ignored issues
show
Bug introduced by
It seems like getPermessi() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

27
        if (!$this->/** @scrutinizer ignore-call */ getPermessi()->canRead($this->getController())) {
Loading history...
28 1
            throw new AccessDeniedException('Non si hanno i permessi per visualizzare questo contenuto');
29
        }
30 5
        $crudtemplate = $this->getCrudTemplate($bundle, $controller, $this->getThisFunctionName());
31
32 5
        $entityclassnotation = $this->getEntityClassNotation();
33 5
        $entityclass = $this->getEntityClassName();
34
35 5
        $formclass = str_replace('Entity', 'Form', $entityclass);
36
37 5
        $modellocolonne = [
38 5
                /*
39 5
                  array(
40 5
                  "nometabella" => $controller,
41 5
                  "nomecampo" => $controller . ".nominativo",
42 5
                  "etichetta" => "Nominativo",
43 5
                  "ordine" => 10,
44 5
                  "larghezza" => 200,
45 5
                  "escluso" => false
46 5
                  ),
47 5
                  array(
48 5
                  "nometabella" => $controller,
49 5
                  "nomecampo" => $controller . ".datanascita",
50 5
                  "etichetta" => "Data di nascita",
51 5
                  "ordine" => 20,
52 5
                  "larghezza" => 100,
53 5
                  "escluso" => false
54 5
                  ),
55
56 5
                 */
57 5
        ];
58
59 5
        $filtri = [];
60 5
        $prefiltri = [];
61 5
        $entityutils = new EntityUtils($this->em);
62 5
        $tablenamefromentity = $entityutils->getTableFromEntity($entityclass);
63 5
        $colonneordinamento = [$tablenamefromentity . '.id' => 'DESC'];
64 5
        $parametritabella = ['em' => ParametriTabella::setParameter('default'),
65 5
            'tablename' => ParametriTabella::setParameter($tablenamefromentity),
66 5
            'nomecontroller' => ParametriTabella::setParameter($controller),
67 5
            'bundle' => ParametriTabella::setParameter($bundle),
68 5
            'entityname' => ParametriTabella::setParameter($entityclassnotation),
69 5
            'entityclass' => ParametriTabella::setParameter($entityclass),
70 5
            'formclass' => ParametriTabella::setParameter($formclass),
71 5
            'modellocolonne' => ParametriTabella::setParameter(json_encode($modellocolonne)),
72 5
            'permessi' => ParametriTabella::setParameter(json_encode($this->getPermessi()->toJson($controller))),
73 5
            'urltabella' => ParametriTabella::setParameter($assetsmanager->getUrl('') . $controller . '/' . 'tabella'),
74 5
            'baseurl' => ParametriTabella::setParameter($assetsmanager->getUrl('')),
75 5
            'idpassato' => ParametriTabella::setParameter($idpassato),
76 5
            'titolotabella' => ParametriTabella::setParameter('Elenco ' . $controller),
77 5
            'multiselezione' => ParametriTabella::setParameter('0'),
78 5
            'editinline' => ParametriTabella::setParameter('0'),
79 5
            'paginacorrente' => ParametriTabella::setParameter('1'),
80 5
            'paginetotali' => ParametriTabella::setParameter(''),
81 5
            'righetotali' => ParametriTabella::setParameter('0'),
82 5
            'righeperpagina' => ParametriTabella::setParameter('15'),
83 5
            'estraituttirecords' => ParametriTabella::setParameter('0'),
84 5
            'colonneordinamento' => ParametriTabella::setParameter(json_encode($colonneordinamento)),
85 5
            'filtri' => ParametriTabella::setParameter(json_encode($filtri)),
86 5
            'prefiltri' => ParametriTabella::setParameter(json_encode($prefiltri)),
87 5
            'traduzionefiltri' => ParametriTabella::setParameter(''),
88 5
        ];
89
90 5
        return $this->render($crudtemplate, ['parametritabella' => $parametritabella]);
0 ignored issues
show
Bug introduced by
It seems like render() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

90
        return $this->/** @scrutinizer ignore-call */ render($crudtemplate, ['parametritabella' => $parametritabella]);
Loading history...
91
    }
92
93
    /**
94
     * Lists all tables entities.
95
     */
96 1
    public function indexDettaglio(Request $request, Packages $assetsmanager): Response
97
    {
98 1
        if (!$this->getPermessi()->canRead($this->getController())) {
99
            throw new AccessDeniedException('Non si hanno i permessi per visualizzare questo contenuto');
100
        }
101
102 1
        $bundle = $this->getBundle();
103 1
        $controller = $this->getController();
104 1
        $parametripassati = json_decode($request->get('parametripassati'), true);
0 ignored issues
show
Bug introduced by
It seems like $request->get('parametripassati') can also be of type null; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

104
        $parametripassati = json_decode(/** @scrutinizer ignore-type */ $request->get('parametripassati'), true);
Loading history...
105
106 1
        $filtri = $this->getParametroIndexDettaglio($parametripassati, 'filtri', []);
107 1
        $prefiltri = $this->getParametroIndexDettaglio($parametripassati, 'prefiltri', []);
108 1
        $titolotabella = $this->getParametroIndexDettaglio($parametripassati, 'titolotabella', 'Elenco ' . $controller);
109 1
        $modellocolonne = $this->getParametroIndexDettaglio($parametripassati, 'modellocolonne', []);
110 1
        $colonneordinamento = $this->getParametroIndexDettaglio($parametripassati, 'colonneordinamento', []);
111 1
        $multiselezione = $this->getParametroIndexDettaglio($parametripassati, 'multiselezione', 0);
112 1
        $parametriform = $this->getParametroIndexDettaglio($parametripassati, 'parametriform', []);
113
114 1
        $template = $bundle . ':' . $controller . ':' . $this->getThisFunctionName() . '.html.twig';
115 1
        if (!$this->template->getLoader()->exists($template)) {
116 1
            $template = $controller . '/Crud/' . $this->getThisFunctionName() . '.html.twig';
117
        }
118
119 1
        $entityclassnotation = $this->getEntityClassName();
120 1
        $entityclass = $this->getEntityClassName();
121
122 1
        $formclass = str_replace('Entity', 'Form', $entityclass);
123
124 1
        $entityutils = new EntityUtils($this->em);
125
126 1
        $tablenamefromentity = $entityutils->getTableFromEntity($entityclass);
127 1
        $parametritabella = ['em' => ParametriTabella::setParameter('default'),
128 1
            'tablename' => ParametriTabella::setParameter($tablenamefromentity),
129 1
            'nomecontroller' => ParametriTabella::setParameter($controller),
130 1
            'bundle' => ParametriTabella::setParameter($bundle),
131 1
            'entityname' => ParametriTabella::setParameter($entityclassnotation),
132 1
            'entityclass' => ParametriTabella::setParameter($entityclass),
133 1
            'formclass' => ParametriTabella::setParameter($formclass),
134 1
            'parametriform' => ParametriTabella::setParameter(json_encode($parametriform)),
135 1
            'modellocolonne' => ParametriTabella::setParameter(json_encode($modellocolonne)),
136 1
            'permessi' => ParametriTabella::setParameter(json_encode($this->getPermessi()->toJson($controller))),
137 1
            'urltabella' => ParametriTabella::setParameter($assetsmanager->getUrl('') . $controller . '/' . 'tabella'),
138 1
            'baseurl' => ParametriTabella::setParameter($assetsmanager->getUrl('')),
139 1
            'idpassato' => ParametriTabella::setParameter(0),
140 1
            'titolotabella' => ParametriTabella::setParameter($titolotabella),
141 1
            'multiselezione' => ParametriTabella::setParameter($multiselezione),
142 1
            'editinline' => ParametriTabella::setParameter('1'),
143 1
            'paginacorrente' => ParametriTabella::setParameter('1'),
144 1
            'paginetotali' => ParametriTabella::setParameter(''),
145 1
            'righeperpagina' => ParametriTabella::setParameter('15'),
146 1
            'colonneordinamento' => ParametriTabella::setParameter(json_encode($colonneordinamento)),
147 1
            'filtri' => ParametriTabella::setParameter(json_encode($filtri)),
148 1
            'prefiltri' => ParametriTabella::setParameter(json_encode($prefiltri)),
149 1
            'traduzionefiltri' => ParametriTabella::setParameter(''),
150 1
        ];
151
152 1
        return $this->render(
153 1
            $template,
154 1
            [
155 1
                            'parametritabella' => $parametritabella,
156 1
                        ]
157 1
        );
158
    }
159
160
    /**
161
     *
162
     * @param mixed $parametripassati
163
     * @param string $keyparametro
164
     * @param mixed $defaultvalue
165
     * @return mixed
166
     */
167 1
    protected function getParametroIndexDettaglio($parametripassati, string $keyparametro, $defaultvalue)
168
    {
169 1
        if (isset($parametripassati[$keyparametro])) {
170
            $parametro = $parametripassati[$keyparametro];
171
        } else {
172 1
            $parametro = $defaultvalue;
173
        }
174
175 1
        return $parametro;
176
    }
177
178
    /**
179
     * Lists all tables entities.
180
     */
181 1
    public function lista(Request $request): JsonResponse
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

181
    public function lista(/** @scrutinizer ignore-unused */ Request $request): JsonResponse

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
182
    {
183 1
        if (!$this->getPermessi()->canRead($this->getController())) {
184
            throw new AccessDeniedException('Non si hanno i permessi per visualizzare questo contenuto');
185
        }
186
187 1
        $entityclassnotation = $this->getEntityClassName();
188 1
        $em = $this->em;
189
        /** @var class-string $entityclassnotation */
190 1
        $righe = $em->getRepository($entityclassnotation)->findAll();
191
192 1
        $lista = [];
193 1
        foreach ($righe as $riga) {
194
            /** @phpstan-ignore-next-line */
195 1
            $lista[] = ['id' => $riga->getId(), 'descrizione' => $riga->__toString()];
196
        }
197
198 1
        return new JsonResponse(ArrayUtils::arrayOrderby($lista, 'descrizione', SORT_ASC));
199
    }
200
201 11
    protected function getTabellaTemplate(string $controller): string
202
    {
203 11
        $tabellatemplate = $controller . '/Tabella/tabellaform.html.twig';
204 11
        if (!$this->template->getLoader()->exists($tabellatemplate)) {
205 8
            $tabellatemplate = '@BiCore/' . $controller . '/Tabella/tabellaform.html.twig';
206 8
            if (!$this->template->getLoader()->exists($tabellatemplate)) {
207 7
                $tabellatemplate = '@BiCore/Standard/Tabella/tabellaform.html.twig';
208
            }
209
        }
210
211 11
        return $tabellatemplate;
212
    }
213
214 13
    protected function getCrudTemplate(string $bundle, string $controller, string $operation): string
215
    {
216 13
        $crudtemplate = $bundle . '/' . $controller . '/Crud/' . $operation . '.html.twig';
217 13
        if (!$this->template->getLoader()->exists($crudtemplate)) {
218 13
            $crudtemplate = $controller . '/Crud/' . $operation . '.html.twig';
219 13
            if (!$this->template->getLoader()->exists($crudtemplate)) {
220 7
                $crudtemplate = '@BiCore/Standard/Crud/' . $operation . '.html.twig';
221
            }
222
        }
223
224 13
        return $crudtemplate;
225
    }
226
227
    /**
228
     * Returns the calling function through a backtrace.
229
     */
230 13
    protected function getThisFunctionName(): string
231
    {
232
        // a funciton x has called a function y which called this
233
        // see stackoverflow.com/questions/190421
234 13
        $caller = debug_backtrace();
235 13
        $caller = $caller[1];
236
237 13
        return $caller['function'];
238
    }
239
240 5
    protected function getEntityClassNotation(): string
241
    {
242 5
        $entityutils = new EntityUtils($this->em);
243 5
        return $entityutils->getClassNameToShortcutNotations($this->getEntityClassName());
244
    }
245
246 14
    protected function getEntityClassName(): string
247
    {
248 14
        $entityfinder = new Finder($this->em);
249
250 14
        return $entityfinder->getClassNameFromEntityName($this->controller);
251
    }
252
}
253