CronCommand   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 95
Duplicated Lines 33.68 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 32
loc 95
ccs 0
cts 55
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
B notificar() 12 23 6
A notificarSobreParte() 0 7 1
B execute() 20 37 3

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\Command;
22
23
use AppBundle\Entity\Usuario;
24
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
25
use Symfony\Component\Console\Input\InputInterface;
26
use Symfony\Component\Console\Output\OutputInterface;
27
28
class CronCommand extends ContainerAwareCommand
29
{
30
    protected function configure()
31
    {
32
        $this
33
            ->setName('gestconv:cron')
34
            ->setDescription('Execute cron tasks')
35
        ;
36
    }
37
38
    /**
39
     * @param OutputInterface $output
40
     * @param Usuario[]|Doctrine\Common\Collections\Collection $usuarios
41
     * @param string $titulo
42
     * @param string $cuerpo
43
     * @param $rol
44
     * @return int
45
     */
46
    private function notificar($output, $usuarios, $titulo, $cuerpo, $rol)
47
    {
48
        $enviados = 0;
49
        $mailer = $this->getContainer()->get('mailer');
50
51
        foreach($usuarios as $usuario) {
52
            $output->writeln('  - Notificando a ' . $usuario . ($rol ? ' (' . $rol . ')' : ''));
53 View Code Duplication
            if ($usuario->getEstaActivo() && $usuario->getNotificaciones() && $usuario->getEmail()) {
54
55
                $mensaje = $mailer->createMessage()
56
                    ->setSubject($this->getContainer()->getParameter('prefijo_notificacion') . ' ' . $titulo)
57
                    ->setFrom($this->getContainer()->getParameter('remite_notificacion'))
58
                    ->setTo(array($usuario->getEmail() => $usuario->__toString()))
59
                    ->setBody($cuerpo);
60
61
                $enviados++;
62
63
                $mailer->send($mensaje);
64
            }
65
        }
66
67
        return $enviados;
68
    }
69
70
    /**
71
     * @param OutputInterface $output
72
     * @param $parte
73
     * @param $titulo
74
     * @param $mensaje
75
     * @param $em
76
     */
77
    private function notificarSobreParte(OutputInterface $output, $parte, $titulo, $mensaje, $em)
78
    {
79
        $output->writeln('* ' . $parte->getId() . ": " . $parte->getAlumno() . ' - ' . $parte->getFechaSuceso()->format('d/m/Y'));
80
        $this->notificar($output, $parte->getAlumno()->getGrupo()->getTutores(), $titulo, $mensaje, 'tutor/a');
81
        $this->notificar($output, array($parte->getUsuario()), $titulo, $mensaje, 'autor/a');
82
        $this->notificar($output, $em->getRepository('AppBundle:Usuario')->getRevisores(), $titulo, $mensaje, 'comisionario/a');
83
    }
84
85
    protected function execute(InputInterface $input, OutputInterface $output)
86
    {
87
        $output->writeln('<info>Ejecutando tareas de cron</info>');
88
        $em = $this->getContainer()->get('doctrine')->getManager();
89
90
        $output->writeln('<info>Localizando partes prescritos</info>');
91
92
        $partes = $em->getRepository('AppBundle:Parte')->findPrescritos($this->getContainer()->getParameter('dias_prescripcion'));
93
94 View Code Duplication
        foreach($partes as $parte) {
95
            $parte->setPrescrito(true);
96
97
            $titulo = 'Parte prescrito de ' . $parte->getAlumno();
98
99
            $mensaje = 'El parte #' . $parte->getId() . ' de ' . $parte->getAlumno() . ' con fecha ' . $parte->getFechaSuceso()->format('d/m/Y') .
100
                ' ha prescrito y no podrá ser sancionado.';
101
102
            $this->notificarSobreParte($output, $parte, $titulo, $mensaje, $em);
103
        }
104
        $em->flush();
105
106
        $output->writeln('<info>Localizando partes a punto de prescribir</info>');
107
108
        $partes = $em->getRepository('AppBundle:Parte')->findPrescritos($this->getContainer()->getParameter('dias_aviso_previo'), true);
109
110 View Code Duplication
        foreach($partes as $parte) {
111
            $parte->setFechaRecordatorio(new \DateTime());
112
113
            $titulo = 'Recordatorio: Parte a punto de prescribir de ' . $parte->getAlumno();
114
115
            $mensaje = 'El parte #' . $parte->getId() . ' de ' . $parte->getAlumno() . ' con fecha ' . $parte->getFechaSuceso()->format('d/m/Y') .
116
                ' está próximo a ser marcado como prescrito y no podrá ser sancionado.';
117
118
            $this->notificarSobreParte($output, $parte, $titulo, $mensaje, $em);
119
        }
120
        $em->flush();
121
    }
122
}
123