Completed
Push — master ( 595880...ff3d97 )
by Karsten
02:08
created

LocalTimeslot::setFrom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * File was created 12.04.2016 06:54
4
 */
5
namespace PeekAndPoke\Types;
6
7
/**
8
 * @author Karsten J. Gerber <[email protected]>
9
 */
10
class LocalTimeslot
11
{
12
    /**
13
     * @var LocalDate
14
     *
15
     * @PeekAndPoke\Component\Slumber\Annotation\Slumber\AsLocalDate()
16
     */
17
    private $from;
18
19
    /**
20
     * @var LocalDate
21
     *
22
     * @PeekAndPoke\Component\Slumber\Annotation\Slumber\AsLocalDate()
23
     */
24
    private $to;
25
26
    /**
27
     * @return LocalTimeslot
28
     */
29 14
    public static function createEmpty()
30
    {
31 14
        return new static();
32
    }
33
34
    /**
35
     * @param LocalDate $from
36
     * @param LocalDate $to
37
     *
38
     * @return LocalTimeslot
39
     */
40 7
    public static function from(LocalDate $from, LocalDate $to)
41
    {
42 7
        $ret       = new static();
43 7
        $ret->from = $from;
44 7
        $ret->to   = $to;
45
46 7
        return $ret;
47
    }
48
49
    /**
50
     * LocalTimeslot cannot be constructed directly.
51
     *
52
     * @see from()
53
     * @see createEmpty()
54
     */
55 21
    protected function __construct()
56
    {
57 21
    }
58
59
    /**
60
     * @return string
61
     */
62 4
    public function __toString()
63
    {
64 4
        return ($this->from ? $this->from->format() : '?') . ' - ' . ($this->to ? $this->to->format() : '?');
65
    }
66
67
    /**
68
     * @return bool
69
     */
70 9
    public function isValid()
71
    {
72 9
        return $this->from !== null
73 9
               && $this->to !== null
74 9
               && $this->from->isBefore($this->to);
75
    }
76
77
    /**
78
     * @param LocalTimeslot $other
79
     *
80
     * @return bool
81
     */
82 9
    public function equals(LocalTimeslot $other = null)
83
    {
84 9
        if ($this === $other) {
85 1
            return true;
86
        }
87
88
        return
89
            // is the other there ?
90
            $other !== null
91
            // is the from the same ?
92 8
            && (
93 7
                ($this->from === null && $other->from === null) ||
94 6
                ($this->from !== null && $this->from->isEqual($other->from))
95 6
            )
96
            // is the to the same ?
97 8
            && (
98 4
                ($this->to === null && $other->to === null) ||
99 3
                ($this->to !== null && $this->to->isEqual($other->to))
100 8
            );
101
    }
102
103
    /**
104
     * @return LocalDate
105
     */
106 7
    public function getFrom()
107
    {
108 7
        return $this->from;
109
    }
110
111
    /**
112
     * @return LocalDate
113
     */
114 7
    public function getTo()
115
    {
116 7
        return $this->to;
117
    }
118
119
    /**
120
     * @param LocalDate $from
121
     *
122
     * @return $this
123
     */
124 12
    public function setFrom(LocalDate $from = null)
125
    {
126 12
        $this->from = $from;
127
128 12
        return $this;
129
    }
130
131
    /**
132
     * @param LocalDate $to
133
     *
134
     * @return $this
135
     */
136 12
    public function setTo(LocalDate $to = null)
137
    {
138 12
        $this->to = $to;
139
140 12
        return $this;
141
    }
142
143
    /**
144
     * Creates a new instance and moves the timeslot by the given number of days
145
     *
146
     * @param int $numDays
147
     *
148
     * @return LocalTimeslot
149
     */
150 3
    public function modifyByDays($numDays)
151
    {
152 3
        return self::from(
153 3
            $this->from->modifyByDays($numDays),
154 3
            $this->to->modifyByDays($numDays)
155 3
        );
156
    }
157
158
    /**
159
     * Creates a new instance and moves the timeslot by the given number of days
160
     * Takes into consideration the daylight savings possible issues
161
     *
162
     * @param int $numDays
163
     *
164
     * @return LocalTimeslot
165
     */
166 3
    public function modifyByDaysDaylightSavingAware($numDays)
167
    {
168 3
        return self::from(
169 3
            $this->from->modifyByDaysDaylightSavingAware($numDays),
170 3
            $this->to->modifyByDaysDaylightSavingAware($numDays)
171 3
        );
172
    }
173
174
    /**
175
     * @return int
176
     */
177 2
    public function getDurationInSecs()
178
    {
179 2
        if ($this->from && $this->to) {
180 1
            return $this->to->getTimestamp() - $this->from->getTimestamp();
181
        }
182
183 1
        return 0;
184
    }
185
186
    /**
187
     * @return float
188
     */
189 2
    public function getDurationInMins()
190
    {
191 2
        return $this->getDurationInSecs() / 60;
192
    }
193
}
194