Semester::now()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Adlogix\EventScheduler\ValueObject;
6
7
use DateTime;
8
use DateTimeInterface;
9
use Webmozart\Assert\Assert;
10
11
/**
12
 * @author Toni Van de Voorde <[email protected]>
13
 */
14
final class Semester
15
{
16
    /**
17
     * @var int
18
     */
19
    private $value;
20
21
    /**
22
     * @param int $value
23
     */
24 12
    private function __construct(int $value)
25
    {
26 12
        Assert::range($value, 1, 2);
27 12
        $this->value = $value;
28 12
    }
29
30
    /**
31
     * @return int
32
     */
33 12
    public function value(): int
34
    {
35 12
        return $this->value;
36
    }
37
38
    /**
39
     * @param Semester $semester
40
     * @return bool
41
     */
42 12
    public function equals(Semester $semester): bool
43
    {
44 12
        return $this->value === $semester->value();
45
    }
46
47
    /**
48
     * @return Semester
49
     */
50
    public static function first(): self
51
    {
52
        return new self(1);
53
    }
54
55
    /**
56
     * @return Semester
57
     */
58
    public static function second(): self
59
    {
60
        return new self(2);
61
    }
62
63
    /**
64
     * @return Semester
65
     */
66
    public static function now(): self
67
    {
68
        return self::fromDateTime(new DateTime('now'));
69
    }
70
71
    /**
72
     * @param DateTimeInterface $dateTime
73
     * @return Semester
74
     */
75 12
    public static function fromDateTime(DateTimeInterface $dateTime): self
76
    {
77 12
        $semester = ceil($dateTime->format('n') / 6);
78
79 12
        return new self((int)$semester);
80
    }
81
82
    /**
83
     * @param int $value
84
     * @return Semester
85
     */
86
    public static function fromValue(int $value)
87
    {
88
        return new self($value);
89
    }
90
}
91