RolePermissionRepository::isPermissionAssigned()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 17
rs 9.9
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Potievdev\SlimRbac\Models\Repository;
4
5
use Doctrine\ORM\EntityRepository;
6
use Potievdev\SlimRbac\Models\Entity\Permission;
7
8
/**
9
 * RolePermissionRepository
10
 *
11
 * This class was generated by the Doctrine ORM. Add your own custom
12
 * repository methods below.
13
 */
14
class RolePermissionRepository extends EntityRepository
15
{
16
    /**
17
     * @param Permission $permission
18
     * @return array
19
     */
20
    public function getRoleIdsByPermission(Permission $permission)
21
    {
22
        $qb = $this->createQueryBuilder('rolePermission');
23
24
        $qb->select('rolePermission.roleId')
25
            ->where($qb->expr()->eq('rolePermission.permissionId', $permission->getId()));
26
27
        return $qb->getQuery()->getArrayResult();
28
    }
29
30
    /**
31
     * Search RolePermission record. If found return true else false
32
     * @param integer $permissionId
33
     * @param integer[] $roleIds
34
     * @return bool
35
     */
36
    public function isPermissionAssigned(int $permissionId, array $roleIds): bool
37
    {
38
        $qb = $this->createQueryBuilder('rolePermission');
39
40
        $result = $qb
41
            ->select('rolePermission.id')
42
            ->where(
43
                $qb->expr()->andX(
44
                    $qb->expr()->eq('rolePermission.permissionId', $permissionId),
45
                    $qb->expr()->in('rolePermission.roleId', $roleIds)
46
                )
47
            )
48
            ->setMaxResults(1)
49
            ->getQuery()
50
            ->getArrayResult();
51
52
        return count($result) > 0;
53
    }
54
}
55