Completed
Push — master ( bb0f5a...762a3e )
by Igor
02:04
created

DateRangeFormatter::getLong()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 12
cts 12
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 12
nc 6
nop 1
crap 7
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Arbor\Moment\Formatter;
6
7
use Arbor\Moment\DateRange;
8
use Arbor\Moment\DateTime;
9
10
/**
11
 * Class DateRangeFormatter
12
 *
13
 * @package Arbor\Moment\Formatter
14
 * @author Igor Vuckovic <[email protected]>
15
 */
16
class DateRangeFormatter implements FormatterInterface
17
{
18
    /** @var DateTime */
19
    private $startDate;
20
21
    /** @var DateTime */
22
    private $endDate;
23
24
    /**
25
     * @param DateRange $dateRange
26
     */
27 17
    public function __construct(DateRange $dateRange)
28
    {
29 17
        $this->startDate = $dateRange->getStartDate();
30 17
        $this->endDate = $dateRange->getEndDate();
31 17
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 10
    public function getShort(string $nullPlaceholder = 'Ongoing') : string
37
    {
38 10
        if ($this->startDate === null && $this->endDate === null) {
39 2
            return $nullPlaceholder;
40
        }
41
42 8
        $string = $this->startDate === null ? $nullPlaceholder : $this->startDate->getShort();
43 8
        if (!($this->startDate && $this->endDate && $this->startDate->getShort() === $this->endDate->getShort())) {
44 6
            $string .= ' - ' . ($this->endDate === null ? $nullPlaceholder : $this->endDate->getShort());
45
        }
46
47 8
        return $string;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 21
    public function getLong(string $nullPlaceholder = 'Ongoing') : string
54
    {
55 21
        if ($this->startDate === null && $this->endDate === null) {
56 3
            return $nullPlaceholder;
57
        }
58
59 18
        if ($this->endDate === null) {
60 3
            return $this->startDate->getLong() . ' - ' . $nullPlaceholder;
61
        }
62
63 15
        if ($this->startDate === null) {
64 3
            return $nullPlaceholder . ' - ' . $this->endDate->getLong();
65
        }
66
67 12
        if ($this->startDate->getLong() === $this->endDate->getLong()) {
68 6
            return $this->startDate->getLong();
69
        }
70
71 6
        if ($this->startDate->getShort() === $this->endDate->getShort()) {
72 3
            return $this->startDate->getLong() . ' - ' . $this->endDate->format(DateTime::FORMAT_TIME);
73
        }
74
75 3
        return $this->startDate->getLong() . ' - ' . $this->endDate->getLong();
76
    }
77
}
78