| Conditions | 8 |
| Paths | 21 |
| Total Lines | 63 |
| Code Lines | 40 |
| Lines | 5 |
| Ratio | 7.94 % |
| 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 |
||
| 51 | public function appPost(\Base $f3) |
||
| 52 | { |
||
| 53 | $this->csrf('@api_apps'); |
||
| 54 | $this->redirectLoggedOutUser(); |
||
| 55 | |||
| 56 | $view = 'apps/apps.phtml'; |
||
| 57 | $oAuth2Model = Models\OAuth2::instance(); |
||
| 58 | $appsMapper = $oAuth2Model->getAppsMapper(); |
||
| 59 | |||
| 60 | // filter input vars of request, set back into REQUEST |
||
| 61 | $appsMapper->copyfrom($f3->get('REQUEST')); |
||
| 62 | $data = $appsMapper->filter(); |
||
| 63 | $request = $f3->get('REQUEST'); |
||
| 64 | foreach ($data as $k => $v) { |
||
| 65 | if (array_key_exists($k, $request)) { |
||
| 66 | $f3->set('REQUEST.' . $k, $v); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | // check app name exists |
||
| 71 | $db = \Registry::get('db'); |
||
| 72 | $m = clone $appsMapper; |
||
| 73 | if ($m->load(['LOWER('.$db->quotekey('name').') = LOWER(?)', $m->name]) && null !== $m->client_id) { |
||
| 74 | $this->notify(_('An app with that name is already in use!'), 'warning'); |
||
| 75 | $f3->set('form', $f3->get('REQUEST')); |
||
| 76 | echo \View::instance()->render($view); |
||
| 77 | return; |
||
| 78 | } |
||
| 79 | |||
| 80 | // check required fields |
||
| 81 | $appsMapper->validationRequired([ |
||
| 82 | 'name', |
||
| 83 | 'description', |
||
| 84 | 'callback_uri', |
||
| 85 | ]); |
||
| 86 | |||
| 87 | // admin group auto-approved |
||
| 88 | $scopes = $f3->get('userScopes'); |
||
| 89 | |||
| 90 | // set defaults |
||
| 91 | $appsMapper->client_id = $appsMapper->setUUID('client_id'); |
||
| 92 | $appsMapper->client_secret = $appsMapper->setUUID('client_secret'); |
||
| 93 | $appsMapper->users_uuid = $f3->get('uuid'); |
||
| 94 | $appsMapper->scope = join(',', $scopes); |
||
| 95 | $appsMapper->status = in_array('admin', $scopes) ? 'approved' : 'registered'; |
||
| 96 | $appsMapper->created = Helpers\Time::database(); |
||
| 97 | |||
| 98 | // at this point the app can be validated |
||
| 99 | if (true !== $appsMapper->validate()) { |
||
| 100 | $this->notify(['info' => $appsMapper->validationErrors($appsMapper->validate(false))]); |
||
| 101 | $f3->set('form', $f3->get('REQUEST')); |
||
| 102 | echo \View::instance()->render($view); |
||
| 103 | return; |
||
| 104 | } |
||
| 105 | |||
| 106 | View Code Duplication | if ($appsMapper->save()) { |
|
| 107 | $this->notify(_('Your new app has been registered!'), 'success'); |
||
| 108 | } else { |
||
| 109 | $this->notify(_('App registration failed!'), 'error'); |
||
| 110 | } |
||
| 111 | |||
| 112 | $f3->reroute('@api_apps'); |
||
| 113 | } |
||
| 114 | |||
| 171 |