Passed
Push — master ( d1c4e0...b88e45 )
by Angel Fernando Quiroz
24:34 queued 16:01
created

AgendaReminder::encodeDateInterval()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
c 0
b 0
f 0
nc 4
nop 0
dl 0
loc 17
rs 9.8333
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Entity;
8
9
use Chamilo\CoreBundle\Traits\TimestampableTypedEntity;
10
use Chamilo\CourseBundle\Entity\CCalendarEvent;
11
use DateInterval;
12
use Doctrine\ORM\Mapping as ORM;
13
use Symfony\Component\Serializer\Annotation\Groups;
14
15
#[ORM\Entity]
16
#[ORM\Table(name: 'agenda_reminder')]
17
class AgendaReminder
18
{
19
    use TimestampableTypedEntity;
20
21
    #[ORM\Id]
22
    #[ORM\Column(type: 'integer')]
23
    #[ORM\GeneratedValue(strategy: 'AUTO')]
24
    #[Groups(['calendar_event:read'])]
25
    protected ?int $id = null;
26
27
    #[ORM\Column(name: 'type', type: 'string')]
28
    protected string $type;
29
30
    #[ORM\Column(name: 'date_interval', type: 'dateinterval')]
31
    protected DateInterval $dateInterval;
32
33
    #[ORM\Column(name: 'sent', type: 'boolean')]
34
    protected bool $sent;
35
36
    #[Groups(['calendar_event:write', 'calendar_event:read'])]
37
    public int $count;
38
39
    #[Groups(['calendar_event:write', 'calendar_event:read'])]
40
    public string $period;
41
42
    #[ORM\ManyToOne(inversedBy: 'reminders')]
43
    #[ORM\JoinColumn(referencedColumnName: 'iid', nullable: false)]
44
    private ?CCalendarEvent $event = null;
45
46
    public function __construct()
47
    {
48
        $this->sent = false;
49
    }
50
51
    public function getId(): ?int
52
    {
53
        return $this->id;
54
    }
55
56
    public function getType(): string
57
    {
58
        return $this->type;
59
    }
60
61
    public function setType(string $type): self
62
    {
63
        $this->type = $type;
64
65
        return $this;
66
    }
67
68
    public function getDateInterval(): DateInterval
69
    {
70
        return $this->dateInterval;
71
    }
72
73
    public function setDateInterval(DateInterval $dateInterval): self
74
    {
75
        $this->dateInterval = $dateInterval;
76
77
        return $this;
78
    }
79
80
    public function isSent(): bool
81
    {
82
        return $this->sent;
83
    }
84
85
    public function setSent(bool $sent): self
86
    {
87
        $this->sent = $sent;
88
89
        return $this;
90
    }
91
92
    public function getEvent(): ?CCalendarEvent
93
    {
94
        return $this->event;
95
    }
96
97
    public function setEvent(?CCalendarEvent $event): static
98
    {
99
        $this->event = $event;
100
101
        return $this;
102
    }
103
104
    public function decodeDateInterval(): static
105
    {
106
        $this->dateInterval = match ($this->period) {
107
            'i' => DateInterval::createFromDateString("{$this->count} minutes"),
108
            'h' => DateInterval::createFromDateString("{$this->count} hours"),
109
            'd' => DateInterval::createFromDateString("{$this->count} days"),
110
            default => null,
111
        };
112
113
        return $this;
114
    }
115
116
    public function encodeDateInterval(): static
117
    {
118
        if ($this->dateInterval->i) {
119
            $this->count = $this->dateInterval->i;
120
            $this->period = 'i';
121
        } elseif ($this->dateInterval->h) {
122
            $this->count = $this->dateInterval->h;
123
            $this->period = 'h';
124
        } elseif ($this->dateInterval->d) {
125
            $this->count = $this->dateInterval->d;
126
            $this->period = 'd';
127
        } else {
128
            $this->count = (int) $this->dateInterval->format('%a');
129
            $this->period = 'd';
130
        }
131
132
        return $this;
133
    }
134
}
135