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