Completed
Push — master ( 3f8a04...dc1f90 )
by Luis Ramón
10:05
created

OrganizationRepository::findFirstByUserOrNull()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
/*
3
  ÁTICA - Aplicación web para la gestión documental de centros educativos
4
5
  Copyright (C) 2015-2017: 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\Repository;
22
23
use AppBundle\Entity\Organization;
24
use AppBundle\Entity\User;
25
use Doctrine\ORM\EntityRepository;
26
27
class OrganizationRepository extends EntityRepository
28
{
29
    /**
30
     * Devuelve las organizaciones a las que pertenece el usuario en la fecha indicada.
31
     * Si no se especifica fecha, se devuelven todas a las que pertenece.
32
     *
33
     * @param User $user
34
     * @param \DateTime $date
35
     * @return \Doctrine\ORM\QueryBuilder
36
     */
37
    public function getMembershipByUserQueryBuilder(User $user, \DateTime $date = null)
38
    {
39
        if ($user->isGlobalAdministrator()) {
40
            return $this->createQueryBuilder('o')
41
                ->orderBy('o.name');
42
        }
43
44
        $query = $this->createQueryBuilder('o')
45
            ->join('o.memberships', 'm');
46
47
        if ($date) {
48
            $query = $query
49
                ->andWhere('m.validUntil >= :date')
50
                ->orWhere('m.validUntil IS NULL')
51
                ->andWhere('m.validFrom <= :date')
52
                ->setParameter('date', $date);
53
        }
54
        return $query
55
            ->andWhere('m.user = :user')
56
            ->setParameter('user', $user)
57
            ->orderBy('o.name');
58
59
    }
60
61
    /**
62
     * Devuelve la primera organización a la que pertenece el usuario indicado en la fecha pasada
63
     * como parámetro.
64
     *
65
     * @param User $user
66
     * @param \DateTime $date
67
     * @return Organization|null
68
     */
69
    public function findFirstByUserOrNull(User $user, \DateTime $date = null)
70
    {
71
        $query = $this->getMembershipByUserQueryBuilder($user, $date);
72
        return $query
73
            ->getQuery()
74
            ->getOneOrNullResult();
75
    }
76
77
    /**
78
     * Devuelve el número de organizaciones a las que pertenece un usuario en una fecha determinada.
79
     *
80
     * @param User $user
81
     * @param \DateTime $date
82
     * @return int
83
     */
84
    public function countOrganizationsByUser(User $user, \DateTime $date = null)
85
    {
86
        $query = $this->getMembershipByUserQueryBuilder($user, $date);
87
        return $query
88
            ->select('COUNT(o)')
89
            ->getQuery()
90
            ->getSingleScalarResult();
91
    }
92
93
    public function count() {
94
        $query = $this->createQueryBuilder('o')->select('count(o)')->getQuery();
95
        return $query->getSingleScalarResult();
96
    }
97
98
    /**
99
     * Pasado un array de ids de organizaciones, devolver la lista de objetos exceptuando la organización actual
100
     * @param $items
101
     * @param Organization $organization
102
     * @return array
103
     */
104
    public function findAllInListByIdButCurrent($items, Organization $organization) {
105
        return $this->createQueryBuilder('o')
106
            ->where('o.id IN (:items)')
107
            ->andWhere('o != :current')
108
            ->setParameter('items', $items)
109
            ->setParameter('current', $organization)
110
            ->orderBy('o.code')
111
            ->getQuery()
112
            ->getResult();
113
    }
114
}
115