| Conditions | 6 |
| Paths | 5 |
| Total Lines | 71 |
| Code Lines | 42 |
| 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 |
||
| 31 | public function reset(HTTPRequest $request): HTTPResponse |
||
| 32 | { |
||
| 33 | if (!$request->isPOST() || !$request->param('ID')) { |
||
| 34 | return $this->owner |
||
| 35 | ->getResponse() |
||
| 36 | ->setStatusCode(400) |
||
| 37 | ->addHeader('Content-Type', 'application/json') |
||
| 38 | ->setBody(json_encode( |
||
| 39 | [ |
||
| 40 | 'error' => _t(__CLASS__ . '.BAD_REQUEST', 'Invalid request') |
||
| 41 | ] |
||
| 42 | )); |
||
| 43 | } |
||
| 44 | |||
| 45 | if (!Permission::check(MemberMFAExtension::MFA_ADMINISTER_REGISTERED_METHODS)) { |
||
| 46 | return $this->owner |
||
| 47 | ->getResponse() |
||
| 48 | ->setStatusCode(403) |
||
| 49 | ->addHeader('Content-Type', 'application/json') |
||
| 50 | ->setBody(json_encode( |
||
| 51 | [ |
||
| 52 | 'error' => _t( |
||
| 53 | __CLASS__ . '.INSUFFICIENT_PERMISSIONS', |
||
| 54 | 'Insufficient permissions to reset user' |
||
| 55 | ) |
||
| 56 | ] |
||
| 57 | )); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** @var Member $memberToReset */ |
||
| 61 | $memberToReset = Member::get()->byID($request->param('ID')); |
||
| 62 | |||
| 63 | if ($memberToReset === null) { |
||
| 64 | return $this->owner |
||
| 65 | ->getResponse() |
||
| 66 | ->setStatusCode(403) |
||
| 67 | ->addHeader('Content-Type', 'application/json') |
||
| 68 | ->setBody(json_encode( |
||
| 69 | [ |
||
| 70 | 'error' => _t( |
||
| 71 | __CLASS__ . '.INVALID_MEMBER', |
||
| 72 | 'Requested member for reset not found' |
||
| 73 | ) |
||
| 74 | ] |
||
| 75 | )); |
||
| 76 | } |
||
| 77 | |||
| 78 | $sent = $this->sendResetEmail($memberToReset); |
||
| 79 | |||
| 80 | if (!$sent) { |
||
| 81 | return $this->owner |
||
| 82 | ->getResponse() |
||
| 83 | ->setStatusCode(500) |
||
| 84 | ->addHeader('Content-Type', 'application/json') |
||
| 85 | ->setBody(json_encode( |
||
| 86 | [ |
||
| 87 | 'error' => _t( |
||
| 88 | __CLASS__ . '.EMAIL_NOT_SENT', |
||
| 89 | 'Email sending failed' |
||
| 90 | ) |
||
| 91 | ] |
||
| 92 | )); |
||
| 93 | } |
||
| 94 | |||
| 95 | return $this->owner |
||
| 96 | ->getResponse() |
||
| 97 | ->setStatusCode(200) |
||
| 98 | ->addHeader('Content-Type', 'application/json') |
||
| 99 | ->setBody(json_encode( |
||
| 100 | [ |
||
| 101 | 'success' => true, |
||
| 102 | ] |
||
| 143 |