| Conditions | 14 |
| Paths | 20 |
| Total Lines | 81 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 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 |
||
| 100 | public function setupCrawler($crawler_id = null) |
||
| 101 | { |
||
| 102 | $this->log('Setup crawler'); |
||
| 103 | |||
| 104 | if (!is_null($crawler_id)) { |
||
| 105 | $this->log('Setup crawler, crawler_id is not set'); |
||
| 106 | $this->setCrawlerId($crawler_id); |
||
| 107 | } |
||
| 108 | |||
| 109 | if ($this->controllerIsSetup()) { |
||
| 110 | $times = config('laravel-job-handler.run_times', 10); |
||
| 111 | |||
| 112 | for ($x = 0; $x <= $times; $x++) { |
||
| 113 | //fetch the last data |
||
| 114 | $this->getCrawler(); |
||
| 115 | |||
| 116 | $this->log('Checking if crawler is enabled'); |
||
| 117 | if (!$this->crawler->enabled) { |
||
| 118 | $this->log('Crawler is not enabled'); |
||
| 119 | throw new CrawlerException('Crawler (#' . $this->crawler_id . ') - crawler isnt enabled in database'); |
||
| 120 | } |
||
| 121 | |||
| 122 | $this->log('Checking if crawler can be runned'); |
||
| 123 | $checkIfCrawlerCanBeRunned = $this->canCrawlerRunAfterPeriod(); |
||
| 124 | |||
| 125 | if ($checkIfCrawlerCanBeRunned['status']) { |
||
| 126 | $this->log('Checked if crawler can runned'); |
||
| 127 | if (is_null($this->crawler->latest_status)) { |
||
| 128 | $this->log('Crawler can be runned, it the first time'); |
||
| 129 | |||
| 130 | //first time it runs... |
||
| 131 | break; |
||
| 132 | } |
||
| 133 | if ($this->crawler->latest_status == 2) { |
||
| 134 | $this->log('Crawler can be runned, last crawler runned successfully'); |
||
| 135 | |||
| 136 | //Done running... |
||
| 137 | break; |
||
| 138 | } |
||
| 139 | |||
| 140 | |||
| 141 | |||
| 142 | if ($this->crawler->latest_status == 3) { |
||
| 143 | if ($this->override_fail_status) { |
||
| 144 | $this->log('Last crawler failed, but it is forced to run'); |
||
| 145 | |||
| 146 | //override the failed state, this will force to rerun... |
||
| 147 | break; |
||
| 148 | } |
||
| 149 | |||
| 150 | $this->log('Last crawler failed, force run is not enabled'); |
||
| 151 | throw new CrawlerException('Crawler (#' . $this->crawler_id . ') - last run had an error and override_fail_status is not enabled'); |
||
| 152 | } |
||
| 153 | } else { |
||
| 154 | $this->log('Crawler needs to wait ('.$checkIfCrawlerCanBeRunned['retry_in'].' seconds) before running again'); |
||
| 155 | throw new CrawlerNotReachedTimeBetweenJobsException('Has to wait ' . $checkIfCrawlerCanBeRunned['retry_in'] . ' more seconds to run'); |
||
| 156 | } |
||
| 157 | |||
| 158 | if ($x == $times && !$this->override_fail_status) { |
||
| 159 | $this->log('Crawler exceeded the max execution time'); |
||
| 160 | $this->failCrawler('Crawler (#' . $this->crawler_id . ') - max execution time'); |
||
| 161 | } |
||
| 162 | |||
| 163 | if ($this->crawler->status == 1) { |
||
| 164 | if ($this->crawler->multiple_crawlers) { |
||
| 165 | $this->log('Crawler can run multiple crawlers at the same time'); |
||
| 166 | break; |
||
| 167 | } |
||
| 168 | |||
| 169 | $wait = config('laravel-job-handler.retry_in_seconds', 3); |
||
| 170 | |||
| 171 | $this->log('Waiting for rechecking ('.$wait.' seconds) if crawler can be runned'); |
||
| 172 | |||
| 173 | sleep($wait); |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | $this->log('All setup, starting crawler'); |
||
| 178 | $this->startCrawler(); |
||
| 179 | } else { |
||
| 180 | throw new CrawlerException('CrawlController is not setup correctly.'); |
||
| 181 | } |
||
| 374 |