Completed
Push — master ( 81d10e...3eebf6 )
by Bernhard
01:55
created

Task::isPushed()   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
/**
4
 * \Wicked\Timely\Entities\Task
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Bernhard Wick <[email protected]>
15
 * @copyright 2016 Bernhard Wick
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/wick-ed/timely
18
 */
19
20
namespace Wicked\Timely\Entities;
21
22
/**
23
 * Task entity
24
 *
25
 * @author    Bernhard Wick <[email protected]>
26
 * @copyright 2016 Bernhard Wick
27
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
28
 * @link      https://github.com/wick-ed/timely
29
 */
30
class Task
31
{
32
33
    /**
34
     * First booking of this task
35
     *
36
     * @var \Wicked\Timely\Entities\Booking $startBooking
37
     */
38
    protected $startBooking;
39
40
    /**
41
     * Last booking of this task
42
     *
43
     * @var \Wicked\Timely\Entities\Booking $endBooking
44
     */
45
    protected $endBooking;
46
47
    /**
48
     * Bookings within this task
49
     *
50
     * @var \Wicked\Timely\Entities\Booking[] $intermediateBookings
51
     */
52
    protected $intermediateBookings;
53
54
    /**
55
     * Intermediate tasks, done within this task instance
56
     *
57
     * @var \Wicked\Timely\Entities\Task[] $intermediateTasks
58
     */
59
    protected $intermediateTasks;
60
61
    /**
62
     * The task's duration
63
     *
64
     * @var integer $duration
65
     */
66
    protected $duration;
67
68
    /**
69
     * Whether or not the task is being clipped due to filtering
70
     *
71
     * @var boolean $isClipped
72
     */
73
    protected $isClipped = false;
74
75
    /**
76
     * Getter for the first booking of the task instance
77
     *
78
     * @return \Wicked\Timely\Entities\Booking
79
     */
80
    public function getStartBooking()
81
    {
82
        return $this->startBooking;
83
    }
84
85
    /**
86
     * Getter for the first booking of the task instance
87
     *
88
     * @return string
89
     */
90
    public function getStartTime()
91
    {
92
        $firstIntermediate = reset($this->intermediateBookings);
93
        if ($this->isClipped() && $firstIntermediate instanceof Pause && $firstIntermediate->isPauseEnd()) {
94
            return $firstIntermediate->getTime();
95
        }
96
        return $this->getStartBooking()->getTime();
97
    }
98
99
    /**
100
     * Getter for the first booking of the task instance
101
     *
102
     * @return string
103
     */
104
    public function getComment()
105
    {
106
        return $this->getStartBooking()->getComment();
107
    }
108
109
    /**
110
     * Getter for the first booking of the task instance
111
     *
112
     * @return string
113
     */
114
    public function isPushed()
115
    {
116
        return $this->getStartBooking()->isPushed();
117
    }
118
119
    /**
120
     * Getter for the last booking of the task instance
121
     *
122
     * @return \Wicked\Timely\Entities\Booking
123
     */
124
    public function getEndBooking()
125
    {
126
        return $this->endBooking;
127
    }
128
129
    /**
130
     * Getter for the first booking of the task instance
131
     *
132
     * @return string
133
     */
134
    public function getEndTime()
135
    {
136
        return $this->getEndBooking()->getTime();
137
    }
138
139
    /**
140
     * Getter for the intermediate bookings
141
     *
142
     * @return \Wicked\Timely\Entities\Booking[]
143
     */
144
    public function getIntermediateBookings()
145
    {
146
        return $this->intermediateBookings;
147
    }
148
149
    /**
150
     * Getter for the task's ticket id
151
     *
152
     * @return string
153
     */
154
    public function getTicketId()
155
    {
156
        return $this->getStartBooking()->getTicketId();
157
    }
158
159
    /**
160
     * Getter for the task duration
161
     *
162
     * @return integer
163
     */
164
    public function getDuration()
165
    {
166
        return $this->duration;
167
    }
168
169
    /**
170
     * Getter for the task duration
171
     *
172
     * @return integer
173
     */
174
    protected function isClipped()
175
    {
176
        return $this->isClipped;
177
    }
178
179
    /**
180
     * Default constructor
181
     *
182
     * @param \Wicked\Timely\Entities\Booking   $startBooking         The first booking of the task
183
     * @param \Wicked\Timely\Entities\Booking   $endBooking           The last booking of the task
184
     * @param \Wicked\Timely\Entities\Booking[] $intermediateBookings Bookings within this task
185
     */
186
    public function __construct($startBooking, $endBooking, $intermediateBookings)
187
    {
188
        // set the properties
189
        $this->startBooking = $startBooking;
190
        $this->endBooking = $endBooking;
191
        $this->intermediateBookings = $intermediateBookings;
192
        // calculate the duration
193
        $this->duration = $this->calculateDuration($startBooking, $endBooking, $intermediateBookings);
194
        // determine if we are being clipped
195
        foreach ($intermediateBookings as $intermediateBooking) {
196
            if ($intermediateBooking instanceof Clipping) {
197
                $this->isClipped = true;
198
                break;
199
            }
200
        }
201
    }
202
203
    /**
204
     * Calculates the duration of a task by given bookings
205
     *
206
     * @param \Wicked\Timely\Entities\Booking   $startBooking         The first booking of the task
207
     * @param \Wicked\Timely\Entities\Booking   $endBooking           The last booking of the task
208
     * @param \Wicked\Timely\Entities\Booking[] $intermediateBookings Bookings within this task
209
     *
210
     * @return integer
211
     */
212
    protected function calculateDuration($startBooking, $endBooking, array $intermediateBookings)
213
    {
214
        // get any potential clippings and include them as task borders
215
        foreach ($intermediateBookings as $intermediateBooking) {
216
            if ($intermediateBooking->canStartTask()) {
217
                $startBooking = $intermediateBooking;
218
219
            } elseif ($intermediateBooking->getTicketId() === Clipping::CLIPPING_TAG_REAR) {
220
                $endBooking = $intermediateBooking;
221
            }
222
        }
223
224
        // get the raw time without breaks and such
225
        $rawTime = strtotime($endBooking->getTime()) - strtotime($startBooking->getTime());
226
227
        // subtract the breaks
228
        if (count($intermediateBookings) > 1) {
229
            $this->intermediateTasks = TaskFactory::getTasksFromBookings(array_merge(array($endBooking), $intermediateBookings), false, true);
230
            foreach ($this->intermediateTasks as $intermediateTask) {
231
                $rawTime -= $intermediateTask->getDuration();
232
            }
233
        }
234
        // return what we got
235
        return $rawTime;
236
    }
237
}
238