Completed
Pull Request — master (#234)
by Luc
05:07
created

AvailableTo   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 7
c 3
b 0
f 1
lcom 0
cbo 2
dl 0
loc 65
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B createFromCalendar() 0 28 4
A getAvailableTo() 0 4 1
A __toString() 0 4 1
1
<?php
2
3
namespace CultuurNet\UDB3\Offer;
4
5
use CultuurNet\UDB3\CalendarInterface;
6
use CultuurNet\UDB3\CalendarType;
7
8
class AvailableTo
9
{
10
    /**
11
     * @var \DateTimeInterface
12
     */
13
    private $availableTo;
14
15
    /**
16
     * AvailableTo constructor.
17
     * @param \DateTimeInterface $availableTo
18
     */
19
    private function __construct(\DateTimeInterface $availableTo)
20
    {
21
        $this->availableTo = $availableTo;
22
    }
23
24
    /**
25
     * @param CalendarInterface $calendar
26
     * @return AvailableTo
27
     */
28
    public static function createFromCalendar(CalendarInterface $calendar)
29
    {
30
        if ($calendar->getType() === CalendarType::PERMANENT()) {
31
            $availableTo = new \DateTime('2100-01-01T00:00:00Z');
32
        } else {
33
            $availableTo = $calendar->getEndDate();
34
        }
35
36
        /**
37
         * https://jira.uitdatabank.be/browse/III-1581
38
         *
39
         * When available to has no time information, it needs to be set to almost midnight 23:59:59.
40
         *
41
         * To check for missing time information a check is done on formats: H:i:s
42
         *
43
         * The fixed date for a permanent calendar type does not require time information.
44
         * This fixed date of 2100-01-01 is checked with the time formats: Y-m-d
45
         */
46
        if ($availableTo->format('Y-m-d') != '2100-01-01' &&
47
            $availableTo->format('H:i:s') == '00:00:00') {
48
            $availableToWithHours = new \DateTime();
49
            $availableToWithHours->setTimestamp($availableTo->getTimestamp());
50
            $availableToWithHours->add(new \DateInterval("P0000-00-00T23:59:59"));
51
            $availableTo = $availableToWithHours;
52
        }
53
54
        return new self($availableTo);
55
    }
56
57
    /**
58
     * @return \DateTimeInterface
59
     */
60
    public function getAvailableTo()
61
    {
62
        return $this->availableTo;
63
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68
    public function __toString()
69
    {
70
        return $this->availableTo->format(\DateTime::ATOM);
71
    }
72
}
73