Completed
Push — master ( 6239a3...1c8aa0 )
by Tobias
07:52
created

RoundDateTime::modify()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Happyr\DoctrineSpecification\Result;
4
5
use Doctrine\ORM\AbstractQuery;
6
use Doctrine\ORM\Query\Parameter;
7
8
/**
9
 * Round a \DateTime to enable caching.
10
 */
11
class RoundDateTime implements ResultModifier
12
{
13
    /**
14
     * @var int How may seconds to round time
15
     */
16
    private $roundSeconds;
17
18
    /**
19
     * @param int $roundSeconds How may seconds to round time
20
     */
21
    public function __construct($roundSeconds)
22
    {
23
        $this->roundSeconds = $roundSeconds;
24
    }
25
26
    /**
27
     * @param AbstractQuery $query
28
     */
29
    public function modify(AbstractQuery $query)
30
    {
31
        foreach ($query->getParameters() as $parameter) {
32
            /* @var $parameter Parameter */
33
            if ($parameter->getValue() instanceof \DateTimeInterface) {
34
                // round down so that the results do not include data that should not be there.
35
                $date = clone $parameter->getValue();
36
                $date = $date->setTimestamp(floor($date->getTimestamp() / $this->roundSeconds) * $this->roundSeconds);
37
38
                $query->setParameter($parameter->getName(), $date, $parameter->getType());
39
            }
40
        }
41
    }
42
}
43