Completed
Push — dev-master ( e1a6ef...11f3cc )
by Vijay
03:14
created

Reports::save()   D

Complexity

Conditions 9
Paths 42

Size

Total Lines 43
Code Lines 26

Duplication

Lines 43
Ratio 100 %

Importance

Changes 0
Metric Value
cc 9
eloc 26
c 0
b 0
f 0
nc 42
nop 2
dl 43
loc 43
rs 4.909
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
 * Inherits REST from UsersData due to same keys.
11
 *
12
 * @author Vijay Mahrra <[email protected]>
13
 * @copyright Vijay Mahrra
14
 * @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html)
15
 */
16
class Reports extends UsersData
17
{
18
    protected $adminOnly = true;
19
20
21
    /**
22
     * Perform a create/update of the an item, used by POST, PUT, PATCH
23
     *
24
     * @param \Base $f3
25
     * @param array $prohibitedFields
26
     * @return void
27
     */
28 View Code Duplication
    protected function save(\Base $f3, array $prohibitedFields = [])
29
    {
30
        // do not allow request to define these fields:
31
        $data = $f3->get('REQUEST');
32
        foreach ($prohibitedFields as $field) {
33
            if (array_key_exists($field, $data)) {
34
                unset($data[$field]);
35
            }
36
        }
37
38
        // load pre-existing value
39
        $db = \Registry::get('db');
40
        $m = $this->getMapper();
41
        if ($f3->get('VERB') == 'PUT') {
42
            $m->load(['uuid = ?', $data['uuid']]);
43
        } else {
44
            $m->load(['users_uuid = ? AND ' . $db->quotekey('key') . ' = ?', $data['users_uuid'], $data['key']]);
45
        }
46
47
        // copy data and validate
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->save()) {
61
                $this->setOAuthError('invalid_request');
62
                $this->failure('error', 'Unable to update object.');
63
                return;
64
            }
65
66
            // return raw data for object?
67
            $adminView = $f3->get('isAdmin') && 'admin' == $f3->get('REQUEST.view');
68
            $this->data = $adminView ? $m->castFields($f3->get('REQUEST.fields')) : $m->exportArray($f3->get('REQUEST.fields'));
69
        }
70
    }
71
72
}
73