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

UserRepository::findByOrganizationAndDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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
use Doctrine\ORM\QueryBuilder;
27
use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface;
28
use Symfony\Component\Security\Core\User\UserInterface;
29
30
class UserRepository extends EntityRepository implements UserLoaderInterface
31
{
32
    /**
33
     * Loads the user for the given username.
34
     *
35
     * This method must return null if the user is not found.
36
     *
37
     * @param string $username The username
38
     *
39
     * @return UserInterface|null
40
     */
41
    public function loadUserByUsername($username) {
42
        if (!$username) {
43
            return null;
44
        }
45
        return $this->getEntityManager()
46
            ->createQuery('SELECT u FROM AppBundle:User u
47
                           WHERE u.loginUsername = :username
48
                           OR u.emailAddress = :username')
49
            ->setParameters([
50
                'username' => $username
51
            ])
52
            ->setMaxResults(1)
53
            ->getOneOrNullResult();
54
    }
55
56
    /**
57
     * @param UserInterface $user
58
     * @return null|UserInterface
59
     */
60
    public function refreshUser(UserInterface $user) {
61
        return $this->loadUserByUsername($user->getUsername());
62
    }
63
64
    /**
65
     * @param $class
66
     * @return bool
67
     */
68
    public function supportsClass($class) {
69
        return $class === User::class;
70
    }
71
72
    /**
73
     * @param Organization $organization
74
     * @param \DateTime|null $date
75
     * @return array
76
     */
77
    public function findByOrganizationAndDate(Organization $organization, $date = null)
78
    {
79
        return $this->getOrganizationAndDateQueryBuilder($organization, $date)->getQuery()->getResult();
80
    }
81
82
    /**
83
     * @param Organization $organization
84
     * @param \DateTime|null $date
85
     * @return QueryBuilder
86
     */
87
    public function getOrganizationAndDateQueryBuilder(Organization $organization, $date = null)
88
    {
89
        $query = $this->createQueryBuilder('u')
90
            ->distinct()
91
            ->join('u.memberships', 'm')
92
            ->where('m.organization = :organization')
93
            ->setParameter('organization', $organization);
94
95
        if ($date) {
96
            $query = $query
97
                ->andWhere('m.validUntil >= :date')
98
                ->orWhere('m.validUntil IS NULL')
99
                ->andWhere('m.validFrom <= :date')
100
                ->setParameter('date', $date);
101
        }
102
103
        return $query;
104
    }
105
106
    /**
107
     * @param Organization $organization
108
     * @param string $fullName
109
     * @param \DateTime|null $fullName
110
     * @return User|null
111
     */
112
    public function findOneByOrganizationAndFullName(Organization $organization, $fullName, $date = null)
113
    {
114
        return $this->createQueryBuilder('u')
115
            ->distinct()
116
            ->where('u.internalCode = :name')
117
            ->join('u.memberships', 'm')
118
            ->andWhere('m.organization = :organization')
119
            ->setParameter('organization', $organization)
120
            ->setParameter('name', $fullName)
121
            ->andWhere('m.validUntil >= :date OR  m.validUntil IS NULL')
122
            ->andWhere('m.validFrom <= :date')
123
            ->setParameter('date', $date)
124
            ->getQuery()
125
            ->getOneOrNullResult();
126
    }
127
}
128