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