| Conditions | 16 |
| Paths | 87 |
| Total Lines | 85 |
| Code Lines | 54 |
| 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 |
||
| 42 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 43 | { |
||
| 44 | $this->detectMagento($output, true); |
||
| 45 | if (!$this->initMagento()) { |
||
| 46 | return; |
||
| 47 | } |
||
| 48 | |||
| 49 | $jobCode = $input->getArgument('job'); |
||
| 50 | if (!$jobCode) { |
||
| 51 | $this->writeSection($output, 'Cronjob'); |
||
| 52 | $jobCode = $this->askJobCode($output, $this->getJobs()); |
||
| 53 | } |
||
| 54 | |||
| 55 | $jobsRoot = Mage::getConfig()->getNode('crontab/jobs'); |
||
| 56 | $defaultJobsRoot = Mage::getConfig()->getNode('default/crontab/jobs'); |
||
| 57 | |||
| 58 | $jobConfig = $jobsRoot->{$jobCode}; |
||
| 59 | if (!$jobConfig || !$jobConfig->run) { |
||
| 60 | $jobConfig = $defaultJobsRoot->{$jobCode}; |
||
| 61 | if (!$jobConfig || !$jobConfig->run) { |
||
| 62 | throw new RuntimeException('No job config found!'); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | $runConfig = $jobConfig->run; |
||
| 67 | |||
| 68 | if ($runConfig->model) { |
||
| 69 | if (!preg_match(self::REGEX_RUN_MODEL, (string) $runConfig->model, $run)) { |
||
| 70 | throw new RuntimeException('Invalid model/method definition, expecting "model/class::method".'); |
||
| 71 | } |
||
| 72 | |||
| 73 | list(, $runModel, $runMethod) = $run; |
||
| 74 | $model = Mage::getModel($runModel); |
||
| 75 | if (false === $model) { |
||
| 76 | throw new RuntimeException(sprintf('Failed to create new "%s" model', $run[1])); |
||
| 77 | } |
||
| 78 | $callback = array($model, $runMethod); |
||
| 79 | $callableName = sprintf("%s::%s", $runModel, $runMethod); |
||
| 80 | if (!$model || !is_callable($callback, false, $callableName)) { |
||
| 81 | throw new RuntimeException(sprintf('Invalid callback: %s', $callableName)); |
||
| 82 | } |
||
| 83 | |||
| 84 | $output->write('<info>Run </info><comment>' . $callableName . '</comment> '); |
||
| 85 | |||
| 86 | Mage::getConfig()->init()->loadEventObservers('crontab'); |
||
| 87 | Mage::app()->addEventArea('crontab'); |
||
| 88 | |||
| 89 | /* @var $schedule Mage_Cron_Model_Schedule */ |
||
| 90 | $schedule = Mage::getModel('cron/schedule'); |
||
| 91 | if (false === $schedule) { |
||
| 92 | throw new RuntimeException('Failed to create new Mage_Cron_Model_Schedule model'); |
||
| 93 | } |
||
| 94 | |||
| 95 | try { |
||
| 96 | $strftime = strftime('%Y-%m-%d %H:%M:%S', time()); |
||
| 97 | $schedule |
||
| 98 | ->setJobCode($jobCode) |
||
| 99 | ->setStatus(Mage_Cron_Model_Schedule::STATUS_RUNNING) |
||
| 100 | ->setCreatedAt($strftime) |
||
| 101 | ->setExecutedAt($strftime) |
||
| 102 | ->save(); |
||
| 103 | |||
| 104 | call_user_func_array($callback, array($schedule)); |
||
| 105 | |||
| 106 | $schedule->setStatus(Mage_Cron_Model_Schedule::STATUS_SUCCESS); |
||
| 107 | } catch (Exception $cronException) { |
||
| 108 | $schedule->setStatus(Mage_Cron_Model_Schedule::STATUS_ERROR); |
||
| 109 | } |
||
| 110 | |||
| 111 | $schedule->setFinishedAt(strftime('%Y-%m-%d %H:%M:%S', time()))->save(); |
||
| 112 | |||
| 113 | if (isset($cronException)) { |
||
| 114 | throw new RuntimeException( |
||
| 115 | sprintf('Cron-job "%s" threw exception %s', $jobCode, get_class($cronException)), |
||
| 116 | 0, |
||
| 117 | $cronException |
||
| 118 | ); |
||
| 119 | } |
||
| 120 | |||
| 121 | $output->writeln('<info>done</info>'); |
||
| 122 | } |
||
| 123 | if (empty($callback)) { |
||
| 124 | Mage::throwException(Mage::helper('cron')->__('No callbacks found')); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 164 |