Conditions | 10 |
Paths | 23 |
Total Lines | 102 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
75 | protected function execute(InputInterface $input, OutputInterface $output) |
||
76 | { |
||
77 | // get the ticket |
||
78 | $ticket = $input->getArgument('ticket'); |
||
79 | |||
80 | // we might need framing dates |
||
81 | $toDate = null; |
||
82 | $fromDate = null; |
||
83 | |||
84 | // there might be a limit |
||
85 | $limit = null; |
||
86 | |||
87 | // prepare all the input options |
||
88 | $this->prepareInputParams($input, $ticket, $fromDate, $toDate, $limit); |
||
89 | |||
90 | // filter by ticket if given |
||
91 | /** @var \Wicked\Timely\Storage\StorageInterface $storage */ |
||
92 | $storage = StorageFactory::getStorage(); |
||
93 | $bookings = $storage->retrieve($ticket, $toDate, $fromDate, $limit); |
||
94 | |||
95 | // return if we did not find any bookings |
||
96 | if (empty($bookings)) { |
||
97 | $output->write('No bookings found, nothing to push ...', true); |
||
98 | return; |
||
99 | } |
||
100 | |||
101 | // get tasks from bookings |
||
102 | $tasks = TaskFactory::getTasksFromBookings($bookings); |
||
103 | |||
104 | // retrieve configuration |
||
105 | $configuration = new DotEnvConfiguration(); |
||
106 | |||
107 | $bookingsPushed = array(); |
||
108 | // get our issue service and push the tasks |
||
109 | $issueService = new IssueService($configuration); |
||
110 | foreach ($tasks as $task) { |
||
111 | // Allready pushed to jira? take next one |
||
112 | if ($task->isPushed()) { |
||
113 | continue; |
||
114 | } |
||
115 | // create a worklog from the task |
||
116 | $workLog = new Worklog(); |
||
117 | $workLog->setComment($task->getComment()) |
||
118 | ->setStarted($task->getStartTime()) |
||
119 | ->setTimeSpent(Date::secondsToUnits(Date::roundByInterval($task->getDuration(), 900))); |
||
120 | |||
121 | // check the sanity of our worklog and discard it if there is something missing |
||
122 | if (!$task->getTicketId() || empty($workLog->timeSpent) || empty($workLog->comment)) |
||
123 | { |
||
124 | $output->writeln('Not pushing one worklog as it misses vital information'); |
||
125 | continue; |
||
126 | } |
||
127 | |||
128 | // log the worklog about to being pushed if output is verbose |
||
129 | if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { |
||
130 | $output->writeln( |
||
131 | sprintf( |
||
132 | '%s : %s > %s', |
||
133 | $task->getTicketId(), |
||
134 | $workLog->timeSpent, |
||
135 | $workLog->comment |
||
136 | ) |
||
137 | ); |
||
138 | } |
||
139 | |||
140 | try { |
||
141 | // push to remote |
||
142 | $issueService->addWorklog($task->getTicketId(), $workLog); |
||
143 | |||
144 | $output->writeln( |
||
145 | sprintf( |
||
146 | 'PUSHED %s : %s > %s', |
||
147 | $task->getTicketId(), |
||
148 | $workLog->timeSpent, |
||
149 | $workLog->comment |
||
150 | ) |
||
151 | ); |
||
152 | |||
153 | $bookingsPushed[] = $task->getStartBooking(); |
||
154 | |||
155 | } catch (JiraException $e) { |
||
156 | $output->write( |
||
157 | sprintf( |
||
158 | 'Error while pushing. Status %s, with message: "%s"', |
||
159 | $e->getCode(), |
||
160 | $e->getMessage() |
||
161 | ) |
||
162 | ); |
||
163 | } |
||
164 | } |
||
165 | |||
166 | foreach ($bookingsPushed as $booking) { |
||
167 | $storage->storePush($booking); |
||
168 | $formatter = FormatterFactory::getFormatter(); |
||
169 | $bookString = $formatter->toString($booking); |
||
170 | $output->write($bookString, true); |
||
171 | } |
||
172 | |||
173 | // write output |
||
174 | $output->write(sprintf('Successfully pushed %s tasks.', count($bookingsPushed)), true); |
||
175 | |||
176 | } |
||
177 | } |
||
178 |