Completed
Push — master ( b79250...5c4b09 )
by Alexandre
04:43
created

Holiday::match()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace AppBundle\Entity;
4
5
use AppBundle\Util\Uuid;
6
use Doctrine\ORM\Mapping\Column;
7
use Doctrine\ORM\Mapping\Entity;
8
use Doctrine\ORM\Mapping\Id;
9
use Symfony\Component\Validator\Constraints as Assert;
10
use Symfony\Component\Validator\Context\ExecutionContextInterface;
11
12
/**
13
 * @Entity
14
 */
15
class Holiday
16
{
17
    /**
18
     * @Id
19
     * @Column(type="string", length=40)
20
     */
21
    private $id;
22
23
    /**
24
     * @Column(type="date")
25
     */
26
    private $beginAt;
27
28
    /**
29
     * @Column(type="date")
30
     */
31
    private $endAt;
32
33
    public function __construct()
34
    {
35
        $this->id = Uuid::generateV4();
36
        $this->beginAt = new \DateTime('next saturday');
37
        $this->endAt = new \DateTime('next sunday');
38
    }
39
40
    public function getId()
41
    {
42
        return $this->id;
43
    }
44
45
    public function match(\DateTime $day)
46
    {
47
        $day = $day->setTime(12, 0, 0);
48
        $begin = $this->beginAt;
49
        $end = $this->endAt->setTime(23, 59, 59);
50
51
        return $day >= $begin && $day <= $end;
52
    }
53
54
    public function getBeginAt()
55
    {
56
        return $this->beginAt;
57
    }
58
59
    public function setBeginAt(\DateTime $beginAt)
60
    {
61
        $this->beginAt = $beginAt;
62
63
        return $this;
64
    }
65
66
    public function getEndAt()
67
    {
68
        return $this->endAt;
69
    }
70
71
    public function setEndAt(\DateTime $endAt)
72
    {
73
        $this->endAt = $endAt;
74
75
        return $this;
76
    }
77
78
    /**
79
     * @Assert\Callback()
80
     */
81
    public function assertDateInterval(ExecutionContextInterface $context)
82
    {
83
        if ($this->beginAt > $this->endAt) {
84
            $context->addViolation('La date de début et la date de fin ne sont pas dans le bon ordre');
85
        }
86
    }
87
}
88