| Conditions | 8 |
| Paths | 123 |
| Total Lines | 121 |
| Code Lines | 71 |
| 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 namespace Comodojo\Extender\Task; |
||
| 73 | public function run(Request $request) { |
||
| 74 | |||
| 75 | $name = $request->getName(); |
||
| 76 | $task = $request->getTask(); |
||
| 77 | $uid = $request->getUid(); |
||
| 78 | $jid = $request->getJid(); |
||
| 79 | $puid = $request->getParentUid(); |
||
| 80 | $pid = 0; |
||
| 81 | $parameters = $request->getParameters(); |
||
| 82 | |||
| 83 | ob_start(); |
||
| 84 | |||
| 85 | try { |
||
| 86 | |||
| 87 | $this->stopwatch->start(); |
||
| 88 | |||
| 89 | $this->logger->notice("Starting new task $name ($task)"); |
||
| 90 | |||
| 91 | $task_def = $this->table->get($name); |
||
| 92 | |||
| 93 | if ( $task_def === null ) throw new Exception("Task $name not found, aborting"); |
||
| 94 | |||
| 95 | $thetask = $task_def->getInstance($name, $parameters); |
||
| 96 | |||
| 97 | $this->events->emit( new TaskEvent('start', $thetask) ); |
||
| 98 | |||
| 99 | $pid = $thetask->getPid(); |
||
| 100 | |||
| 101 | $this->openWorklog( |
||
| 102 | $uid, |
||
| 103 | $puid, |
||
| 104 | $pid, |
||
| 105 | $name, |
||
| 106 | $jid, |
||
| 107 | $task, |
||
| 108 | $parameters, |
||
| 109 | $this->stopwatch->getStartTime() |
||
| 110 | ); |
||
| 111 | |||
| 112 | $this->installErrorHandler(); |
||
| 113 | |||
| 114 | try { |
||
| 115 | |||
| 116 | $result = $thetask->run(); |
||
| 117 | |||
| 118 | $status = Worklog::STATUS_COMPLETE; |
||
| 119 | |||
| 120 | $this->events->emit( new TaskEvent('complete', $thetask) ); |
||
| 121 | |||
| 122 | } catch (TaskException $te) { |
||
| 123 | |||
| 124 | $status = Worklog::STATUS_ABORT; |
||
| 125 | |||
| 126 | $result = $te->getMessage(); |
||
| 127 | |||
| 128 | $this->events->emit( new TaskEvent('abort', $thetask) ); |
||
| 129 | |||
| 130 | } catch (Exception $e) { |
||
| 131 | |||
| 132 | $status = Worklog::STATUS_ERROR; |
||
| 133 | |||
| 134 | $result = $e->getMessage(); |
||
| 135 | |||
| 136 | $this->events->emit( new TaskEvent('error', $thetask) ); |
||
| 137 | |||
| 138 | } |
||
| 139 | |||
| 140 | $this->restoreErrorHandler(); |
||
| 141 | |||
| 142 | $this->events->emit( new TaskEvent('stop', $thetask) ); |
||
| 143 | |||
| 144 | $this->stopwatch->stop(); |
||
| 145 | |||
| 146 | $this->closeWorklog($status, $result, $this->stopwatch->getStopTime()); |
||
| 147 | |||
| 148 | $drift = $this->stopwatch->getDrift()->format('%s'); |
||
| 149 | |||
| 150 | $this->logger->notice("Task $name ($task) pid $pid ends in ".($status === Worklog::STATUS_COMPLETE ? 'success' : 'error')." in $drift secs"); |
||
| 151 | |||
| 152 | $result = new Result([ |
||
| 153 | $uid, |
||
| 154 | $pid, |
||
| 155 | $jid, |
||
| 156 | $name, |
||
| 157 | $status === Worklog::STATUS_COMPLETE ? true : false, |
||
| 158 | $this->stopwatch->getStartTime(), |
||
| 159 | $this->stopwatch->getStopTime(), |
||
| 160 | $result, |
||
| 161 | $this->worklog_id |
||
| 162 | ]); |
||
| 163 | |||
| 164 | $this->events->emit( new TaskEvent(self::statusToEvent($status), $thetask, $result) ); |
||
|
|
|||
| 165 | |||
| 166 | } catch (Exception $e) { |
||
| 167 | |||
| 168 | // ob_end_clean(); |
||
| 169 | // throw $e; |
||
| 170 | |||
| 171 | if ( $this->stopwatch->isActive() ) { |
||
| 172 | $this->stopwatch->stop(); |
||
| 173 | } |
||
| 174 | |||
| 175 | $result = new Result([ |
||
| 176 | $uid, |
||
| 177 | $pid, |
||
| 178 | $jid, |
||
| 179 | $name, |
||
| 180 | false, |
||
| 181 | $this->stopwatch->getStartTime(), |
||
| 182 | $this->stopwatch->getStopTime(), |
||
| 183 | $e->getMessage(), |
||
| 184 | $this->worklog_id |
||
| 185 | ]); |
||
| 186 | |||
| 187 | } |
||
| 188 | |||
| 189 | $this->stopwatch->clear(); |
||
| 190 | |||
| 191 | ob_end_clean(); |
||
| 192 | |||
| 193 | return $result; |
||
| 194 | |||
| 319 |