| Conditions | 11 |
| Paths | 13 |
| Total Lines | 72 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 28 | protected function main() |
||
| 29 | { |
||
| 30 | $this->checkPosted(); |
||
| 31 | $database = $this->getDatabase(); |
||
| 32 | $request = $this->getRequest($database); |
||
| 33 | $currentUser = User::getCurrent($database); |
||
| 34 | |||
| 35 | $target = WebRequest::postString('target'); |
||
| 36 | |||
| 37 | // FIXME: domains! |
||
| 38 | $requestQueue = RequestQueue::getByApiName($database, $target, 1); |
||
|
|
|||
| 39 | |||
| 40 | if ($requestQueue === false) { |
||
| 41 | throw new ApplicationLogicException('Defer target not valid'); |
||
| 42 | } |
||
| 43 | |||
| 44 | if ($request->getQueue() == $requestQueue->getId() && $request->getStatus() == RequestStatus::OPEN) { |
||
| 45 | SessionAlert::warning('This request is already in the specified queue.'); |
||
| 46 | $this->redirect('viewRequest', null, array('id' => $request->getId())); |
||
| 47 | |||
| 48 | return; |
||
| 49 | } |
||
| 50 | |||
| 51 | $closureDate = $request->getClosureDate(); |
||
| 52 | $date = new DateTime(); |
||
| 53 | $date->modify("-7 days"); |
||
| 54 | |||
| 55 | if ($request->getStatus() == RequestStatus::CLOSED && $closureDate < $date) { |
||
| 56 | if (!$this->barrierTest('reopenOldRequest', $currentUser, 'RequestData')) { |
||
| 57 | throw new ApplicationLogicException( |
||
| 58 | "You are not allowed to re-open a request that has been closed for over a week."); |
||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | if ($request->getEmail() === $this->getSiteConfiguration()->getDataClearEmail()) { |
||
| 63 | if (!$this->barrierTest('reopenClearedRequest', $currentUser, 'RequestData')) { |
||
| 64 | throw new ApplicationLogicException( |
||
| 65 | "You are not allowed to re-open a request for which the private data has been purged."); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | if ($request->getStatus() === RequestStatus::JOBQUEUE) { |
||
| 70 | /** @var JobQueue[] $pendingJobs */ |
||
| 71 | $pendingJobs = JobQueueSearchHelper::get($database)->byRequest($request->getId())->statusIn([ |
||
| 72 | JobQueue::STATUS_QUEUED, |
||
| 73 | JobQueue::STATUS_READY, |
||
| 74 | JobQueue::STATUS_WAITING, |
||
| 75 | ])->fetch(); |
||
| 76 | |||
| 77 | foreach ($pendingJobs as $job) { |
||
| 78 | $job->setStatus(JobQueue::STATUS_CANCELLED); |
||
| 79 | $job->setError('Cancelled by request deferral'); |
||
| 80 | $job->save(); |
||
| 81 | |||
| 82 | Logger::backgroundJobCancelled($database, $job); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | $request->setReserved(null); |
||
| 87 | $request->setStatus(RequestStatus::OPEN); |
||
| 88 | $request->setQueue($requestQueue->getId()); |
||
| 89 | $request->setUpdateVersion(WebRequest::postInt('updateversion')); |
||
| 90 | $request->save(); |
||
| 91 | |||
| 92 | Logger::deferRequest($database, $request, $requestQueue->getLogName()); |
||
| 93 | |||
| 94 | $this->getNotificationHelper()->requestDeferred($request); |
||
| 95 | |||
| 96 | $deto = htmlentities($requestQueue->getDisplayName(), ENT_COMPAT, 'UTF-8'); |
||
| 97 | SessionAlert::success("Request {$request->getId()} deferred to {$deto}"); |
||
| 98 | |||
| 99 | $this->redirect(); |
||
| 100 | } |
||
| 102 |