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) |
||
| 17 | { |
||
| 18 | if ($request->get('oper') == 'edit') { |
||
| 19 | $gestionepermessi = $this->get("ficorebundle.gestionepermessi"); |
||
| 20 | $operatore = $gestionepermessi->utentecorrente(); |
||
| 21 | |||
| 22 | $id = $request->get('id'); |
||
| 23 | |||
| 24 | $em = $this->getDoctrine()->getManager(); |
||
| 25 | $tabelle = $em->getRepository('FiCoreBundle:Tabelle')->find($id); |
||
| 26 | if (!$tabelle) { |
||
| 27 | throw new AccessDeniedException("Oggetto non trovato"); |
||
| 28 | } |
||
| 29 | $tabelle->setOperatoriId($operatore['id']); |
||
| 30 | $nometabella = $this->getRequestValue($request, 'nometabella'); |
||
| 31 | if ($nometabella) { |
||
| 32 | $tabelle->setNometabella($nometabella); |
||
| 33 | } |
||
| 34 | $nomecampo = $this->getRequestValue($request, 'nomecampo'); |
||
| 35 | if ($nomecampo) { |
||
| 36 | $tabelle->setNomecampo($nomecampo); |
||
| 37 | } |
||
| 38 | $mostraindex = $this->getRequestValue($request, 'mostraindex'); |
||
| 39 | $tabelle->setMostraindex($mostraindex); |
||
| 40 | $ordineindex = $this->getRequestValue($request, 'ordineindex'); |
||
| 41 | $tabelle->setOrdineindex($ordineindex); |
||
| 42 | $etichettaindex = $this->getRequestValue($request, 'etichettaindex'); |
||
| 43 | $tabelle->setEtichettaindex($etichettaindex); |
||
| 44 | |||
| 45 | $larghezzaindex = $this->getRequestValue($request, 'larghezzaindex'); |
||
| 46 | $tabelle->setLarghezzaindex($larghezzaindex); |
||
| 47 | |||
| 48 | $mostrastampa = $this->getRequestValue($request, 'mostrastampa'); |
||
| 49 | $tabelle->setMostrastampa($mostrastampa); |
||
| 50 | |||
| 51 | $ordinestampa = $this->getRequestValue($request, 'ordinestampa'); |
||
| 52 | $tabelle->setOrdinestampa($ordinestampa); |
||
| 53 | |||
| 54 | $etichettastampa = $this->getRequestValue($request, 'etichettastampa'); |
||
| 55 | $tabelle->setEtichettastampa($etichettastampa); |
||
| 56 | |||
| 57 | $larghezzastampa = $this->getRequestValue($request, 'larghezzastampa'); |
||
| 58 | $tabelle->setLarghezzastampa($larghezzastampa); |
||
| 59 | $em->persist($tabelle); |
||
| 60 | $em->flush(); |
||
| 61 | } |
||
| 62 | |||
| 63 | return new Response('OK'); |
||
| 64 | } |
||
| 65 | |||
| 66 | private function getRequestValue($request, $attribute) |
||
| 74 | |||
| 75 | 1 | public function configuraAction(Request $request, $nometabella) |
|
| 76 | { |
||
| 77 | 1 | $this->setup($request); |
|
| 78 | 1 | $gestionepermessi = $this->get("ficorebundle.gestionepermessi"); |
|
| 79 | 1 | $operatore = $gestionepermessi->utentecorrente(); |
|
| 80 | 1 | $this->generaDB(array('tabella' => $nometabella), $request); |
|
| 81 | 1 | $this->generaDB(array('tabella' => $nometabella, 'operatore' => $operatore['id']), $request); |
|
| 82 | |||
| 83 | 1 | $namespace = $this->getNamespace(); |
|
| 84 | 1 | $bundle = $this->getBundle(); |
|
| 85 | 1 | $controller = $this->getController(); |
|
| 86 | 1 | $container = $this->container; |
|
| 87 | |||
| 88 | 1 | $nomebundle = $namespace . $bundle . 'Bundle'; |
|
| 89 | |||
| 90 | 1 | $em = $this->getDoctrine()->getManager(); |
|
| 91 | 1 | $entities = $em->getRepository($nomebundle . ':' . $controller)->findAll(); |
|
| 92 | |||
| 93 | $dettaglij = array( |
||
| 94 | 'nomecampo' => array( |
||
| 95 | 1 | array('nomecampo' => 'nomecampo', 'lunghezza' => '150', 'descrizione' => 'Campo', 'tipo' => 'text', 'editable' => false),), |
|
| 96 | 'mostraindex' => array( |
||
| 97 | 1 | array('nomecampo' => 'mostraindex', 'lunghezza' => '100', 'descrizione' => 'Vedi in griglia', 'tipo' => 'boolean'), |
|
| 98 | 1 | ), |
|
| 99 | 'ordineindex' => array( |
||
| 100 | 1 | array('nomecampo' => 'ordineindex', 'lunghezza' => '100', 'descrizione' => 'Ordine in griglia', 'tipo' => 'text'), |
|
| 101 | 1 | ), |
|
| 102 | 'etichettaindex' => array( |
||
| 103 | 1 | array('nomecampo' => 'etichettaindex', 'lunghezza' => '150', 'descrizione' => 'Label in griglia', 'tipo' => 'text'), |
|
| 104 | 1 | ), |
|
| 105 | 'larghezzaindex' => array( |
||
| 106 | 1 | array('nomecampo' => 'larghezzaindex', 'lunghezza' => '100', 'descrizione' => 'Largh. in griglia', 'tipo' => 'text'), |
|
| 107 | 1 | ), |
|
| 108 | 'mostrastampa' => array( |
||
| 109 | 1 | array('nomecampo' => 'mostrastampa', 'lunghezza' => '100', 'descrizione' => 'Vedi in stampa', 'tipo' => 'boolean'), |
|
| 110 | 1 | ), |
|
| 111 | 'ordinestampa' => array( |
||
| 112 | 1 | array('nomecampo' => 'ordinestampa', 'lunghezza' => '100', 'descrizione' => 'Ordine in stampa', 'tipo' => 'text'), |
|
| 113 | 1 | ), |
|
| 114 | 'etichettastampa' => array( |
||
| 115 | 1 | array('nomecampo' => 'etichettastampa', 'lunghezza' => '150', 'descrizione' => 'Label in stampa', 'tipo' => 'text'), |
|
| 116 | 1 | ), |
|
| 117 | 'larghezzastampa' => array( |
||
| 118 | 1 | array('nomecampo' => 'larghezzastampa', 'lunghezza' => '100', 'descrizione' => 'Largh. in stampa', 'tipo' => 'text'), |
|
| 119 | 1 | ), |
|
| 120 | 1 | ); |
|
| 121 | |||
| 122 | $paricevuti = array( |
||
| 123 | 1 | 'doctrine' => $em, |
|
| 124 | 1 | 'nomebundle' => $nomebundle, |
|
| 125 | 1 | 'nometabella' => $controller, |
|
| 126 | 1 | 'dettaglij' => $dettaglij, |
|
| 127 | 1 | 'container' => $container, |
|
| 128 | 1 | ); |
|
| 129 | |||
| 130 | 1 | $paricevuti['escludere'] = array('nometabella', 'operatori_id'); |
|
| 131 | |||
| 132 | 1 | $testata = Griglia::testataPerGriglia($paricevuti); |
|
| 133 | |||
| 134 | 1 | $testata['titolo'] = "Configurazione colonne per tabella $nometabella"; |
|
| 135 | 1 | $testata['multisearch'] = 0; |
|
| 136 | 1 | $testata['showdel'] = 0; |
|
| 137 | 1 | $testata['showadd'] = 0; |
|
| 138 | 1 | $testata['showedit'] = 0; |
|
| 139 | 1 | $testata['showprint'] = 0; |
|
| 140 | 1 | $testata['editinline'] = 1; |
|
| 141 | 1 | $testata['nomelist'] = '#listconfigura'; |
|
| 142 | 1 | $testata['nomepager'] = '#pagerconfigura'; |
|
| 143 | 1 | $testata['tastochiudi'] = 1; |
|
| 144 | 1 | $testata['div'] = '#dettaglioconf'; |
|
| 145 | 1 | $testata['chiamante'] = $nometabella; |
|
| 146 | 1 | $testata['percorsogriglia'] = $nometabella . '/grigliapopup'; |
|
| 147 | 1 | $testata['altezzagriglia'] = '300'; |
|
| 148 | 1 | $testata['larghezzagriglia'] = '900'; |
|
| 149 | |||
| 150 | 1 | $testata['permessiedit'] = 1; |
|
| 151 | 1 | $testata['permessidelete'] = 1; |
|
| 152 | 1 | $testata['permessicreate'] = 1; |
|
| 153 | 1 | $testata['permessiread'] = 1; |
|
| 154 | $twigparm = array( |
||
| 155 | 1 | 'entities' => $entities, |
|
| 156 | 1 | 'nomecontroller' => $controller, |
|
| 157 | 1 | 'testata' => json_encode($testata), |
|
| 158 | 1 | 'chiamante' => $nometabella, |
|
| 159 | 1 | ); |
|
| 160 | |||
| 161 | 1 | return $this->render($nomebundle . ':' . $controller . ':configura.html.twig', $twigparm); |
|
| 162 | } |
||
| 163 | |||
| 164 | 1 | public function generaDB($parametri, Request $request) |
|
| 165 | { |
||
| 166 | 1 | if (!isset($parametri['tabella'])) { |
|
| 167 | return false; |
||
| 168 | } |
||
| 169 | |||
| 170 | 1 | $this->setup($request); |
|
| 171 | 1 | $namespace = $this->getNamespace(); |
|
| 172 | 1 | $bundle = $this->getBundle(); |
|
| 173 | |||
| 174 | 1 | $nomebundle = $namespace . $bundle . 'Bundle'; |
|
| 175 | |||
| 176 | 1 | $nometabella = $parametri['tabella']; |
|
| 177 | 1 | $em = $this->getDoctrine()->getManager(); |
|
| 178 | |||
| 179 | 1 | $bundles = $this->get('kernel')->getBundles(); |
|
| 180 | 1 | $tableClassName = ""; |
|
| 181 | 1 | $entityClass = ""; |
|
| 182 | 1 | foreach ($bundles as $bundle) { |
|
| 183 | 1 | $className = get_class($bundle); |
|
| 184 | 1 | $entityClass = substr($className, 0, strrpos($className, '\\')); |
|
| 185 | 1 | $tableClassName = '\\' . $entityClass . '\\Entity\\' . $nometabella; |
|
| 186 | 1 | if (!class_exists($tableClassName)) { |
|
| 187 | 1 | $tableClassName = ''; |
|
| 188 | 1 | continue; |
|
| 189 | } else { |
||
| 190 | 1 | break; |
|
| 191 | } |
||
| 192 | 1 | } |
|
| 193 | |||
| 194 | 1 | if (!$tableClassName) { |
|
| 195 | throw new \Exception('Entity per la tabella ' . $nometabella . ' non trovata', '-1'); |
||
| 196 | } |
||
| 197 | |||
| 198 | 1 | if (!$entityClass) { |
|
| 199 | throw new \Exception('Entity class per la tabella ' . $nometabella . ' non trovata', '-1'); |
||
| 200 | } |
||
| 201 | |||
| 202 | 1 | $bundleClass = str_replace('\\', '', $entityClass); |
|
| 203 | |||
| 204 | 1 | $c = $em->getClassMetadata($bundleClass . ':' . $nometabella); |
|
| 205 | |||
| 206 | 1 | $colonne = $c->getColumnNames(); |
|
| 207 | 1 | $this->scriviDB($colonne, $nometabella, $nomebundle, $parametri); |
|
| 208 | 1 | } |
|
| 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 | 1 | ); |
|
| 217 | |||
| 218 | 1 | if (isset($parametri['operatore'])) { |
|
| 219 | 1 | $vettorericerca['operatori_id'] = $parametri['operatore']; |
|
| 220 | 1 | } |
|
| 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 | 1 | } |
|
| 227 | 1 | } |
|
| 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 | 1 | } |
|
| 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 | 1 | } |
|
| 250 | 1 | } 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) |
||
| 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(); |
|
| 321 | |||
| 322 | public function listacampitabellaAction(Request $request) |
||
| 374 | |||
| 375 | private function listacampitabelladettagli($escludiid, $colonne, $nomebundle, $controller) |
||
| 431 | } |
||
| 432 |