| Conditions | 7 |
| Paths | 6 |
| Total Lines | 64 |
| Code Lines | 30 |
| 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 declare(strict_types=1); |
||
| 46 | public function reset(HTTPRequest $request): HTTPResponse |
||
| 47 | { |
||
| 48 | if (!$request->isPOST() || !$request->param('ID')) { |
||
| 49 | return $this->jsonResponse( |
||
| 50 | [ |
||
| 51 | 'error' => _t(__CLASS__ . '.BAD_REQUEST', 'Invalid request') |
||
| 52 | ], |
||
| 53 | 400 |
||
| 54 | ); |
||
| 55 | } |
||
| 56 | |||
| 57 | $body = json_decode($request->getBody() ?? '', true); |
||
| 58 | |||
| 59 | if (!SecurityToken::inst()->check($body['csrf_token'] ?? null)) { |
||
| 60 | return $this->jsonResponse( |
||
| 61 | [ |
||
| 62 | 'error' => _t(__CLASS__ . '.INVALID_CSRF_TOKEN', 'Invalid or missing CSRF token') |
||
| 63 | ], |
||
| 64 | 400 |
||
| 65 | ); |
||
| 66 | } |
||
| 67 | |||
| 68 | if (!Permission::check(MemberExtension::MFA_ADMINISTER_REGISTERED_METHODS)) { |
||
| 69 | return $this->jsonResponse( |
||
| 70 | [ |
||
| 71 | 'error' => _t( |
||
| 72 | __CLASS__ . '.INSUFFICIENT_PERMISSIONS', |
||
| 73 | 'Insufficient permissions to reset user' |
||
| 74 | ) |
||
| 75 | ], |
||
| 76 | 403 |
||
| 77 | ); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** @var Member $memberToReset */ |
||
| 81 | $memberToReset = Member::get()->byID($request->param('ID')); |
||
| 82 | |||
| 83 | if ($memberToReset === null) { |
||
| 84 | return $this->jsonResponse( |
||
| 85 | [ |
||
| 86 | 'error' => _t( |
||
| 87 | __CLASS__ . '.INVALID_MEMBER', |
||
| 88 | 'Requested member for reset not found' |
||
| 89 | ) |
||
| 90 | ], |
||
| 91 | 403 |
||
| 92 | ); |
||
| 93 | } |
||
| 94 | |||
| 95 | $sent = $this->sendResetEmail($memberToReset); |
||
| 96 | |||
| 97 | if (!$sent) { |
||
| 98 | return $this->jsonResponse( |
||
| 99 | [ |
||
| 100 | 'error' => _t( |
||
| 101 | __CLASS__ . '.EMAIL_NOT_SENT', |
||
| 102 | 'Email sending failed' |
||
| 103 | ) |
||
| 104 | ], |
||
| 105 | 500 |
||
| 106 | ); |
||
| 107 | } |
||
| 108 | |||
| 109 | return $this->jsonResponse(['success' => true], 200); |
||
| 110 | } |
||
| 157 |