| Conditions | 12 |
| Paths | 21 |
| Total Lines | 59 |
| 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 |
||
| 69 | public function index(SS_HTTPRequest $request) |
||
| 70 | { |
||
| 71 | $doc = $this->getDocumentFromID($request); |
||
| 72 | |||
| 73 | if (!empty($doc)) { |
||
| 74 | $canView = $doc->canView(); |
||
| 75 | |||
| 76 | if ($canView) { |
||
| 77 | $path = $doc->getFullPath(); |
||
| 78 | if (is_file($path)) { |
||
| 79 | $fileBin = trim(`whereis file`); |
||
| 80 | if (function_exists('finfo_file')) { |
||
| 81 | // discover the mime type properly |
||
| 82 | $finfo = finfo_open(FILEINFO_MIME_TYPE); |
||
| 83 | $mime = finfo_file($finfo, $path); |
||
| 84 | } elseif (is_executable($fileBin)) { |
||
| 85 | $path = escapeshellarg($path); |
||
| 86 | // try to use the system tool |
||
| 87 | $mime = `$fileBin -i -b $path`; |
||
| 88 | $mime = explode(';', $mime); |
||
| 89 | $mime = trim($mime[0]); |
||
| 90 | } else { |
||
| 91 | // make do with what we have |
||
| 92 | $ext = $doc->getExtension(); |
||
| 93 | if ($ext == 'pdf') { |
||
| 94 | $mime = 'application/pdf'; |
||
| 95 | } elseif ($ext == 'html' || $ext =='htm') { |
||
| 96 | $mime = 'text/html'; |
||
| 97 | } else { |
||
| 98 | $mime = 'application/octet-stream'; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | if (self::$testMode) { |
||
| 103 | return $path; |
||
| 104 | } |
||
| 105 | |||
| 106 | // set fallback if no config nor file-specific value |
||
| 107 | $disposition = 'attachment'; |
||
| 108 | |||
| 109 | // file-specific setting |
||
| 110 | if ($doc->DownloadBehavior == 'open') { |
||
| 111 | $disposition = 'inline'; |
||
| 112 | } |
||
| 113 | |||
| 114 | //if a DMSDocument can be downloaded and all the permissions/privileges has passed, |
||
| 115 | //its ViewCount should be increased by 1 just before the browser sending the file to front. |
||
| 116 | $doc->trackView(); |
||
| 117 | |||
| 118 | return $this->sendFile($path, $mime, $doc->getFilenameWithoutID(), $disposition); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | if (self::$testMode) { |
||
| 124 | return 'This asset does not exist.'; |
||
| 125 | } |
||
| 126 | $this->httpError(404, 'This asset does not exist.'); |
||
| 127 | } |
||
| 128 | |||
| 151 |