ParteRepository   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 224
Duplicated Lines 5.8 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 22
c 2
b 0
f 0
lcom 1
cbo 4
dl 13
loc 224
ccs 0
cts 191
cp 0
rs 10

21 Methods

Rating   Name   Duplication   Size   Complexity  
A countAll() 0 9 1
A findAllSancionablesPorAlumno() 0 15 1
A findNoNotificados() 0 8 1
A findPorUsuario() 0 8 1
A findNoNotificadosPorUsuario() 0 5 1
A findAllNoNotificadosPorUsuario() 0 7 1
A countNoNotificadosPorUsuario() 0 7 1
A countNoNotificadosPorUsuarioOTutoria() 0 7 1
A findAllOrdered() 0 9 1
A findNotificados() 0 8 1
A findAllNoNotificadosPorAlumnoYUsuario() 0 10 1
A findAllNoNotificadosPorAlumno() 0 9 1
A findPorUsuarioOTutoria() 0 17 1
A findNoNotificadosPorUsuarioOTutoria() 13 13 1
A findAllPorUsuarioOTutoria() 0 7 1
A countNoNotificados() 0 7 1
A countPorUsuarioOTutoria() 0 7 1
A countPorUsuario() 0 7 1
A countSancionables() 0 9 1
A countSancionablesPrioritarios() 0 10 1
A findPrescritos() 0 21 2

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\Entity;
22
23
use Doctrine\ORM\EntityRepository;
24
25
/**
26
 * ParteRepository
27
 *
28
 * Clase repositorio para añadir métodos adicionales
29
 */
