Completed
Pull Request — master (#469)
by
unknown
02:16
created

AvailableTo::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
        } elseif ($calendar->getType() === CalendarType::SINGLE()) {
33
            $availableTo = $calendar->getEndDate() ? $calendar->getEndDate() : $calendar->getStartDate();
34
        } else {
35
            $availableTo = $calendar->getEndDate();
36
        }
37
38
        /**
39
         * https://jira.uitdatabank.be/browse/III-1581
40
         *
41
         * When available to has no time information, it needs to be set to almost midnight 23:59:59.
42
         *
43
         * To check for missing time information a check is done on formats: H:i:s
44
         *
45
         * The fixed date for a permanent calendar type does not require time information.
46
         * This fixed date of 2100-01-01 is checked with the time formats: Y-m-d
47
         */
48
        if ($availableTo->format('Y-m-d') != '2100-01-01' &&
49
            $availableTo->format('H:i:s') == '00:00:00') {
50
            $availableToWithHours = new \DateTime();
51
            $availableToWithHours->setTimestamp($availableTo->getTimestamp());
52
            $availableToWithHours->add(new \DateInterval("P0000-00-00T23:59:59"));
53
            $availableTo = $availableToWithHours;
54
        }
55
56
        return new self($availableTo);
57
    }
58
59
    /**
60
     * @return \DateTimeInterface
61
     */
62
    public function getAvailableTo()
63
    {
64
        return $this->availableTo;
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70
    public function __toString()
71
    {
72
        return $this->availableTo->format(\DateTime::ATOM);
73
    }
74
}
75