Passed
Push — dev-master ( 289114...1a42ae )
by Vijay
34:35
created

Reports::save()   C

Complexity

Conditions 9
Paths 42

Size

Total Lines 52
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 33
nc 42
nop 2
dl 0
loc 52
rs 6.5703
c 0
b 0
f 0

How to fix   Long Method   

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 Reports 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 Reports extends APIMapper
16
{
17
    protected $adminOnly = true;
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
        // do not allow request to define these fields:
30
        $data = $f3->get('REQUEST');
31
        foreach ($prohibitedFields as $field) {
32
            if (array_key_exists($field, $data)) {
33
                unset($data[$field]);
34
            }
35
        }
36
37
        // load pre-existing value
38
        $db = \Registry::get('db');
39
        $m = $this->getMapper();
40
        if ($f3->get('VERB') == 'PUT') {
41
            $m->load(['uuid = ?', $data['uuid']]);
42
        } else {
43
            $m->load(['users_uuid = ? AND ' . $db->quotekey('key') . ' = ?', $data['users_uuid'], $data['key']]);
44
        }
45
46
        // copy data and validate
47
        $oldMapper = clone($m);
48
        $m->copyfrom($data);
49
        $m->validationRequired([
50
            'users_uuid', 'key', 'name', 'query'
51
        ]);
52
        $errors = $m->validate(false);
53
        if (true !== $errors) {
54
            foreach ($errors as $error) {
55
                $this->setOAuthError('invalid_request');
56
                $this->failure($error['field'], $error['rule']);
57
            }
58
        } else {
59
            // load in original data and then replace for save
60
            if (!$m->validateSave()) {
61
                $this->setOAuthError('invalid_request');
62
                $this->failure('error', 'Unable to update object.');
63
                return;
64
            }
65
66
            $this->audit([
67
                'users_uuid' => $m->users_uuid,
68
                'actor' => $m->client_id,
69
                'event' => 'Report Updated via API',
70
                'old' => $oldMapper->cast(),
71
                'new' => $m->cast()
72
            ]);
73
74
            // return raw data for object?
75
            $adminView = $f3->get('isAdmin') && 'admin' == $f3->get('REQUEST.view');
76
            $this->data = $adminView ? $m->castFields($f3->get('REQUEST.fields')) : $m->exportArray($f3->get('REQUEST.fields'));
77
        }
78
    }
79
80
81
    /**
82
     * Update data
83
     *
84
     * @param \Base $f3
85
     * @param array $params
86
     * @return void
87
     */
88
    public function patch(\Base $f3, array $params)
89
    {
90
        $m = $this->getIdObjectIfAdmin($f3, $params, 'uuid', $f3->get('id'));
91
        if (!is_object($m) || null == $m->uuid) {
92
            return;
93
        }
94
95
        $f3->set('REQUEST.users_uuid', $m->users_uuid);
96
        $f3->set('REQUEST.key', $m->key);
97
98
        // these fields can't be modified
99
        return $this->save($f3, [
100
            'id', 'uuid'
101
        ]);
102
    }
103
104
105
    /**
106
     * Replace data
107
     *
108
     * @param \Base $f3
109
     * @return void
110
     */
111
    public function put(\Base $f3)
112
    {
113
        $m = $this->getIdObjectIfAdmin($f3, $params, 'uuid', $f3->get('id'));
0 ignored issues
show
Bug introduced by
The variable $params does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
114
        if (!is_object($m) || null == $m->uuid) {
115
            return;
116
        }
117
118
        $f3->set('REQUEST.uuid', $m->uuid);
119
        $f3->set('REQUEST.users_uuid', $m->users_uuid);
120
121
        // these fields can't be modified
122
        return $this->save($f3, [
123
            'id'
124
        ]);
125
    }
126
127
128
    /**
129
     * Create new data
130
     *
131
     * @param \Base $f3
132
     * @param array $params
133
     * @return void
134
     */
135
    public function post(\Base $f3, array $params)
136
    {
137
        $isAdmin = $f3->get('isAdmin');
138
        if ($isAdmin && !empty($params) && array_key_exists('id', $params)) {
139
            $users_uuid = $params['id'];
140
        } elseif (!$isAdmin) {
141
            $users_uuid = $f3->get('uuid');
142
        } else {
143
            $users_uuid = $f3->get('REQUEST.users_uuid');
144
        }
145
        if (!$isAdmin) {
146
            return;
147
        }
148
        $f3->set('REQUEST.users_uuid', $users_uuid);
149
150
        // this fields can't be modified
151
        $prohibitedFields = [
152
            'id', 'uuid'
153
        ];
154
155
        return $this->save($f3, $prohibitedFields);
156
    }
157
158
159
}
160