| Conditions | 9 |
| Paths | 19 |
| Total Lines | 70 |
| Code Lines | 46 |
| 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 |
||
| 118 | private function executeConfigModel( |
||
| 119 | OutputInterface $output, |
||
| 120 | $runConfigModel, |
||
| 121 | $jobCode |
||
| 122 | ) { |
||
| 123 | if (!preg_match(self::REGEX_RUN_MODEL, $runConfigModel, $runMatches)) { |
||
| 124 | throw new RuntimeException( |
||
| 125 | sprintf( |
||
| 126 | 'Invalid model/method definition "%s" for job "%s", expecting "model/class::method".', |
||
| 127 | $runConfigModel, |
||
| 128 | $jobCode |
||
| 129 | ) |
||
| 130 | ); |
||
| 131 | } |
||
| 132 | list(, $runModel, $runMethod) = $runMatches; |
||
| 133 | unset($runMatches); |
||
| 134 | |||
| 135 | $model = Mage::getModel($runModel); |
||
| 136 | if (false === $model) { |
||
| 137 | throw new RuntimeException(sprintf('Failed to create new "%s" model for job "%s"', $runModel, $jobCode)); |
||
| 138 | } |
||
| 139 | $callback = array($model, $runMethod); |
||
| 140 | $callableName = sprintf("%s::%s", $runModel, $runMethod); |
||
| 141 | if (!$model || !is_callable($callback, false, $callableName)) { |
||
| 142 | throw new RuntimeException(sprintf('Invalid callback: %s for job "%s"', $callableName, $jobCode)); |
||
| 143 | } |
||
| 144 | |||
| 145 | $output->write('<info>Run </info><comment>' . $callableName . '</comment> '); |
||
| 146 | |||
| 147 | Mage::getConfig()->init()->loadEventObservers('crontab'); |
||
| 148 | Mage::app()->addEventArea('crontab'); |
||
| 149 | |||
| 150 | /* @var $schedule Mage_Cron_Model_Schedule */ |
||
| 151 | $schedule = Mage::getModel('cron/schedule'); |
||
| 152 | if (false === $schedule) { |
||
| 153 | throw new RuntimeException('Failed to create new Mage_Cron_Model_Schedule model'); |
||
| 154 | } |
||
| 155 | |||
| 156 | try { |
||
| 157 | $timestamp = strftime('%Y-%m-%d %H:%M:%S', time()); |
||
| 158 | $schedule |
||
| 159 | ->setJobCode($jobCode) |
||
| 160 | ->setStatus(Mage_Cron_Model_Schedule::STATUS_RUNNING) |
||
| 161 | ->setCreatedAt($timestamp) |
||
| 162 | ->setExecutedAt($timestamp) |
||
| 163 | ->save(); |
||
| 164 | |||
| 165 | call_user_func_array($callback, array($schedule)); |
||
| 166 | |||
| 167 | $schedule->setStatus(Mage_Cron_Model_Schedule::STATUS_SUCCESS); |
||
| 168 | } catch (Exception $cronException) { |
||
| 169 | $schedule->setStatus(Mage_Cron_Model_Schedule::STATUS_ERROR); |
||
| 170 | } |
||
| 171 | |||
| 172 | $schedule->setFinishedAt(strftime('%Y-%m-%d %H:%M:%S', time()))->save(); |
||
| 173 | |||
| 174 | if (isset($cronException)) { |
||
| 175 | throw new RuntimeException( |
||
| 176 | sprintf('Cron-job "%s" threw exception %s', $jobCode, get_class($cronException)), |
||
| 177 | 0, |
||
| 178 | $cronException |
||
| 179 | ); |
||
| 180 | } |
||
| 181 | |||
| 182 | $output->writeln('<info>done</info>'); |
||
| 183 | |||
| 184 | if (empty($callback)) { |
||
| 185 | Mage::throwException(Mage::helper('cron')->__('No callbacks found')); |
||
| 186 | } |
||
| 187 | } |
||
| 188 | } |
||
| 189 |