Completed
Push — master ( 1f8449...3785e1 )
by Bernhard
12:25
created

Task::calculateDuration()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 22
rs 8.6737
cc 5
eloc 11
nc 8
nop 3
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
     * Getter for the first booking of the task instance
70
     *
71
     * @return \Wicked\Timely\Entities\Booking
72
     */
73
    public function getStartBooking()
74
    {
75
        return $this->startBooking;
76
    }
77
78
    /**
79
     * Getter for the last booking of the task instance
80
     *
81
     * @return \Wicked\Timely\Entities\Booking
82
     */
83
    public function getEndBooking()
84
    {
85
        return $this->endBooking;
86
    }
87
88
    /**
89
     * Getter for the intermediate bookings
90
     *
91
     * @return \Wicked\Timely\Entities\Booking[]
92
     */
93
    public function getIntermediateBookings()
94
    {
95
        return $this->intermediateBookings;
96
    }
97
98
    /**
99
     * Getter for the task duration
100
     *
101
     * @return integer
102
     */
103
    public function getDuration()
104
    {
105
        return $this->duration;
106
    }
107
108
    /**
109
     * Default constructor
110
     *
111
     * @param \Wicked\Timely\Entities\Booking   $startBooking         The first booking of the task
112
     * @param \Wicked\Timely\Entities\Booking   $endBooking           The last booking of the task
113
     * @param \Wicked\Timely\Entities\Booking[] $intermediateBookings Bookings within this task
114
     */
115
    public function __construct($startBooking, $endBooking, $intermediateBookings)
116
    {
117
        // set the properties
118
        $this->startBooking = $startBooking;
119
        $this->endBooking = $endBooking;
120
        $this->intermediateBookings = $intermediateBookings;
121
        // calculate the duration
122
        $this->duration = $this->calculateDuration($startBooking, $endBooking, $intermediateBookings);
123
    }
124
125
    /**
126
     * Calculates the duration of a task by given bookings
127
     *
128
     * @param \Wicked\Timely\Entities\Booking   $startBooking         The first booking of the task
129
     * @param \Wicked\Timely\Entities\Booking   $endBooking           The last booking of the task
130
     * @param \Wicked\Timely\Entities\Booking[] $intermediateBookings Bookings within this task
131
     *
132
     * @return integer
133
     */
134
    protected function calculateDuration($startBooking, $endBooking, array $intermediateBookings)
135
    {
136
        // get any potential clippings and include them as task borders
137
        foreach ($intermediateBookings as $intermediateBooking) {
138
            if ($intermediateBooking->getTicketId() === Clipping::CLIPPING_TAG_FRONT) {
139
                $startBooking = $intermediateBooking;
140
141
            } elseif ($intermediateBooking->getTicketId() === Clipping::CLIPPING_TAG_REAR) {
142
                $endBooking = $intermediateBooking;
143
            }
144
        }
145
146
        // get the raw time without breaks and such
147
        $rawTime = strtotime($endBooking->getTime()) - strtotime($startBooking->getTime());
148
        // subtract the breaks
149
        $intermediateTasks = TaskFactory::getTasksFromBookings(array_merge(array($endBooking), $intermediateBookings), false, true);
150
        foreach ($intermediateTasks as $intermediateTask) {
151
            $rawTime -= $intermediateTask->getDuration();
152
        }
153
        // return what we got
154
        return $rawTime;
155
    }
156
}
157