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

findByUserAndElementsQueryBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
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\Element;
24
use AppBundle\Entity\User;
25
use Doctrine\ORM\EntityRepository;
26
use Doctrine\ORM\QueryBuilder;
27
28
class RoleRepository extends EntityRepository
29
{
30
    /**
31
     * @param User $user
32
     * @param Element[] $elements
33
     *
34
     * @return QueryBuilder
35
     */
36
    public function findByUserAndElementsQueryBuilder(User $user, array $elements)
37
    {
38
        return $this->createQueryBuilder('r')
39
            ->andWhere('r.user = :user')
40
            ->andWhere('r.element IN (:elements)')
41
            ->setParameter('user', $user)
42
            ->setParameter('elements', $elements);
43
    }
44
45
    /**
46
     * @param User $user
47
     * @param Element[] $elements
48
     *
49
     * @return int
50
     */
51
    public function countByUserAndElements(User $user, array $elements)
52
    {
53
        $count = $this->findByUserAndElementsQueryBuilder($user, $elements)
54
            ->select('COUNT(r.element)')
55
            ->getQuery()
56
            ->getSingleScalarResult();
57
58
        return (int) ($count);
59
    }
60
}
61