Passed
Branch master (d7957a)
by Samson
04:01
created

FromJdnConverter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 68
c 0
b 0
f 0
ccs 21
cts 22
cp 0.9545
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A set() 0 12 2
A process() 0 12 1
A setDate() 0 8 1
1
<?php
2
3
namespace Andegna\Converter;
4
5
use Andegna\Exception\InvalidDateException;
6
7
/**
8
 * Converts JDN To Ethiopian Date.
9
 */
10
class FromJdnConverter extends Converter
11
{
12
    /**
13
     * FromJdnConverter constructor.
14
     *
15
     * @param $jdn int
16
     *
17
     * @throws \Andegna\Exception\InvalidDateException
18
     */
19 81
    public function __construct(int $jdn)
20
    {
21 81
        $this->set($jdn);
22 81
    }
23
24
    /**
25
     * Set the JDN for processing.
26
     *
27
     * @param $jdn
28
     *
29
     * @throws \Andegna\Exception\InvalidDateException
30
     *
31
     * @return $this
32
     */
33 81
    public function set(int $jdn)
34
    {
35 81
        if (!$this->isValidInteger($jdn)) {
36
            throw new InvalidDateException();
37
        }
38
39 81
        $this->jdn = $jdn;
40
41 81
        $date = static::process($jdn);
42
43 81
        return $this->setDate($date);
44
    }
45
46
    /**
47
     * @param $jdn integer
48
     *
49
     * @return array
50
     */
51 81
    protected static function process(int $jdn): array
52
    {
53 81
        $r = (($jdn - 1723856) % 1461);
54 81
        $n = ($r % 365) + 365 * (int) ($r / 1460);
55
56 81
        $year = 4 * (int) (($jdn - 1723856) / 1461) +
57 81
            (int) ($r / 365) - (int) ($r / 1460);
58 81
        $month = (int) ($n / 30) + 1;
59 81
        $day = ($n % 30) + 1;
60
61 81
        return compact('day', 'month', 'year');
62
    }
63
64
    /**
65
     * @param $date array
66
     *
67
     * @return $this|Converter
68
     */
69 81
    protected function setDate(array $date): Converter
70
    {
71 81
        $this->day = $date['day'];
72 81
        $this->month = $date['month'];
73 81
        $this->year = $date['year'];
74
75 81
        return $this;
76
    }
77
}
78