Passed
Push — master ( a74354...254a60 )
by Andrea
18:29 queued 11s
created

FiApiCoreControllerTrait::index()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 81
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 41
nc 2
nop 2
dl 0
loc 81
ccs 0
cts 42
cp 0
crap 6
rs 9.264
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\JsonResponse;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
13
14
trait FiApiCoreControllerTrait
15
{
16
17
18
    /**
19
     * Lists all tables entities.
20
     */
21
    public function index(Request $request, Packages $assetsmanager)
22
    {
23
        $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
        $controller = $this->getController();
0 ignored issues
show
Bug introduced by
The method getController() does not exist on Cdf\BiCoreBundle\Control...iApiCoreControllerTrait. Did you maybe mean getControllerItemName()? ( 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();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
25
        $idpassato = $request->get('id');
26
27
        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
            throw new AccessDeniedException('Non si hanno i permessi per visualizzare questo contenuto');
29
        }
30
        $crudtemplate = $this->getCrudTemplate($bundle, $controller, $this->getThisFunctionName());
31
32
        //$entityclassnotation = $this->getEntityClassNotation();
33
        //$entityclass = $this->getEntityClassName();
34
35
        $entityclassnotation = 'App:'.$this->model;
36
        // Variable containing API controller
37
38
        $modellocolonne = [
39
                /*
40
                  $controller . ".nominativo" => array(
41
                  "nometabella" => $controller,
42
                  "nomecampo" => "nominativo",
43
                  "etichetta" => "Nominativo",
44
                  "ordine" => 10,
45
                  "larghezza" => 200,
46
                  "escluso" => false
47
                  ),
48
                  $controller . ".datanascita" => array(
49
                  "nometabella" => $controller,
50
                  "nomecampo" => "datanascita",
51
                  "etichetta" => "Data di nascita",
52
                  "ordine" => 20,
53
                  "larghezza" => 100,
54
                  "escluso" => false
55
                  ),
56
57
                 */
58
        ];
59
60
        //append automatic computed enum options
61
        $modellocolonne = array_merge($modellocolonne, $this->enumOptions);
62
63
        //dump($modellocolonne);
64
        //dump($this->enumOptions);
65
66
        $filtri = [];
67
        $prefiltri = [];
68
        //$entityutils = new EntityUtils($this->get('doctrine')->getManager());
69
        //$tablenamefromentity = $entityutils->getTableFromEntity($entityclass);
70
        $tablenamefromentity = $controller;
71
        $colonneordinamento = [$tablenamefromentity.'.id' => 'DESC'];
72
        $parametritabella = ['em' => ParametriTabella::setParameter('default'),
73
            'tablename' => ParametriTabella::setParameter($tablenamefromentity),
74
            'nomecontroller' => ParametriTabella::setParameter($controller),
75
            'bundle' => ParametriTabella::setParameter($bundle),
76
            'entityname' => ParametriTabella::setParameter($entityclassnotation),
77
            'entityclass' => ParametriTabella::setParameter($this->controllerItem),
78
            'formclass' => ParametriTabella::setParameter($this->formClass),
79
            'modellocolonne' => ParametriTabella::setParameter(json_encode($modellocolonne)),
80
            'permessi' => ParametriTabella::setParameter(json_encode($this->getPermessi()->toJson($controller))),
81
            'urltabella' => ParametriTabella::setParameter($assetsmanager->getUrl('/').$controller.'/'.'tabella'),
82
            'baseurl' => ParametriTabella::setParameter($assetsmanager->getUrl('/')),
83
            'idpassato' => ParametriTabella::setParameter($idpassato),
84
            'titolotabella' => ParametriTabella::setParameter('Elenco '.$controller),
85
            'multiselezione' => ParametriTabella::setParameter('0'),
86
            'editinline' => ParametriTabella::setParameter('0'),
87
            'paginacorrente' => ParametriTabella::setParameter('1'),
88
            'paginetotali' => ParametriTabella::setParameter(''),
89
            'righetotali' => ParametriTabella::setParameter('0'),
90
            'righeperpagina' => ParametriTabella::setParameter('15'),
91
            'estraituttirecords' => ParametriTabella::setParameter('0'),
92
            'colonneordinamento' => ParametriTabella::setParameter(json_encode($colonneordinamento)),
93
            'filtri' => ParametriTabella::setParameter(json_encode($filtri)),
94
            'prefiltri' => ParametriTabella::setParameter(json_encode($prefiltri)),
95
            'traduzionefiltri' => ParametriTabella::setParameter(''),
96
            'isApi' => ParametriTabella::setParameter('1'),
97
            'apicontroller' => ParametriTabella::setParameter($this->apiController),
98
            'apicollection' => ParametriTabella::setParameter($this->collection)
99
        ];
100
101
        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

101
        return $this->/** @scrutinizer ignore-call */ render($crudtemplate, ['parametritabella' => $parametritabella]);
Loading history...
102
    }
103
104
    /**
105
     * Lists all tables entities.
106
     */
107
  /*  public function indexDettaglio(Request $request, Packages $assetsmanager)
108
    {
109
        if (!$this->getPermessi()->canRead($this->getController())) {
110
            throw new AccessDeniedException('Non si hanno i permessi per visualizzare questo contenuto');
111
        }
112
113
        $bundle = $this->getBundle();
114
        $controller = $this->getController();
115
        $parametripassati = json_decode($request->get('parametripassati'), true);
116
117
        $filtri = $this->getParametroIndexDettaglio($parametripassati, 'filtri', []);
118
        $prefiltri = $this->getParametroIndexDettaglio($parametripassati, 'prefiltri', []);
119
        $titolotabella = $this->getParametroIndexDettaglio($parametripassati, 'titolotabella', 'Elenco '.$controller);
120
        $modellocolonne = $this->getParametroIndexDettaglio($parametripassati, 'modellocolonne', []);
121
        $colonneordinamento = $this->getParametroIndexDettaglio($parametripassati, 'colonneordinamento', []);
122
        $multiselezione = $this->getParametroIndexDettaglio($parametripassati, 'multiselezione', 0);
123
        $parametriform = $this->getParametroIndexDettaglio($parametripassati, 'parametriform', []);
124
125
        $template = $bundle.':'.$controller.':'.$this->getThisFunctionName().'.html.twig';
126
        if (!$this->get('twig')->getLoader()->exists($template)) {
127
            $template = $controller.'/Crud/'.$this->getThisFunctionName().'.html.twig';
128
        }
129
130
        $entityclassnotation = $this->getEntityClassNotation();
131
        $entityclass = $this->getEntityClassName();
132
133
        $formclass = str_replace('Entity', 'Form', $entityclass);
134
135
        $entityutils = new EntityUtils($this->get('doctrine')->getManager());
136
137
        $tablenamefromentity = $entityutils->getTableFromEntity($entityclass);
138
        $parametritabella = ['em' => ParametriTabella::setParameter('default'),
139
            'tablename' => ParametriTabella::setParameter($tablenamefromentity),
140
            'nomecontroller' => ParametriTabella::setParameter($controller),
141
            'bundle' => ParametriTabella::setParameter($bundle),
142
            'entityname' => ParametriTabella::setParameter($entityclassnotation),
143
            'entityclass' => ParametriTabella::setParameter($entityclass),
144
            'formclass' => ParametriTabella::setParameter($formclass),
145
            'parametriform' => ParametriTabella::setParameter(json_encode($parametriform)),
146
            'modellocolonne' => ParametriTabella::setParameter(json_encode($modellocolonne)),
147
            'permessi' => ParametriTabella::setParameter(json_encode($this->getPermessi()->toJson($controller))),
148
            'urltabella' => ParametriTabella::setParameter($assetsmanager->getUrl('/').$controller.'/'.'tabella'),
149
            'baseurl' => ParametriTabella::setParameter($assetsmanager->getUrl('/')),
150
            'idpassato' => ParametriTabella::setParameter(0),
151
            'titolotabella' => ParametriTabella::setParameter($titolotabella),
152
            'multiselezione' => ParametriTabella::setParameter($multiselezione),
153
            'editinline' => ParametriTabella::setParameter('1'),
154
            'paginacorrente' => ParametriTabella::setParameter('1'),
155
            'paginetotali' => ParametriTabella::setParameter(''),
156
            'righeperpagina' => ParametriTabella::setParameter('15'),
157
            'colonneordinamento' => ParametriTabella::setParameter(json_encode($colonneordinamento)),
158
            'filtri' => ParametriTabella::setParameter(json_encode($filtri)),
159
            'prefiltri' => ParametriTabella::setParameter(json_encode($prefiltri)),
160
            'traduzionefiltri' => ParametriTabella::setParameter(''),
161
        ];
162
163
        return $this->render(
164
            $template,
165
            [
166
                            'parametritabella' => $parametritabella,
167
                        ]
168
        );
169
    }*/
170
171
  /*  protected function getParametroIndexDettaglio($parametripassati, $keyparametro, $defaultvalue)
172
    {
173
        if (isset($parametripassati[$keyparametro])) {
174
            $parametro = $parametripassati[$keyparametro];
175
        } else {
176
            $parametro = $defaultvalue;
177
        }
178
179
        return $parametro;
180
    }*/
181
182
    /**
183
     * Lists all tables entities.
184
     */
185
  /*  public function lista(Request $request)
186
    {
187
        if (!$this->getPermessi()->canRead($this->getController())) {
188
            throw new AccessDeniedException('Non si hanno i permessi per visualizzare questo contenuto');
189
        }
190
191
        $entityclassnotation = $this->getEntityClassNotation();
192
        $em = $this->get('doctrine')->getManager();
193
        $righe = $em->getRepository($entityclassnotation)->findAll();
194
195
        $lista = [];
196
        foreach ($righe as $riga) {
197
            $lista[] = ['id' => $riga->getId(), 'descrizione' => $riga->__toString()];
198
        }
199
200
        return new JsonResponse(ArrayUtils::arrayOrderby($lista, 'descrizione', SORT_ASC));
201
    }*/
202
203
    protected function getTabellaTemplate($controller)
204
    {
205
        $tabellatemplate = $controller.'/Tabella/tabellaform.html.twig';
206
        if (!$this->get('twig')->getLoader()->exists($tabellatemplate)) {
0 ignored issues
show
Bug introduced by
It seems like get() 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

206
        if (!$this->/** @scrutinizer ignore-call */ get('twig')->getLoader()->exists($tabellatemplate)) {
Loading history...
207
            $tabellatemplate = '@BiCore/'.$controller.'/Tabella/tabellaform.html.twig';
208
            if (!$this->get('twig')->getLoader()->exists($tabellatemplate)) {
209
                $tabellatemplate = '@BiCore/Standard/Tabella/tabellaform.html.twig';
210
            }
211
        }
212
213
        return $tabellatemplate;
214
    }
215
216
    protected function getCrudTemplate($bundle, $controller, $operation)
217
    {
218
        $crudtemplate = $bundle.'/'.$controller.'/Crud/'.$operation.'.html.twig';
219
        if (!$this->get('twig')->getLoader()->exists($crudtemplate)) {
220
            $crudtemplate = $controller.'/Crud/'.$operation.'.html.twig';
221
            if (!$this->get('twig')->getLoader()->exists($crudtemplate)) {
222
                $crudtemplate = '@BiCore/Standard/Crud/'.$operation.'.html.twig';
223
            }
224
        }
225
226
        return $crudtemplate;
227
    }
228
229
    /**
230
     * Returns the calling function through a backtrace.
231
     */
232
    protected function getThisFunctionName()
233
    {
234
        // a funciton x has called a function y which called this
235
        // see stackoverflow.com/questions/190421
236
        $caller = debug_backtrace();
237
        $caller = $caller[1];
238
239
        return $caller['function'];
240
    }
241
242
    /**
243
     * Return the model class name needed to instantiate it
244
     */
245
    protected function getModelClassName(): String
246
    {
247
        return $this->modelClass;
248
    }
249
250
    /**
251
     * Return the controller item class name needed to instantiate it
252
     */
253
    protected function getControllerItemName(): String
254
    {
255
        return $this->controllerItem;
256
    }
257
258
    /**
259
     * Return the form class name needed to reference the proper form
260
     */
261
    protected function getFormName(): String
262
    {
263
        return $this->formClass;
264
    }
265
266
       /* protected function getEntityClassNotation()
267
    {
268
        $em = $this->get('doctrine')->getManager();
269
        $entityutils = new EntityUtils($em);
270
271
        return $entityutils->getClassNameToShortcutNotations($this->getEntityClassName());
272
    }*/
273
274
   /* protected function getEntityClassName()
275
    {
276
        $em = $this->get('doctrine')->getManager();
277
        $entityfinder = new Finder($em);
278
279
        return $entityfinder->getClassNameFromEntityName($this->controller);
280
    }*/
281
}
282