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

Grouped::toString()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 41
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 2
Metric Value
c 5
b 0
f 2
dl 0
loc 41
rs 8.439
cc 6
eloc 18
nc 16
nop 1
1
<?php
2
3
/**
4
 * \Wicked\Timely\Formatter\Grouped
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\Formatter;
21
22
use Wicked\Timely\Entities\Booking;
23
use Wicked\Timely\Entities\Pause;
24
use Wicked\Timely\Entities\TaskFactory;
25
use Wicked\Timely\Helper\Date;
26
27
/**
28
 * Flat storage
29
 *
30
 * @author    Bernhard Wick <[email protected]>
31
 * @copyright 2016 Bernhard Wick
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/wick-ed/timely
34
 */
35
class Grouped implements FormatterInterface
36
{
37
38
    /**
39
     * Default character sequence for segment separation
40
     *
41
     * @var string SEPARATOR
42
     */
43
    const SEPARATOR = ' | ';
44
45
    /**
46
     * The total duration of all tasks grouped together
47
     *
48
     * @var integer $totalDuration
49
     */
50
    protected $totalDuration;
51
52
    /**
53
     * Formats a booking into a string
54
     *
55
     * @param \Wicked\Timely\Entities\Booking[]|\Wicked\Timely\Entities\Booking $bookings The bookings to format
56
     *
57
     * @return string
58
     */
59
    public function toString($bookings)
60
    {
61
        // if we do not get an array make one
62
        if (!is_array($bookings)) {
63
            $bookings = array($bookings);
64
        }
65
66
        // create the tasks from the bookings
67
        $tasks = TaskFactory::getTasksFromBookings($bookings);
68
        // iterate the tasks and sort them by ticket
69
        $groups = array();
70
        foreach ($tasks as $task) {
71
            // skip tasks which are used to describe meta bookings
72
            if ($task->getStartBooking()->isMetaBooking()) {
73
                continue;
74
            }
75
            // prepare for collection
76
            $ticketId = $task->getStartBooking()->getTicketId();
77
            // prepare the group if needed
78
            if (!isset($groups[$ticketId])) {
79
                $groups[$ticketId] = array();
80
            }
81
            // collect the grouped tasks
82
            $groups[$ticketId][] = $task;
83
        }
84
85
        // generate the group string
86
        $result = '';
87
        foreach ($groups as $ticketId => $groupedTasks) {
88
            $result .= $this->renderGroup($ticketId, $groupedTasks);
89
        }
90
91
        // print the total amount of time spent
92
        $result .= '
93
====================================================
94
Total: ' . Date::secondsToUnits($this->totalDuration) . '
95
====================================================';
96
97
        // return the string
98
        return $result;
99
    }
100
101
    /**
102
     * Render a certain group of tasks
103
     *
104
     * @param string                         $ticketId Ticket id to render a group for
105
     * @param \Wicked\Timely\Command\Track[] $tasks    Set of tasks to render a group for
106
     *
107
     * @return string
108
     */
109
    protected function renderGroup($ticketId, array $tasks)
110
    {
111
        // collect some vital numbers
112
        $total = 0;
113
        $ticketList = '';
114
        foreach ($tasks as $task) {
115
            // create the entry string
116
            $booking = $task->getStartBooking();
0 ignored issues
show
Bug introduced by
The method getStartBooking() does not seem to exist on object<Wicked\Timely\Command\Track>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
117
            $ticketList .= implode(
118
                self::SEPARATOR,
119
                array(
120
                $booking->getTime(),
121
                $booking->getTicketId(),
122
                Date::secondsToUnits($task->getDuration()),
0 ignored issues
show
Bug introduced by
The method getDuration() does not seem to exist on object<Wicked\Timely\Command\Track>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
123
                $booking->getComment()
124
                )
125
            ) . '
126
    ';
127
            $this->totalDuration += $total += $task->getDuration();
0 ignored issues
show
Bug introduced by
The method getDuration() does not seem to exist on object<Wicked\Timely\Command\Track>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
128
        }
129
130
        // we also need the first and last element of the array
131
        $firstBooking= reset($tasks)->getStartBooking();
0 ignored issues
show
Bug introduced by
The method getStartBooking() does not seem to exist on object<Wicked\Timely\Command\Track>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
132
        $lastBooking = end($tasks)->getEndBooking();
133
134
        // begin the string generation
135
        $result = $ticketId . '     ' .  $firstBooking->getTime() . ' -> ' . $lastBooking->getTime() . '
136
====================================================
137
    ' . $ticketList;
138
139
        // print the total and end with another linebreak without indention to break groups apart
140
        $result .= '-------------------------------------------------
141
    ' . Date::secondsToUnits($total) . '
142
143
';
144
        // return the string
145
        return $result;
146
    }
147
}
148