DateRangeFormatter   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 0
loc 69
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B getShort() 0 20 6
C getLong() 0 24 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
        if ($this->startDate === null) {
43 2
            return $nullPlaceholder . ' - ' . $this->endDate->getShort();
44
        }
45
46 6
        if ($this->endDate === null) {
47 2
            return $this->startDate->getShort() . ' - ' . $nullPlaceholder;
48
        }
49
50 4
        if ($this->startDate->getShort() === $this->endDate->getShort()) {
51 2
            return $this->startDate->getShort();
52
        }
53
54 2
        return $this->startDate->getShort() . ' - ' . $this->endDate->getShort();
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 21
    public function getLong(string $nullPlaceholder = 'Ongoing') : string
61
    {
62 21
        if ($this->startDate === null && $this->endDate === null) {
63 3
            return $nullPlaceholder;
64
        }
65
66 18
        if ($this->startDate === null) {
67 3
            return $nullPlaceholder . ' - ' . $this->endDate->getLong();
68
        }
69
70 15
        if ($this->endDate === null) {
71 3
            return $this->startDate->getLong() . ' - ' . $nullPlaceholder;
72
        }
73
74 12
        if ($this->startDate->getLong() === $this->endDate->getLong()) {
75 6
            return $this->startDate->getLong();
76
        }
77
78 6
        if ($this->startDate->getShort() === $this->endDate->getShort()) {
79 3
            return $this->startDate->getLong() . ' - ' . $this->endDate->format(DateTime::FORMAT_TIME);
80
        }
81
82 3
        return $this->startDate->getLong() . ' - ' . $this->endDate->getLong();
83
    }
84
}
85