| Conditions | 13 |
| Paths | 46 |
| Total Lines | 114 |
| 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 |
||
| 80 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 81 | { |
||
| 82 | // $passwd = $this->prompt_silent($output); |
||
| 83 | // $output->writeln('fertig für heute:'.$passwd.'#'); |
||
| 84 | |||
| 85 | // get the ticket |
||
| 86 | $ticket = $input->getArgument('ticket'); |
||
| 87 | |||
| 88 | // we might need framing dates |
||
| 89 | $toDate = null; |
||
| 90 | $fromDate = null; |
||
| 91 | |||
| 92 | // there might be a limit |
||
| 93 | $limit = null; |
||
| 94 | |||
| 95 | // prepare all the input options |
||
| 96 | $this->prepareInputParams($input, $ticket, $fromDate, $toDate, $limit); |
||
| 97 | |||
| 98 | // filter by ticket if given |
||
| 99 | /** @var \Wicked\Timely\Storage\StorageInterface $storage */ |
||
| 100 | $storage = StorageFactory::getStorage(); |
||
| 101 | $bookings = $storage->retrieve($ticket, $toDate, $fromDate, $limit); |
||
| 102 | |||
| 103 | // return if we did not find any bookings |
||
| 104 | if (empty($bookings)) { |
||
| 105 | $output->write('No bookings found, nothing to push ...', true); |
||
| 106 | return; |
||
| 107 | } |
||
| 108 | |||
| 109 | // get tasks from bookings |
||
| 110 | $tasks = TaskFactory::getTasksFromBookings($bookings); |
||
| 111 | |||
| 112 | // retrieve configuration |
||
| 113 | $configuration = new DotEnvConfiguration(); |
||
| 114 | $password = $configuration->getJiraPassword(); |
||
| 115 | if (empty($password) || strtolower($password) === self::KEYCHAIN_NAME) { |
||
| 116 | $password = $this->getPasswdFromKeychain($output, $configuration); |
||
| 117 | if (empty($password)) { |
||
| 118 | return; |
||
| 119 | } |
||
| 120 | $configuration->setJiraPassword($password); |
||
| 121 | |||
| 122 | } |
||
| 123 | unset($password); |
||
| 124 | $bookingsPushed = array(); |
||
| 125 | // get our issue service and push the tasks |
||
| 126 | $issueService = new IssueService($configuration); |
||
| 127 | foreach ($tasks as $task) { |
||
| 128 | // Allready pushed to jira? take next one |
||
| 129 | if ($task->isPushed()) { |
||
| 130 | continue; |
||
| 131 | } |
||
| 132 | // create a worklog from the task |
||
| 133 | $workLog = new Worklog(); |
||
| 134 | $workLog->setComment($task->getComment()) |
||
| 135 | ->setStarted($task->getStartTime()) |
||
| 136 | ->setTimeSpent(Date::secondsToUnits(Date::roundByInterval($task->getDuration(), 900))); |
||
| 137 | |||
| 138 | // check the sanity of our worklog and discard it if there is something missing |
||
| 139 | if (!$task->getTicketId() || empty($workLog->timeSpent) || empty($workLog->comment)) |
||
| 140 | { |
||
| 141 | $output->writeln('Not pushing one worklog as it misses vital information'); |
||
| 142 | continue; |
||
| 143 | } |
||
| 144 | |||
| 145 | // log the worklog about to being pushed if output is verbose |
||
| 146 | if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { |
||
| 147 | $output->writeln( |
||
| 148 | sprintf( |
||
| 149 | '%s : %s > %s', |
||
| 150 | $task->getTicketId(), |
||
| 151 | $workLog->timeSpent, |
||
| 152 | $workLog->comment |
||
| 153 | ) |
||
| 154 | ); |
||
| 155 | } |
||
| 156 | |||
| 157 | try { |
||
| 158 | // push to remote |
||
| 159 | $issueService->addWorklog($task->getTicketId(), $workLog); |
||
| 160 | |||
| 161 | $output->writeln( |
||
| 162 | sprintf( |
||
| 163 | 'PUSHED %s : %s > %s', |
||
| 164 | $task->getTicketId(), |
||
| 165 | $workLog->timeSpent, |
||
| 166 | $workLog->comment |
||
| 167 | ) |
||
| 168 | ); |
||
| 169 | |||
| 170 | $bookingsPushed[] = $task->getStartBooking(); |
||
| 171 | |||
| 172 | } catch (JiraException $e) { |
||
| 173 | $output->write( |
||
| 174 | sprintf( |
||
| 175 | 'Error while pushing. Status %s, with message: "%s"', |
||
| 176 | $e->getCode(), |
||
| 177 | $e->getMessage() |
||
| 178 | ) |
||
| 179 | ); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | foreach ($bookingsPushed as $booking) { |
||
| 184 | $storage->storePush($booking); |
||
| 185 | $formatter = FormatterFactory::getFormatter(); |
||
| 186 | $bookString = $formatter->toString($booking); |
||
| 187 | $output->write($bookString, true); |
||
| 188 | } |
||
| 189 | |||
| 190 | // write output |
||
| 191 | $output->write(sprintf('Successfully pushed %s tasks.', count($bookingsPushed)), true); |
||
| 192 | |||
| 193 | } |
||
| 194 | |||
| 237 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.