| Conditions | 7 |
| Paths | 6 |
| Total Lines | 57 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 109 | public function index() |
||
| 110 | { |
||
| 111 | $request = $this->getRequest(); |
||
| 112 | $member = Security::getCurrentUser(); |
||
| 113 | $file = File::get()->byID($request->param("ID")); |
||
| 114 | |||
| 115 | if (empty($file)) { |
||
| 116 | return $this->httpError(404); |
||
| 117 | } |
||
| 118 | |||
| 119 | // Does this file need to have permissions checked? |
||
| 120 | $product = $file->DownloadableProduct(); |
||
| 121 | |||
| 122 | if (!$product->exists()) { |
||
| 123 | return $this->redirect($file->AbsoluteLink()); |
||
| 124 | } |
||
| 125 | |||
| 126 | // If the user is logged in, can they download this file? |
||
| 127 | if (isset($member) && $product->canDownload($member)) { |
||
| 128 | $this->extend('onBeforeSendFile', $file); |
||
| 129 | return $this->sendFile($file); |
||
| 130 | } |
||
| 131 | |||
| 132 | // Finally Attempt to get the invoice from the URL vars |
||
| 133 | // and see if it matches this download |
||
| 134 | $invoice = Invoice::get()->filter( |
||
| 135 | [ |
||
| 136 | "ID" => $request->param('InvoiceID'), |
||
| 137 | "AccessKey" => $request->param('AccessKey') |
||
| 138 | ] |
||
| 139 | )->first(); |
||
| 140 | |||
| 141 | if (isset($invoice)) { |
||
| 142 | $origin = new DateTime($invoice->dbObject('StartDate')->Rfc822()); |
||
| 143 | $now = new DateTime(); |
||
| 144 | $diff = (int) $now->diff($origin)->format('%d'); |
||
| 145 | |||
| 146 | if ($diff < $product->LinkLife) { |
||
| 147 | $this->extend('onBeforeSendFile', $file); |
||
| 148 | return $this->sendFile($file); |
||
| 149 | } else { |
||
| 150 | return Security::permissionFailure( |
||
| 151 | $this, |
||
| 152 | _t( |
||
| 153 | 'SilverCommerce\DownloadableProducts.LinkExpired', |
||
| 154 | 'This download link has now expired.' |
||
| 155 | ) |
||
| 156 | ); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | // Finally, return a login screen |
||
| 161 | return Security::permissionFailure( |
||
| 162 | $this, |
||
| 163 | _t( |
||
| 164 | 'SilverCommerce\DownloadableProducts.NotAuthorised', |
||
| 165 | 'You are not authorised to access this resource. Please log in.' |
||
| 166 | ) |
||
| 199 |