Completed
Push — master ( f48f3f...e7b8b9 )
by Yohan
02:34
created

EventRepository::getEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
/*
4
 * This file is part of CalendR, a Fréquence web project.
5
 *
6
 * (c) 2012 Fréquence web
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CalendR\Bridge\Doctrine\ORM;
13
14
/**
15
 * Trait that transforms a Doctrine2 EntityRepository into
16
 * a CalendR Event Provider.
17
 *
18
 * @author Yohan Giarelli <[email protected]>
19
 */
20
trait EventRepository
21
{
22
    /**
23
     * @param \DateTime $begin
24
     * @param \DateTime $end
25
     * @param array     $options
26
     *
27
     * @return \Doctrine\ORM\QueryBuilder
28
     */
29
    public function getEventsQueryBuilder(\DateTime $begin, \DateTime $end, array $options = array())
30
    {
31
        $begin = sprintf("'%s'", $begin->format('Y-m-d H:i:s'));
32
        $end   = sprintf("'%s'", $end->format('Y-m-d H:i:s'));
33
        $qb    = $this->createQueryBuilderForGetEvent($options);
34
35
        return $qb
36
            ->andWhere(
37
                $qb->expr()->orX(
38
                // Period in event
39
                    $qb->expr()->andX(
40
                        $qb->expr()->lte($this->getBeginFieldName(), $begin),
41
                        $qb->expr()->gte($this->getEndFieldName(), $end)
42
                    ),
43
                    // Event in period
44
                    $qb->expr()->andX(
45
                        $qb->expr()->gte($this->getBeginFieldName(), $begin),
46
                        $qb->expr()->lt($this->getEndFieldName(), $end)
47
                    ),
48
                    // Event begins during period
49
                    $qb->expr()->andX(
50
                        $qb->expr()->lt($this->getBeginFieldName(), $end),
51
                        $qb->expr()->gte($this->getBeginFieldName(), $begin)
52
                    ),
53
                    // Event ends during period
54
                    $qb->expr()->andX(
55
                        $qb->expr()->gte($this->getEndFieldName(), $begin),
56
                        $qb->expr()->lt($this->getEndFieldName(), $end)
57
                    )
58
                )
59
            )
60
            ;
61
    }
62
63
    /**
64
     * @param \DateTime $begin
65
     * @param \DateTime $end
66
     * @param array     $options
67
     *
68
     * @return \Doctrine\ORM\Query
69
     */
70
    public function getEventsQuery(\DateTime $begin, \DateTime $end, array $options = array())
71
    {
72
        return $this->getEventsQueryBuilder($begin, $end, $options)->getQuery();
73
    }
74
75
    /**
76
     * @param \DateTime $begin
77
     * @param \DateTime $end
78
     * @param array     $options
79
     *
80
     * @return array<\CalendR\Event\EventInterface>
81
     */
82
    public function getEvents(\DateTime $begin, \DateTime $end, array $options = array())
83
    {
84
        return $this->getEventsQuery($begin, $end, $options)->getResult();
85
    }
86
87
    /**
88
     * @param array $options
89
     *
90
     * @return \Doctrine\ORM\QueryBuilder
91
     */
92
    public function createQueryBuilderForGetEvent(array $options)
93
    {
94
        return $this->createQueryBuilder('evt');
0 ignored issues
show
Bug introduced by
The method createQueryBuilder() does not exist on CalendR\Bridge\Doctrine\ORM\EventRepository. Did you maybe mean createQueryBuilderForGetEvent()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
95
    }
96
97
    /**
98
     * Returns the begin date field name.
99
     *
100
     * @return string
101
     */
102
    public function getBeginFieldName()
103
    {
104
        return 'evt.begin';
105
    }
106
107
    /**
108
     * Returns the end date field name.
109
     *
110
     * @return string
111
     */
112
    public function getEndFieldName()
113
    {
114
        return 'evt.end';
115
    }
116
}
117