TimeInterval::getDatetimeField()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Symball\ReportBundle\Query;
4
5
use Doctrine\Common\Persistence\ObjectRepository;
6
use Symball\ReportBundle\Interfaces\QueryInterface;
7
use Symball\ReportBundle\Query\Base;
8
9
class TimeInterval extends Base implements QueryInterface
10
{
11
    protected $currentDate;
12
    protected $interval;
13
    protected $dateTimeField;
14
15
    /**
16
     * Define the time range to use for each query set
17
     *
18
     * @param \DateInterval $interval
19
     * @return $this
20
     */
21
    public function setIntervalDateTime(\DateInterval $interval)
22
    {
23
        $this->interval = $interval;
24
25
        return $this;
26
    }
27
28
    /**
29
     * Set the "current" time that the query will work from
30
     *
31
     * @param \DateTime $dateTime
32
     * @return $this
33
     */
34
    public function setHeadDateTime(\DateTime $dateTime)
35
    {
36
        $this->currentDate = $dateTime;
37
38
        return $this;
39
    }
40
41
    /**
42
     * Define the database field which will be used as the DateTime handler
43
     *
44
     * @param string $fieldName
45
     * @return $this
46
     */
47
    public function setDateTimeField($fieldName)
48
    {
49
        $this->dateTimeField = $fieldName;
50
51
        return $this;
52
    }
53
54
    /**
55
     * For this report type, add the custom logic to query
56
     *
57
     * @return type
58
     */
59
    public function prepareQuery()
60
    {
61
62
        $query = parent::prepareQuery();
63
64
        $earlyDate = clone $this->currentDate;
65
        $this->currentDate->add($this->interval);
66
67
        $query
68
        ->field($this->getDateTimeField())->gte($earlyDate)
69
        ->field($this->getDateTimeField())->lte($this->currentDate);
70
71
        return $query;
72
    }
73
74
    /**
75
     * Stringify the current data set
76
     *
77
     * @return string
78
     */
79
    public function getTitle()
80
    {
81
        $laterDate = clone $this->currentDate;
82
        $laterDate->add($this->interval);
83
84
        return $this->currentDate->format('y-m-d') . ' - ' . $laterDate->format('y-m-d');
85
    }
86
87
    /**
88
     * Return the field reference that is being used to define time
89
     *
90
     * @return string
91
     */
92
    public function getDatetimeField()
93
    {
94
        return $this->dateTimeField;
95
    }
96
}
97