| Conditions | 14 |
| Paths | 13 |
| Total Lines | 44 |
| Code Lines | 24 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | 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 |
||
| 20 | public function index() |
||
| 21 | { |
||
| 22 | $request = $this->getRequest(); |
||
| 23 | $ID = (int) $request->getVar("ID"); |
||
| 24 | $Hash = $request->getVar("Hash"); |
||
| 25 | |||
| 26 | if (!$ID || !$Hash) { |
||
| 27 | return $this->httpError(404); |
||
| 28 | } |
||
| 29 | |||
| 30 | $sendDraft = $this->config()->send_draft; |
||
| 31 | |||
| 32 | /** @var File|null $File */ |
||
| 33 | $File = File::get()->byID($ID); |
||
| 34 | if (!$File && $sendDraft && class_exists(Versioned::class)) { |
||
|
|
|||
| 35 | /** @var File|null $File */ |
||
| 36 | $File = Versioned::get_one_by_stage(File::class, Versioned::DRAFT, "ID = " . $ID); |
||
| 37 | } |
||
| 38 | if (!$File) { |
||
| 39 | return $this->httpError(404); |
||
| 40 | } |
||
| 41 | |||
| 42 | // Verify hash |
||
| 43 | $FileHash = substr($File->File->Hash, 0, 10); |
||
| 44 | if ($Hash != $FileHash && !Permission::check("CMS_ACCESS")) { |
||
| 45 | return $this->httpError(404); |
||
| 46 | } |
||
| 47 | |||
| 48 | // Check protected |
||
| 49 | $sendProtected = $this->config()->send_protected; |
||
| 50 | $adminSendProtected = $this->config()->admin_send_protected; |
||
| 51 | $currentUserID = Security::getCurrentUser()->ID ?? 0; |
||
| 52 | $isOwner = $File->OwnerID === $currentUserID; |
||
| 53 | if ($File->getVisibility() == "protected") { |
||
| 54 | if (!$sendProtected && !$isOwner) { |
||
| 55 | if ($adminSendProtected && Permission::check("CMS_ACCESS")) { |
||
| 56 | // We can proceed |
||
| 57 | } else { |
||
| 58 | return $this->httpError(404); |
||
| 59 | } |
||
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | EncryptHelper::sendDecryptedFile($File); |
||
| 64 | } |
||
| 66 |