Complex classes like TabelleController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TabelleController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class TabelleController extends FiCoreController |
||
14 | { |
||
15 | |||
16 | public function aggiornaAction(Request $request) |
||
65 | |||
66 | private function getRequestValue($request, $attribute) |
||
74 | |||
75 | 1 | public function configuraAction(Request $request, $nometabella) |
|
163 | |||
164 | 1 | public function generaDB($parametri, Request $request) |
|
209 | |||
210 | 1 | private function scriviDB($colonne, $nometabella, $nomebundle, $parametri) |
|
211 | { |
||
212 | 1 | foreach ($colonne as $colonna) { |
|
213 | $vettorericerca = array( |
||
214 | 1 | 'nometabella' => $nometabella, |
|
215 | 1 | 'nomecampo' => $colonna, |
|
216 | ); |
||
217 | |||
218 | 1 | if (isset($parametri['operatore'])) { |
|
219 | 1 | $vettorericerca['operatori_id'] = $parametri['operatore']; |
|
220 | } |
||
221 | |||
222 | 1 | $trovato = $this->getDoctrine()->getRepository($nomebundle . ':Tabelle')->findBy($vettorericerca, array()); |
|
223 | |||
224 | 1 | if (empty($trovato)) { |
|
225 | 1 | $this->creaRecordTabelle($nometabella, $colonna, $vettorericerca, $parametri); |
|
226 | } |
||
227 | } |
||
228 | 1 | } |
|
229 | |||
230 | 1 | private function creaRecordTabelle($nometabella, $colonna, $vettorericerca, $parametri) |
|
231 | { |
||
232 | 1 | $crea = new Tabelle(); |
|
233 | 1 | $crea->setNometabella($nometabella); |
|
234 | 1 | $crea->setNomecampo($colonna); |
|
235 | |||
236 | 1 | if (isset($parametri['operatore'])) { |
|
237 | 1 | $idOperatore = $parametri['operatore']; |
|
238 | 1 | $creaoperatore = $this->getDoctrine()->getRepository('FiCoreBundle:Operatori')->find($idOperatore); |
|
239 | 1 | if ($creaoperatore instanceof \Fi\CoreBundle\Entity\Operatori) { |
|
240 | 1 | $crea->setOperatori($creaoperatore); |
|
241 | } |
||
242 | |||
243 | 1 | $vettorericerca['operatori_id'] = null; |
|
244 | 1 | $ritrovato = $this->getDoctrine()->getRepository('FiCoreBundle:Tabelle')->findOneBy($vettorericerca); |
|
245 | |||
246 | 1 | if (!empty($ritrovato)) { |
|
247 | 1 | $crea->setMostrastampa($ritrovato->hasMostrastampa() ? true : false); |
|
248 | 1 | $crea->setMostraindex($ritrovato->hasMostraindex() ? true : false); |
|
249 | } |
||
250 | } else { |
||
251 | 1 | $crea->setMostrastampa(true); |
|
252 | 1 | $crea->setMostraindex(true); |
|
253 | } |
||
254 | |||
255 | 1 | $ma = $this->getDoctrine()->getManager(); |
|
256 | 1 | $ma->persist($crea); |
|
257 | 1 | $ma->flush(); |
|
258 | 1 | } |
|
259 | |||
260 | public function grigliapopupAction(Request $request, $chiamante) |
||
261 | { |
||
262 | $this->setup($request); |
||
263 | $namespace = $this->getNamespace(); |
||
264 | $bundle = $this->getBundle(); |
||
265 | $controller = $this->getController(); |
||
266 | |||
267 | $nomebundle = $namespace . $bundle . 'Bundle'; |
||
268 | $em = $this->getDoctrine()->getManager(); |
||
269 | |||
270 | $gestionepermessi = $this->get("ficorebundle.gestionepermessi"); |
||
271 | $operatore = $gestionepermessi->utentecorrente(); |
||
272 | $tabellej = array(); |
||
273 | $tabellej['operatori_id'] = array('tabella' => 'operatori', 'campi' => array('username', 'operatore')); |
||
274 | |||
275 | $paricevuti = array( |
||
276 | 'request' => $request, |
||
277 | 'doctrine' => $em, |
||
278 | 'container' => $this->container, |
||
279 | 'nomebundle' => $nomebundle, |
||
280 | 'nometabella' => $controller, |
||
281 | 'tabellej' => $tabellej,); |
||
282 | |||
283 | $paricevuti['escludere'] = array('nometabella', 'operatori_id'); |
||
284 | $paricevuti['precondizioni'] = array('Tabelle.nometabella' => $chiamante, 'Tabelle.operatori_id' => $operatore['id']); |
||
285 | |||
286 | return new Response(Griglia::datiPerGriglia($paricevuti)); |
||
287 | } |
||
288 | |||
289 | 1 | protected function setParametriGriglia($prepar = array()) |
|
290 | { |
||
291 | 1 | $this->setup($prepar['request']); |
|
292 | 1 | $namespace = $this->getNamespace(); |
|
293 | 1 | $bundle = $this->getBundle(); |
|
294 | 1 | $controller = $this->getController(); |
|
295 | |||
296 | 1 | $gestionepermessi = $this->get("ficorebundle.gestionepermessi"); |
|
297 | 1 | $canRead = ($gestionepermessi->leggere(array('modulo' => $controller)) ? 1 : 0); |
|
298 | 1 | if (!$canRead) { |
|
299 | throw new AccessDeniedException("Non si hanno i permessi per visualizzare questo contenuto"); |
||
300 | } |
||
301 | |||
302 | 1 | $nomebundle = $namespace . $bundle . 'Bundle'; |
|
303 | 1 | $tabellej = array(); |
|
304 | 1 | $tabellej['operatori_id'] = array('tabella' => 'operatori', 'campi' => array('username')); |
|
305 | 1 | $escludi = array("operatori"); //'operatori_id' |
|
306 | |||
307 | $paricevuti = array( |
||
308 | 1 | 'container' => $this->container, |
|
309 | 1 | 'nomebundle' => $nomebundle, |
|
310 | 1 | 'nometabella' => $controller, |
|
311 | 1 | 'tabellej' => $tabellej, |
|
312 | 1 | 'escludere' => $escludi |
|
313 | ); |
||
314 | |||
315 | 1 | if (!empty($prepar)) { |
|
316 | 1 | $paricevuti = array_merge($paricevuti, $prepar); |
|
317 | } |
||
318 | |||
319 | 1 | self::$parametrigriglia = $paricevuti; |
|
320 | 1 | } |
|
321 | |||
322 | public function listacampitabellaAction(Request $request) |
||
323 | { |
||
324 | $this->setup($request); |
||
325 | $namespace = $this->getNamespace(); |
||
326 | $bundle = $this->getBundle(); |
||
327 | $controller = $this->getController(); |
||
328 | $nomebundle = $namespace . $bundle . 'Bundle'; |
||
329 | |||
330 | $nometabella = trim($request->get('tabella')); |
||
331 | if (!isset($nometabella)) { |
||
332 | return false; |
||
333 | } |
||
334 | |||
335 | $escludiid = $request->get('escludiid'); |
||
336 | if (!isset($escludiid)) { |
||
337 | $escludiid = 0; |
||
338 | } |
||
339 | |||
340 | $em = $this->getDoctrine()->getManager(); |
||
341 | $tableClassName = ""; |
||
342 | $entityClass = ""; |
||
343 | $bundles = $this->get('kernel')->getBundles(); |
||
344 | foreach ($bundles as $bundle) { |
||
345 | $className = get_class($bundle); |
||
346 | $entityClass = substr($className, 0, strrpos($className, '\\')); |
||
347 | $tableClassName = '\\' . $entityClass . '\\Entity\\' . $nometabella; |
||
348 | if (!class_exists($tableClassName)) { |
||
349 | $tableClassName = ''; |
||
350 | continue; |
||
351 | } else { |
||
352 | break; |
||
353 | } |
||
354 | } |
||
355 | |||
356 | if (!$tableClassName) { |
||
357 | throw new \Exception('Entity per la tabella ' . $nometabella . ' non trovata', '-1'); |
||
358 | } |
||
359 | |||
360 | if (!$entityClass) { |
||
361 | throw new \Exception('Entity class per la tabella ' . $nometabella . ' non trovata', '-1'); |
||
362 | } |
||
363 | |||
364 | $bundleClass = str_replace('\\', '', $entityClass); |
||
365 | $c = $em->getClassMetadata($bundleClass . ':' . $nometabella); |
||
366 | $colonne = $c->getColumnNames(); |
||
367 | |||
368 | $risposta = $this->listacampitabelladettagli($escludiid, $colonne, $nomebundle, $controller); |
||
369 | //natcasesort($risposta); |
||
370 | asort($risposta, SORT_NATURAL | SORT_FLAG_CASE); |
||
371 | |||
372 | return new JsonResponse($risposta); |
||
373 | } |
||
374 | |||
375 | private function listacampitabelladettagli($escludiid, $colonne, $nomebundle, $controller) |
||
431 | } |
||
432 |