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