Passed
Pull Request — master (#6532)
by Angel Fernando Quiroz
08:33
created

AccessUrlRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Repository\Node;
8
9
use Chamilo\CoreBundle\Entity\AccessUrl;
10
use Chamilo\CoreBundle\Entity\User;
11
use Chamilo\CoreBundle\Repository\ResourceRepository;
12
use Doctrine\ORM\NonUniqueResultException;
13
use Doctrine\ORM\NoResultException;
14
use Doctrine\ORM\QueryBuilder;
15
use Doctrine\Persistence\ManagerRegistry;
16
17
class AccessUrlRepository extends ResourceRepository
18
{
19
    public function __construct(ManagerRegistry $registry)
20
    {
21
        parent::__construct($registry, AccessUrl::class);
22
    }
23
24
    /**
25
     * Select the first access_url ID in the list as a default setting for
26
     * the creation of new users.
27
     */
28
    public function getFirstId(): int
29
    {
30
        $qb = $this->createQueryBuilder('a');
31
        $qb->select('MIN (a.id)');
32
33
        $q = $qb->getQuery();
34
35
        try {
36
            return (int) $q->getSingleScalarResult();
37
        } catch (NonUniqueResultException|NoResultException $e) {
38
            return 0;
39
        }
40
    }
41
42
    /**
43
     * @return array<int, AccessUrl>
44
     */
45
    public function findByUser(User $user): array
46
    {
47
        /** @var QueryBuilder $qb */
48
        $qb = $this->createQueryBuilder('url');
49
50
        return $qb
51
            ->join('url.users', 'users')
52
            ->where($qb->expr()->eq('users.user', ':user'))
53
            ->setParameter('user', $user->getId())
54
            ->getQuery()
55
            ->getResult()
56
        ;
57
    }
58
59
    public function getUserActivePortals(User $user): QueryBuilder
60
    {
61
        /** @var QueryBuilder $qb */
62
        $qb = $this->createQueryBuilder('url');
63
64
        return $qb
65
            ->join('url.users', 'users')
66
            ->where($qb->expr()->eq('users.user', ':user'))
67
            ->andWhere($qb->expr()->eq('url.active', true))
68
            ->andWhere($qb->expr()->neq('url.isLoginOnly', true))
69
            ->setParameter('user', $user->getId())
70
        ;
71
    }
72
73
    public function getOnlyLoginAccessUrl(): ?AccessUrl
74
    {
75
        return $this->findOneBy(['isLoginOnly' => true]);
76
    }
77
78
    /**
79
     * @throws NonUniqueResultException
80
     * @throws NoResultException
81
     */
82
    public function exists(string $url): bool
83
    {
84
        return $this->createQueryBuilder('a')
85
            ->select('COUNT(a.id)')
86
            ->where('a.url = :url')
87
            ->setParameter('url', $url)
88
            ->getQuery()
89
            ->getSingleScalarResult() > 0
90
        ;
91
    }
92
93
    /**
94
     * @return array<int, string>
95
     */
96
    public function getUrlList(): array
97
    {
98
        return $this->createQueryBuilder('a')
99
            ->select('a.url')
100
            ->getQuery()
101
            ->getSingleColumnResult()
102
        ;
103
    }
104
}
105