TimeLimitedAwareTrait   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
eloc 16
c 1
b 0
f 1
dl 0
loc 42
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setEndDate() 0 3 1
A getStartDate() 0 3 1
A isActive() 0 14 4
A setStartDate() 0 3 1
A getEndDate() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Model;
6
7
use DateTime;
8
use DateTimeInterface;
9
10
trait TimeLimitedAwareTrait
11
{
12
    protected $startDate;
13
14
    protected $endDate;
15
16
    protected $active;
17
18
    public function getStartDate(): ?DateTimeInterface
19
    {
20
        return $this->startDate;
21
    }
22
23
    public function setStartDate(DateTimeInterface $startDate): void
24
    {
25
        $this->startDate = $startDate;
26
    }
27
28
    public function getEndDate(): ?DateTimeInterface
29
    {
30
        return $this->endDate;
31
    }
32
33
    public function setEndDate(DateTimeInterface $endDate): void
34
    {
35
        $this->endDate = $endDate;
36
    }
37
38
    public function isActive(): bool
39
    {
40
        $today = new DateTime();
41
        $afterStart = true;
42
        $beforeEnd = true;
43
        if ($this->startDate instanceof DateTimeInterface) {
44
            $afterStart = $today->getTimestamp() > $this->startDate->getTimestamp();
45
        }
46
47
        if ($this->endDate instanceof DateTimeInterface) {
48
            $beforeEnd = $today->getTimestamp() < $this->endDate->getTimestamp();
49
        }
50
51
        return $afterStart && $beforeEnd;
52
    }
53
}
54