Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 29 | abstract class BaseController extends Controller |
||
| 30 | { |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Genera un objeto documento PDF |
||
| 34 | * |
||
| 35 | * @param $titulo |
||
| 36 | * @param $logos |
||
| 37 | * @param $plantilla |
||
| 38 | * @param $margen |
||
| 39 | * @param $codigo |
||
| 40 | * @return TCPDF |
||
| 41 | */ |
||
| 42 | protected function generarPdf($titulo, $logos, $plantilla, $margen = 0, $codigo = null) |
||
| 43 | { |
||
| 44 | $pdf = $this->get('white_october.tcpdf')->create(); |
||
| 45 | $pdf->SetCreator(PDF_CREATOR); |
||
| 46 | $pdf->SetAuthor('Gestconv'); |
||
| 47 | $pdf->SetTitle($titulo); |
||
| 48 | $pdf->SetKeywords(''); |
||
| 49 | $pdf->SetExtendedHeaderData( |
||
| 50 | array( |
||
| 51 | $logos['centro'], |
||
| 52 | $logos['organizacion'], |
||
| 53 | $logos['sello'] |
||
| 54 | ), |
||
| 55 | array( |
||
| 56 | $this->container->getParameter('centro') . ' - ' . $this->container->getParameter('localidad'), |
||
| 57 | $plantilla['proceso'], |
||
| 58 | $plantilla['descripcion'], |
||
| 59 | $plantilla['modelo'], |
||
| 60 | $plantilla['revision'] |
||
| 61 | ) |
||
| 62 | ); |
||
| 63 | if ($codigo) { |
||
| 64 | $pdf->setBarcode($codigo); |
||
| 65 | } |
||
| 66 | $pdf->setFooterData(array(0, 0, 128), array(0, 64, 128)); |
||
| 67 | $pdf->SetHeaderMargin(PDF_MARGIN_HEADER + $plantilla['margen']); |
||
| 68 | $pdf->SetFooterMargin(PDF_MARGIN_FOOTER); |
||
| 69 | |||
| 70 | // mostrar cabecera |
||
| 71 | $pdf->setPrintHeader(true); |
||
| 72 | $pdf->setPrintFooter(true); |
||
| 73 | |||
| 74 | // set default monospaced font |
||
| 75 | $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); |
||
| 76 | |||
| 77 | // set margins |
||
| 78 | $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); |
||
| 79 | |||
| 80 | // set auto page breaks |
||
| 81 | $pdf->SetAutoPageBreak(true, PDF_MARGIN_BOTTOM - $margen); |
||
| 82 | |||
| 83 | $pdf->SetFont('helvetica', '', 10, '', true); |
||
| 84 | |||
| 85 | $pdf->AddPage(); |
||
| 86 | |||
| 87 | return $pdf; |
||
| 88 | } |
||
| 89 | |||
| 90 | protected function notificarParte($usuarios, Parte $parte) |
||
| 91 | { |
||
| 92 | $enviados = 0; |
||
| 93 | $mailer = $this->get('mailer'); |
||
| 94 | $adjunto = null; |
||
| 95 | |||
| 96 | foreach($usuarios as $usuario) { |
||
| 97 | if ($usuario->getEstaActivo() && $usuario->getNotificaciones() && $usuario->getEmail()) { |
||
| 98 | |||
| 99 | View Code Duplication | if (is_null($adjunto)) { |
|
| 100 | $plantilla = $this->container->getParameter('parte'); |
||
| 101 | $logos = $this->container->getParameter('logos'); |
||
| 102 | |||
| 103 | $pdf = $this->generarPdf('Parte #' . $parte->getId(), $logos, $plantilla, 0, 'P' . $parte->getId()); |
||
| 104 | |||
| 105 | $html = $this->renderView('AppBundle:Parte:imprimir.html.twig', |
||
| 106 | array( |
||
| 107 | 'parte' => $parte, |
||
| 108 | 'usuario' => $usuario, |
||
| 109 | 'localidad' => $this->container->getParameter('localidad') |
||
| 110 | )); |
||
| 111 | |||
| 112 | $pdf->writeHTML($html); |
||
| 113 | |||
| 114 | $adjunto = Swift_Attachment::newInstance($pdf->getPDFData(), 'P' . $parte->getId() . '.pdf', 'application/pdf')->setDisposition('inline'); |
||
| 115 | } |
||
| 116 | |||
| 117 | $mensaje = $mailer->createMessage() |
||
| 118 | ->setSubject( |
||
| 119 | $this->container->getParameter('prefijo_notificacion') . ' Nuevo parte notificado de ' . $parte->getAlumno() . |
||
| 120 | ($parte->getPrioritario() ? " (PRIORITARIO)" : "") |
||
| 121 | ) |
||
| 122 | ->setFrom($this->container->getParameter('remite_notificacion')) |
||
| 123 | ->setTo(array($usuario->getEmail() => $usuario->__toString())) |
||
| 124 | ->setBody('La familia del estudiante ' . $parte->getAlumno() . ' ha sido notificada del parte que se incluye adjunto en el mensaje.') |
||
| 125 | ->attach($adjunto); |
||
| 126 | |||
| 127 | $enviados++; |
||
| 128 | |||
| 129 | $mailer->send($mensaje); |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | return $enviados; |
||
| 134 | } |
||
| 135 | |||
| 136 | protected function notificarSancion($usuarios, Sancion $sancion) |
||
| 182 | |||
| 183 | protected function notificar($usuarios, $titulo, $cuerpo) |
||
| 205 | } |
||
| 206 |