BaseController   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 177
Duplicated Lines 26.55 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 20
c 2
b 0
f 0
lcom 1
cbo 5
dl 47
loc 177
ccs 0
cts 111
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B notificarSancion() 18 46 6
B notificar() 12 22 5
A generarPdf() 0 47 2
C notificarParte() 17 45 7

How to fix   Duplicated Code   

Duplicated Code

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
2
/*
3
  GESTCONV - Aplicación web para la gestión de la convivencia en centros educativos
4
5
  Copyright (C) 2015: Luis Ramón López López
6
7
  This program is free software: you can redistribute it and/or modify
8
  it under the terms of the GNU Affero General Public License as published by
9
  the Free Software Foundation, either version 3 of the License, or
10
  (at your option) any later version.
11
12
  This program is distributed in the hope that it will be useful,
13
  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
  GNU Affero General Public License for more details.
16
17
  You should have received a copy of the GNU Affero General Public License
18
  along with this program.  If not, see [http://www.gnu.org/licenses/].
19
*/
20
21
namespace AppBundle\Controller;
22
23
use AppBundle\Entity\Parte;
24
use AppBundle\Entity\Sancion;
25
use Swift_Attachment;
26
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
27
use TCPDF;
28
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)
137
    {
138
        $enviados = 0;
139
        $mailer = $this->get('mailer');
140
        $adjunto = null;
141
142
        foreach($usuarios as $usuario) {
143
            if ($usuario->getEstaActivo() && $usuario->getNotificaciones() && $usuario->getEmail()) {
144
145 View Code Duplication
                if (is_null($adjunto)) {
146
                    $plantilla = $this->container->getParameter('sancion');
147
                    $logos = $this->container->getParameter('logos');
148
149
                    $pdf = $this->generarPdf('Sancion #' . $sancion->getId(), $logos, $plantilla, -15, 'S' . $sancion->getId());
150
151
                    $html = $this->renderView('AppBundle:Sancion:imprimir.html.twig',
152
                        array(
153
                            'sancion' => $sancion,
154
                            'usuario' => $usuario,
155
                            'localidad' => $this->container->getParameter('localidad'),
156
                            'director' => $this->container->getParameter('director')
157
                        ));
158
159
                    $pdf->writeHTML($html);
160
161
                    $adjunto = Swift_Attachment::newInstance($pdf->getPDFData(), 'S' . $sancion->getId() . '.pdf', 'application/pdf')->setDisposition('inline');
162
                }
163
164
                $partes = $sancion->getPartes();
165
                $alumno = $partes[0]->getAlumno();
166
167
                $mensaje = $mailer->createMessage()
168
                    ->setSubject($this->container->getParameter('prefijo_notificacion') . ' Nueva sanción a ' . $alumno)
169
                    ->setFrom($this->container->getParameter('remite_notificacion'))
170
                    ->setTo(array($usuario->getEmail() => $usuario->__toString()))
171
                    ->setBody('La comisión de convivencia ha sancionado al alumno/a ' . $alumno . '. Los detalles se incluyen en el documento adjunto.')
172
                    ->attach($adjunto);
173
174
                $enviados++;
175
176
                $mailer->send($mensaje);
177
            }
178
        }
179
180
        return $enviados;
181
    }
182
183
    protected function notificar($usuarios, $titulo, $cuerpo)
184
    {
185
        $enviados = 0;
186
        $mailer = $this->get('mailer');
187
188
        foreach($usuarios as $usuario) {
189 View Code Duplication
            if ($usuario->getEstaActivo() && $usuario->getNotificaciones() && $usuario->getEmail()) {
190
191
                $mensaje = $mailer->createMessage()
192
                    ->setSubject($this->container->getParameter('prefijo_notificacion') . ' ' . $titulo)
193
                    ->setFrom($this->container->getParameter('remite_notificacion'))
194
                    ->setTo(array($usuario->getEmail() => $usuario->__toString()))
195
                    ->setBody($cuerpo);
196
197
                $enviados++;
198
199
                $mailer->send($mensaje);
200
            }
201
        }
202
203
        return $enviados;
204
    }
205
}
206