| Conditions | 9 |
| Paths | 6 |
| Total Lines | 65 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | 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 |
||
| 25 | public function run($request) |
||
| 26 | { |
||
| 27 | $job = Injector::inst()->create(StaticCacheFullBuildJob::class); |
||
| 28 | $signature = $job->getSignature(); |
||
| 29 | |||
| 30 | // see if we already have this job in a queue |
||
| 31 | $filter = [ |
||
| 32 | 'Signature' => $signature, |
||
| 33 | 'JobStatus' => [ |
||
| 34 | QueuedJob::STATUS_NEW, |
||
| 35 | QueuedJob::STATUS_INIT, |
||
| 36 | ], |
||
| 37 | ]; |
||
| 38 | |||
| 39 | $existing = DataList::create(QueuedJobDescriptor::class)->filter($filter)->first(); |
||
| 40 | |||
| 41 | if ($existing && $existing->exists()) { |
||
| 42 | $this->log(sprintf( |
||
| 43 | 'There is already a %s in the queue, added %s %s', |
||
| 44 | StaticCacheFullBuildJob::class, |
||
| 45 | $existing->Created, |
||
| 46 | $existing->StartAfter ? 'and set to start after ' . $existing->StartAfter : '' |
||
| 47 | )); |
||
| 48 | return false; |
||
| 49 | } |
||
| 50 | |||
| 51 | if ($request->getVar('startAfter')) { |
||
| 52 | $now = DBDatetime::now(); |
||
| 53 | $today = $now->Date(); |
||
| 54 | $startTime = $request->getVar('startAfter'); |
||
| 55 | |||
| 56 | // move to tomorrow if the starttime has passed today |
||
| 57 | if ($now->Time24() > $startTime) { |
||
| 58 | $timestamp = strtotime($today . ' ' . $startTime . ' +1 day'); |
||
| 59 | $dayWord = 'tomorrow'; |
||
| 60 | } else { |
||
| 61 | $timestamp = strtotime($today . ' ' . $startTime); |
||
| 62 | $dayWord = 'today'; |
||
| 63 | } |
||
| 64 | $startAfter = (new \DateTime())->setTimestamp($timestamp); |
||
| 65 | $thisTimeTomorrow = (new \DateTime())->setTimestamp(strtotime($now . ' +1 day'))->getTimestamp(); |
||
| 66 | |||
| 67 | // sanity check that we are in the next 24 hours - prevents some weird stuff sneaking through |
||
| 68 | if ($startAfter->getTimestamp() > $thisTimeTomorrow || $startAfter->getTimestamp() < $now->getTimestamp()) { |
||
| 69 | $this->log('Invalid startAfter parameter passed. Please ensure the time format is HHmm e.g. 1300'); |
||
| 70 | return false; |
||
| 71 | } |
||
| 72 | |||
| 73 | $this->log(sprintf( |
||
| 74 | '%s queued for %s %s.', |
||
| 75 | StaticCacheFullBuildJob::class, |
||
| 76 | $startAfter->format('H:m'), |
||
| 77 | $dayWord |
||
| 78 | )); |
||
| 79 | } else { |
||
| 80 | $startAfter = null; |
||
| 81 | $this->log(StaticCacheFullBuildJob::class . ' added to the queue for immediate processing'); |
||
| 82 | } |
||
| 83 | |||
| 84 | $job->setJobData(0, 0, false, new \stdClass(), [ |
||
| 85 | 'Building static cache for full site', |
||
| 86 | ]); |
||
| 87 | QueuedJobService::singleton()->queueJob($job, $startAfter ? $startAfter->format('Y-m-d H:i:s') : null); |
||
| 88 | |||
| 89 | return true; |
||
| 90 | } |
||
| 98 |