Passed
Push — preprodparkur ( f4828c...eec125 )
by Angel Fernando Quiroz
13:34
created

PersonalAgendaRepository::getEventsForInvitee()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 34
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 34
rs 9.6666
cc 3
nc 4
nop 3
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Entity\Repository;
6
7
use Chamilo\CoreBundle\Entity\PersonalAgenda;
8
use Chamilo\UserBundle\Entity\User;
9
use Doctrine\ORM\EntityRepository;
10
use Doctrine\ORM\NonUniqueResultException;
11
12
class PersonalAgendaRepository extends EntityRepository
13
{
14
    /**
15
     * @return array<int, PersonalAgenda>
16
     */
17
    public function getEventsForInvitee(User $user, ?\DateTime $startDate, ?\DateTime $endDate): array
18
    {
19
        $qb = $this->createQueryBuilder('pa');
20
        $qb
21
            ->innerJoin('pa.invitation', 'i')
22
            ->innerJoin('i.invitees', 'iu')
23
            ->where(
24
                $qb->expr()->eq('iu.user', ':user')
25
            )
26
        ;
27
28
        $params = [
29
            'user' => $user,
30
        ];
31
32
        if ($startDate) {
33
            $qb->andWhere(
34
                $qb->expr()->gte('pa.date', ':start_date')
35
            );
36
37
            $params['start_date'] = $startDate;
38
        }
39
40
        if ($endDate) {
41
            $qb->andWhere(
42
                $qb->expr()->lte('pa.enddate', ':end_date')
43
            );
44
45
            $params['end_date'] = $endDate;
46
        }
47
48
        $qb->setParameters($params);
49
50
        return $qb->getQuery()->getResult();
51
    }
52
53
    public function findOneByIdAndInvitee(int $eventId, User $user): ?PersonalAgenda
54
    {
55
        $qb = $this->createQueryBuilder('pa');
56
        $qb
57
            ->innerJoin('pa.invitation', 'i')
58
            ->innerJoin('i.invitees', 'iu')
59
            ->where(
60
                $qb->expr()->eq('pa.id', ':id')
61
            )
62
            ->andWhere(
63
                $qb->expr()->eq('iu.user', ':user')
64
            )
65
            ->setParameters(['id' => $eventId, 'user' => $user])
66
        ;
67
68
        try {
69
            return $qb->getQuery()->getOneOrNullResult();
70
        } catch (NonUniqueResultException $e) {
71
            return null;
72
        }
73
    }
74
}
75