OAuth2Apps::save()   F
last analyzed

Complexity

Conditions 13
Paths 576

Size

Total Lines 63
Code Lines 33

Duplication

Lines 23
Ratio 36.51 %

Importance

Changes 0
Metric Value
cc 13
eloc 33
c 0
b 0
f 0
nc 576
nop 2
dl 23
loc 63
rs 3.365

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 OAuth2Apps 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 OAuth2Apps extends Mapper
16
{
17
    protected $table = 'oauth2_apps';
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('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
91
92
    /**
93
     * Update data
94
     *
95
     * @param \Base $f3
96
     * @param array $params
97
     * @return null|array|boolean
98
     */
99
    public function patch(\Base $f3, array $params)
100
    {
101
        $m = $this->getIdObjectIfAdmin($f3, $params, 'client_id', $params['id']);
102
        if (!is_object($m) || null == $m->client_id) {
103
            return;
104
        }
105
106
        $f3->set('REQUEST.client_id', $m->client_id);
107
        $f3->set('REQUEST.client_secret', $m->client_secret);
108
109
        // these fields can't be modified
110
        return $this->save($f3, [
111
            'id'
112
        ]);
113
    }
114
115
116
    /**
117
     * Replace data
118
     *
119
     * @param \Base $f3
120
     * @param array $params
121
     * @return null|array|boolean
122
     */
123
    public function put(\Base $f3, array $params)
124
    {
125
        $m = $this->getIdObjectIfAdmin($f3, $params, 'client_id', $params['id']);
126
        if (!is_object($m) || null == $m->cilent_id) {
127
            return;
128
        }
129
130
        return $this->save($f3, [
131
            'id'
132
        ]);
133
    }
134
135
}
136