1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* \Wicked\Timely\Command\Push |
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 wick-ed |
15
|
|
|
* @copyright 2020 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\Command; |
21
|
|
|
|
22
|
|
|
use JiraRestApi\JiraException; |
23
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
24
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
25
|
|
|
use Wicked\Timely\Entities\Booking; |
26
|
|
|
use Wicked\Timely\Entities\TaskFactory; |
27
|
|
|
use Wicked\Timely\Formatter\FormatterFactory; |
28
|
|
|
use Wicked\Timely\PushServices\PushServiceFactory; |
29
|
|
|
use Wicked\Timely\Storage\StorageFactory; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class for the "track" command. Command is used to track time bookings |
33
|
|
|
* |
34
|
|
|
* @author wick-ed |
35
|
|
|
* @copyright 2020 Bernhard Wick |
36
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
37
|
|
|
* @link https://github.com/wick-ed/timely |
38
|
|
|
*/ |
39
|
|
|
class Push extends AbstractReadCommand |
40
|
|
|
{ |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Constants used within this command |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
const COMMAND_NAME = 'push'; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Configures the "track" command |
51
|
|
|
* |
52
|
|
|
* @return void |
53
|
|
|
* |
54
|
|
|
* {@inheritDoc} |
55
|
|
|
* @see \Symfony\Component\Console\Command\Command::configure() |
56
|
|
|
*/ |
57
|
|
|
protected function configure() |
58
|
|
|
{ |
59
|
|
|
$this |
60
|
|
|
->setName(static::COMMAND_NAME) |
61
|
|
|
->setDescription('Pushes booked times against the configured remote') |
62
|
|
|
->setHelp(<<<'EOF' |
63
|
|
|
The <info>%command.name%</info> command is used to push tracked times to an external time keeping service. |
64
|
|
|
Jira being an example of a supported service. |
65
|
|
|
|
66
|
|
|
The command has the same syntax and usability as the <info>show</info> command. |
67
|
|
|
On execution the command will use the service's internal format to process all tracked times that a similar <info>show</info> command would have displayed. |
68
|
|
|
|
69
|
|
|
The following command would create e.g. Jira worklogs for yesterday's tasks: |
70
|
|
|
|
71
|
|
|
<info>timely %command.name% yesterday</info> |
72
|
|
|
|
73
|
|
|
The <info>%command.name%</info> command keeps track of already pushed time trackings so nothing gets pushed twice. |
74
|
|
|
EOF |
75
|
|
|
) |
76
|
|
|
; |
77
|
|
|
// add all the read options from the abstract super class |
78
|
|
|
parent::configure(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Execute the command |
83
|
|
|
* |
84
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input The command input |
85
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output The command output |
86
|
|
|
* |
87
|
|
|
* @return int |
88
|
|
|
* |
89
|
|
|
* {@inheritDoc} |
90
|
|
|
* @throws JiraException |
91
|
|
|
* @throws \JsonMapper_Exception |
92
|
|
|
* @see \Symfony\Component\Console\Command\Command::execute() |
93
|
|
|
*/ |
94
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
95
|
|
|
{ |
96
|
|
|
// get the ticket |
97
|
|
|
$ticket = $input->getArgument('ticket'); |
98
|
|
|
|
99
|
|
|
// we might need framing dates |
100
|
|
|
$toDate = null; |
101
|
|
|
$fromDate = null; |
102
|
|
|
|
103
|
|
|
// there might be a limit |
104
|
|
|
$limit = null; |
105
|
|
|
|
106
|
|
|
// prepare all the input options |
107
|
|
|
$this->prepareInputParams($input, $ticket, $fromDate, $toDate, $limit); |
|
|
|
|
108
|
|
|
|
109
|
|
|
// filter by ticket if given |
110
|
|
|
/** @var \Wicked\Timely\Storage\StorageInterface $storage */ |
111
|
|
|
$storage = StorageFactory::getStorage(); |
112
|
|
|
$bookings = $storage->retrieve($ticket, $toDate, $fromDate, $limit); |
113
|
|
|
|
114
|
|
|
// return if we did not find any bookings |
115
|
|
|
if (empty($bookings)) { |
116
|
|
|
$output->write('No bookings found, nothing to push ...', true); |
117
|
|
|
return; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// get tasks from bookings |
121
|
|
|
$tasks = TaskFactory::getTasksFromBookings($bookings); |
122
|
|
|
|
123
|
|
|
// get our issue service and push the tasks |
124
|
|
|
$bookingsPushed = array(); |
125
|
|
|
$pushService = PushServiceFactory::getService($output); |
126
|
|
|
foreach ($tasks as $task) { |
127
|
|
|
// Already pushed to jira? take next one |
128
|
|
|
if ($task->isPushed()) { |
129
|
|
|
continue; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
try { |
133
|
|
|
$result = $pushService->push($task); |
134
|
|
|
if ($result) { |
135
|
|
|
$bookingsPushed[] = $task->getStartBooking(); |
136
|
|
|
$output->writeln( |
137
|
|
|
sprintf( |
138
|
|
|
'PUSHED %s : %s > %s', |
139
|
|
|
$task->getTicketId(), |
140
|
|
|
$task->getDuration(), |
141
|
|
|
$task->getComment() |
142
|
|
|
) |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
} catch (\Exception $e) { |
146
|
|
|
$output->write( |
147
|
|
|
sprintf( |
148
|
|
|
'<error>Error while pushing. Status %s returned.</error>', |
149
|
|
|
$e->getCode() |
150
|
|
|
), |
151
|
|
|
true |
152
|
|
|
); |
153
|
|
|
$output->write( |
154
|
|
|
sprintf( |
155
|
|
|
'Returned error: %s', |
156
|
|
|
$e->getMessage() |
157
|
|
|
), |
158
|
|
|
true, |
159
|
|
|
64 |
160
|
|
|
); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
// mark the bookings as pushed in our storage |
165
|
|
|
foreach ($bookingsPushed as $booking) { |
166
|
|
|
$storage->storePush($booking); |
167
|
|
|
$formatter = FormatterFactory::getFormatter(); |
168
|
|
|
$bookString = $formatter->toString($booking); |
169
|
|
|
$output->write($bookString, true); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
// if the last task has been cut off by pushing we will re-add it's start booking |
173
|
|
|
$lastTask = end($tasks); |
174
|
|
|
if ($lastTask->isOngoing() && !$lastTask->isPaused()) { |
175
|
|
|
$continuedBooking = new Booking( |
176
|
|
|
$lastTask->getComment(), |
177
|
|
|
$lastTask->getTicketId() |
178
|
|
|
); |
179
|
|
|
$storage->store($continuedBooking); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
// write output |
183
|
|
|
if (empty($bookingsPushed)) { |
184
|
|
|
$output->write('<comment>Did not push anything.</comment>', true); |
185
|
|
|
} else { |
186
|
|
|
$output->write(sprintf('Successfully pushed %s tasks.', count($bookingsPushed)), true); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return 0; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.