OAuth2Tokens::save()   F
last analyzed

Complexity

Conditions 14
Paths 1152

Size

Total Lines 67
Code Lines 35

Duplication

Lines 23
Ratio 34.33 %

Importance

Changes 0
Metric Value
cc 14
eloc 35
c 0
b 0
f 0
nc 1152
nop 2
dl 23
loc 67
rs 2.6141

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace FFCMS\Controllers\API;
4
5
use FFMVC\Helpers;
6
use FFCMS\{Traits, Models, Mappers};
7
8
/**
9
 * Api OAuth2Tokens REST Controller Class.
10
 *
11
 * @author Vijay Mahrra <[email protected]>
12
 * @copyright Vijay Mahrra
13
 * @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html)
14
 */
15
class OAuth2Tokens extends Mapper
16
{
17
    protected $table = 'oauth2_tokens';
18
19
20
    /**
21
     * Perform a create/update of the an item, used by POST, PUT, PATCH
22
     *
23
     * @param \Base $f3
24
     * @param array $prohibitedFields
25
     * @return void
26
     */
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
95
96
    /**
97
     * Update data
98
     *
99
     * @param \Base $f3
100
     * @param array $params
101
     * @return null|array|boolean
102
     */
103
    public function patch(\Base $f3, array $params)
104
    {
105
        $m = $this->getIdObjectIfAdmin($f3, $params, 'uuid', $params['id']);
106
        if (!is_object($m) || null == $m->uuid) {
107
            return;
108
        }
109
110
        $f3->set('REQUEST.uuid', $m->uuid);
111
112
        $client_id = $f3->get('REQUEST.client_id');
113
        if (empty($client_id)) {
114
            $f3->set('REQUEST.client_id', $m->client_id);
115
        }
116
117
        // these fields can't be modified
118
        return $this->save($f3, [
119
            'id'
120
        ]);
121
    }
122
123
}
124