30
class ParteRepository extends EntityRepository
31
{
32
    public function findAllOrdered()
33
    {
34
        return $this->getEntityManager()
35
            ->getRepository('AppBundle:Parte')
36
            ->createQueryBuilder('p')
37
            ->orderBy('p.fechaSuceso', 'DESC')
38
            ->getQuery()
39
            ->getResult();
40
    }
41
42
    public function findNotificados()
43
    {
44
        return $this->getEntityManager()
45
            ->getRepository('AppBundle:Parte')
46
            ->createQueryBuilder('p')
47
            ->innerJoin('p.alumno', 'a')
48
            ->where('p.fechaAviso IS NOT NULL');
49
    }
50
51
    public function findNoNotificados()
52
    {
53
        return $this->getEntityManager()
54
            ->getRepository('AppBundle:Parte')
55
            ->createQueryBuilder('p')
56
            ->innerJoin('p.alumno', 'a')
57
            ->where('p.fechaAviso IS NULL');
58
    }
59
60
    public function findAllNoNotificadosPorAlumnoYUsuario($alumno, $usuario)
61
    {
62
        return $this->findNoNotificadosPorUsuarioOTutoria($usuario)
63
            ->andWhere('p.alumno = :alumno')
64
            ->setParameter('usuario', $usuario)
65
            ->setParameter('alumno', $alumno)
66
            ->orderBy('p.fechaSuceso', 'DESC')
67
            ->getQuery()
68
            ->getResult();
69
    }
70
71
    public function findAllNoNotificadosPorAlumno($alumno)
72
    {
73
        return $this->findNoNotificados()
74
            ->andWhere('p.alumno = :alumno')
75
            ->setParameter('alumno', $alumno)
76
            ->orderBy('p.fechaSuceso', 'DESC')
77
            ->getQuery()
78
            ->getResult();
79
    }
80
81
    public function findPorUsuarioOTutoria($usuario)
82
    {
83
        $orX = $this->getEntityManager()->createQueryBuilder()
84
            ->expr()->orX()
85
            ->add('p.usuario = :usuario')
86
            ->add('a.grupo = :grupo');
87
88
        return $this->getEntityManager()
89
            ->getRepository('AppBundle:Parte')
90
            ->createQueryBuilder('p')
91
            ->select('p')
92
            ->innerJoin('p.alumno', 'a')
93
            ->innerJoin('AppBundle:Grupo', 'g', 'WITH', 'a.grupo = g')
94
            ->andWhere($orX)
95
            ->setParameter('usuario', $usuario)
96
            ->setParameter('grupo', $usuario->getTutoria());
97
    }
98
99
    public function findPorUsuario($usuario)
100
    {
101
        return $this->getEntityManager()
102
            ->getRepository('AppBundle:Parte')
103
            ->createQueryBuilder('p')
104
            ->where('p.usuario = :usuario')
105
            ->setParameter('usuario', $usuario);
106
    }
107
108
    public function findNoNotificadosPorUsuario($usuario)
109
    {
110
        return $this->findPorUsuario($usuario)
111
            ->andWhere('p.fechaAviso IS NULL');
112
    }
113
114 View Code Duplication
    public function findNoNotificadosPorUsuarioOTutoria($usuario)
115
    {
116
        $orX = $this->getEntityManager()->createQueryBuilder()
117
            ->expr()->orX()
118
            ->add('p.usuario = :usuario')
119
            ->add('a.grupo = :grupo');
120
121
        return $this->findNoNotificados()
122
            ->innerJoin('AppBundle:Grupo', 'g', 'WITH', 'a.grupo = g')
123
            ->andWhere($orX)
124
            ->setParameter('usuario', $usuario)
125
            ->setParameter('grupo', $usuario->getTutoria());
126
    }
127
128
    public function findAllNoNotificadosPorUsuario($usuario)
129
    {
130
        return $this->findNoNotificadosPorUsuario($usuario)
131
            ->orderBy('p.fechaSuceso', 'DESC')
132
            ->getQuery()
133
            ->getResult();
134
    }
135
136
    public function findAllPorUsuarioOTutoria($usuario)
137
    {
138
        return $this->findPorUsuarioOTutoria($usuario)
139
            ->orderBy('p.fechaSuceso', 'DESC')
140
            ->getQuery()
141
            ->getResult();
142
    }
143
144
    public function countAll()
145
    {
146
        return $this->getEntityManager()
147
            ->getRepository('AppBundle:Parte')
148
            ->createQueryBuilder('p')
149
            ->select('COUNT(p.id)')
150
            ->getQuery()
151
            ->getSingleScalarResult();
152
    }
153
154
    public function countNoNotificados()
155
    {
156
        return $this->findNoNotificados()
157
            ->select('COUNT(p.id)')
158
            ->getQuery()
159
            ->getSingleScalarResult();
160
    }
161
162
    public function countNoNotificadosPorUsuario($usuario)
163
    {
164
        return $this->findNoNotificadosPorUsuario($usuario)
165
            ->select('COUNT(p.id)')
166
            ->getQuery()
167
            ->getSingleScalarResult();
168
    }
169
170
    public function countNoNotificadosPorUsuarioOTutoria($usuario)
171
    {
172
        return $this->findNoNotificadosPorUsuarioOTutoria($usuario)
173
            ->select('COUNT(p.id)')
174
            ->getQuery()
175
            ->getSingleScalarResult();
176
    }
177
178
    public function countPorUsuarioOTutoria($usuario)
179
    {
180
        return $this->findPorUsuarioOTutoria($usuario)
181
            ->select('COUNT(p.id)')
182
            ->getQuery()
183
            ->getSingleScalarResult();
184
    }
185
186
187
    public function countPorUsuario($usuario)
188
    {
189
        return $this->findPorUsuario($usuario)
190
            ->select('COUNT(p.id)')
191
            ->getQuery()
192
            ->getSingleScalarResult();
193
    }
194
195
    public function countSancionables()
196
    {
197
        return $this->findNotificados()
198
            ->select('COUNT(p.id)')
199
            ->andWhere('p.prescrito = false')
200
            ->andWhere('p.sancion IS NULL')
201
            ->getQuery()
202
            ->getSingleScalarResult();
203
    }
204
205
    public function countSancionablesPrioritarios()
206
    {
207
        return $this->findNotificados()
208
            ->select('COUNT(p.id)')
209
            ->andWhere('p.prescrito = false')
210
            ->andWhere('p.sancion IS NULL')
211
            ->andWhere('p.prioritario = true')
212
            ->getQuery()
213
            ->getSingleScalarResult();
214
    }
215
216
    public function findAllSancionablesPorAlumno($alumno)
217
    {
218
        return $this->getEntityManager()
219
            ->createQueryBuilder()
220
            ->select('p')
221
            ->from('AppBundle\Entity\Parte', 'p', 'p.id')
222
            ->andWhere('p.fechaAviso IS NOT NULL')
223
            ->andWhere('p.sancion IS NULL')
224
            ->andWhere('p.prescrito = false')
225
            ->andWhere('p.alumno = :alumno')
226
            ->orderBy('p.fechaSuceso')
227
            ->setParameter('alumno', $alumno)
228
            ->getQuery()
229
            ->getResult();
230
    }
231
232
    public function findPrescritos($plazo, $filtrarRecordados = false)
233
    {
234
        $fecha = new \DateTime();
235
        $fecha->sub(new \DateInterval('P' . $plazo . 'D'));
236
237
        $data = $this->getEntityManager()
238
            ->getRepository('AppBundle:Parte')
239
            ->createQueryBuilder('p')
240
            ->where('p.sancion IS NULL')
241
            ->andWhere('p.prescrito = false')
242
            ->andWhere('p.fechaSuceso < :fechaLimite');
243
244
        if ($filtrarRecordados) {
245
            $data = $data->andWhere('p.fechaRecordatorio IS NULL');
246
        }
247
248
        return $data
249
            ->setParameter('fechaLimite', $fecha)
250
            ->getQuery()
251
            ->getResult();
252
    }
253
}
254