Completed
Push — master ( e962b5...4a55b4 )
by Artem
13:43
created

DateRange   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 63
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getSince() 0 4 1
A getTill() 0 4 1
A isEqual() 0 8 4
A assertSameTimezones() 0 6 2
1
<?php
2
/*
3
 * This file is part of the DateTime library.
4
 *
5
 * (c) Artem Henvald <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Fresh\DateTime;
14
15
use Fresh\DateTime\Exception\LogicException;
16
17
/**
18
 * DateRange.
19
 *
20
 * @author Artem Henvald <[email protected]>
21
 */
22
final class DateRange implements DateRangeInterface
23
{
24
    private const INTERNAL_DATE_FORMAT = 'Y-m-d';
25
26
    /** @var \DateTimeImmutable */
27
    private $since;
28
29
    /** @var \DateTimeImmutable */
30
    private $till;
31
32
    /**
33
     * @param \DateTimeInterface $since
34
     * @param \DateTimeInterface $till
35
     */
36
    public function __construct(\DateTimeInterface $since, \DateTimeInterface $till)
37
    {
38
        $this->assertSameTimezones($since, $till);
39
40
        $this->since = \DateTimeImmutable::createFromFormat(\DateTime::RFC3339, $since->format(\DateTime::RFC3339), $since->getTimezone());
41
        $this->till = \DateTimeImmutable::createFromFormat(\DateTime::RFC3339, $till->format(\DateTime::RFC3339), $till->getTimezone());
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getSince(): \DateTimeImmutable
48
    {
49
        return $this->since;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getTill(): \DateTimeImmutable
56
    {
57
        return $this->till;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function isEqual(DateRangeInterface $dateRange): bool
64
    {
65
        return $this->since->format(self::INTERNAL_DATE_FORMAT) === $dateRange->getSince()->format(self::INTERNAL_DATE_FORMAT)
66
            && $this->till->format(self::INTERNAL_DATE_FORMAT) === $dateRange->getTill()->format(self::INTERNAL_DATE_FORMAT)
67
            && $this->since->getTimezone()->getName() === $dateRange->getTill()->getTimezone()->getName()
68
            && $this->till->getTimezone()->getName() === $dateRange->getTill()->getTimezone()->getName()
69
        ;
70
    }
71
72
    /**
73
     * @param \DateTimeInterface $since
74
     * @param \DateTimeInterface $till
75
     *
76
     * @throws LogicException
77
     */
78
    private function assertSameTimezones(\DateTimeInterface $since, \DateTimeInterface $till): void
79
    {
80
        if ($since->getTimezone()->getName() !== $till->getTimezone()->getName()) {
81
            throw new LogicException('Dates have different timezones');
82
        }
83
    }
84
}
85