ToJdnConverter::set()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
ccs 9
cts 9
cp 1
rs 9.7333
cc 2
nc 2
nop 3
crap 2
1
<?php
2
3
namespace Andegna\Converter;
4
5
use Andegna\Exception\InvalidDateException;
6
use Andegna\Validator\DateValidator as DateValidator;
7
8
/**
9
 * Converts Ethiopian Date to JDN.
10
 */
11
class ToJdnConverter extends Converter
12
{
13
    /**
14
     * ToJdnConverter constructor.
15
     *
16
     * @param $day
17
     * @param $month
18
     * @param $year
19
     *
20
     * @throws \Andegna\Exception\InvalidDateException
21
     */
22 98
    public function __construct(int $day, int $month, int $year)
23
    {
24 98
        $this->set($day, $month, $year);
25 81
    }
26
27
    /**
28
     * Set the date for processing.
29
     *
30
     * @param $day
31
     * @param $month
32
     * @param $year
33
     *
34
     * @throws \Andegna\Exception\InvalidDateException
35
     *
36
     * @return $this
37
     */
38 98
    public function set(int $day, int $month, int $year)
39
    {
40 98
        $validator = new DateValidator($day, $month, $year);
41
42 98
        if (!$validator->isValid()) {
43 17
            throw new InvalidDateException();
44
        }
45
46 81
        $this->day = $day;
47 81
        $this->month = $month;
48 81
        $this->year = $year;
49
50 81
        $this->jdn = (int) static::process($day, $month, $year);
51
52 81
        return $this;
53
    }
54
55
    /**
56
     * @param $day
57
     * @param $month
58
     * @param $year
59
     *
60
     * @return int
61
     */
62 81
    protected static function process($day, $month, $year)
63
    {
64
        return
65
            (1723856 + 365) +
66 81
            365 * ($year - 1) +
67 81
            (int) ($year / 4) +
68 81
            30 * $month +
69 81
            $day - 31;
70
    }
71
}
72