| Conditions | 14 |
| Paths | 1152 |
| Total Lines | 67 |
| Code Lines | 35 |
| Lines | 23 |
| Ratio | 34.33 % |
| 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 |
||
| 27 | private function save(\Base $f3, array $prohibitedFields = []) |
||
| 28 | { |
||
| 29 | // set audit user if not set |
||
| 30 | $data = $f3->get('REQUEST'); |
||
| 31 | $user = $f3->get('user'); |
||
| 32 | |||
| 33 | if (!array_key_exists('users_uuid', $data)) { |
||
| 34 | $data['users_uuid'] = $user['uuid']; |
||
| 35 | } |
||
| 36 | |||
| 37 | if (!array_key_exists('client_id', $data)) { |
||
| 38 | $data['client_id'] = Helpers\Str::uuid(16); |
||
| 39 | } |
||
| 40 | |||
| 41 | if (!array_key_exists('token', $data)) { |
||
| 42 | $data['token'] = Helpers\Str::uuid(16); |
||
| 43 | } |
||
| 44 | |||
| 45 | if (!array_key_exists('type', $data)) { |
||
| 46 | $data['type'] = 'access_token'; |
||
| 47 | } |
||
| 48 | |||
| 49 | if (!array_key_exists('scope', $data)) { |
||
| 50 | $data['scope'] = 'read'; |
||
| 51 | } |
||
| 52 | |||
| 53 | // do not allow request to define these fields: |
||
| 54 | foreach ($prohibitedFields as $field) { |
||
| 55 | if (array_key_exists($field, $data)) { |
||
| 56 | unset($data[$field]); |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | // load pre-existing value |
||
| 61 | $m = $this->getMapper(); |
||
| 62 | |||
| 63 | // copy data and validate |
||
| 64 | $m->copyfrom($data); |
||
| 65 | $m->validationRequired([ |
||
| 66 | 'users_uuid' |
||
| 67 | ]); |
||
| 68 | |||
| 69 | $errors = $m->validate(false); |
||
| 70 | View Code Duplication | if (true !== $errors) { |
|
| 71 | foreach ($errors as $error) { |
||
| 72 | $this->setOAuthError('invalid_request'); |
||
| 73 | $this->failure($error['field'], $error['rule']); |
||
| 74 | } |
||
| 75 | } else { |
||
| 76 | // load original record, ovewrite |
||
| 77 | if (!empty($data['uuid'])) { |
||
| 78 | $m->load(['uuid = ?', $data['uuid']]); |
||
| 79 | } |
||
| 80 | $m->copyfrom($data); |
||
| 81 | |||
| 82 | // load in original data and then replace for save |
||
| 83 | if (!$m->save()) { |
||
| 84 | $this->setOAuthError('invalid_request'); |
||
| 85 | $this->failure('error', 'Unable to update object.'); |
||
| 86 | return; |
||
| 87 | } |
||
| 88 | |||
| 89 | // return raw data for object? |
||
| 90 | $adminView = $f3->get('isAdmin') && 'admin' == $f3->get('REQUEST.view'); |
||
| 91 | $this->data = $adminView ? $m->castFields($f3->get('REQUEST.fields')) : $m->exportArray($f3->get('REQUEST.fields')); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 124 |