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