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

UsersData::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 UsersData 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 UsersData extends Mapper
16
{
17
    protected $adminOnly = false;
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 View Code Duplication
    protected 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
        $m->copyfrom($data);
48
        $m->validationRequired([
49
            'users_uuid', 'key', 'value'
50
        ]);
51
        $errors = $m->validate(false);
52
        if (true !== $errors) {
53
            foreach ($errors as $error) {
54
                $this->setOAuthError('invalid_request');
55
                $this->failure($error['field'], $error['rule']);
56
            }
57
        } else {
58
            // load in original data and then replace for save
59
            if (!$m->save()) {
60
                $this->setOAuthError('invalid_request');
61
                $this->failure('error', 'Unable to update object.');
62
                return;
63
            }
64
65
            // return raw data for object?
66
            $adminView = $f3->get('isAdmin') && 'admin' == $f3->get('REQUEST.view');
67
            $this->data = $adminView ? $m->castFields($f3->get('REQUEST.fields')) : $m->exportArray($f3->get('REQUEST.fields'));
68
        }
69
    }
70
71
72
    /**
73
     * Update data
74
     *
75
     * @param \Base $f3
76
     * @param array $params
77
     * @return null|array|boolean
78
     */
79
    public function patch(\Base $f3, array $params)
80
    {
81
        $isAdmin = $f3->get('isAdmin');
82
        $m = $this->getIdObjectIfUser($f3, $params, 'uuid', $params['id']);
83
        if (!is_object($m) || null == $m->uuid) {
84
            return;
85
        } elseif (!$isAdmin && $m->users_uuid !== $f3->get('uuid')) {
86
            $this->failure('authentication_error', "User does not have permission.", 401);
87
            return $this->setOAuthError('access_denied');
88
        }
89
90
        $f3->set('REQUEST.users_uuid', $m->users_uuid);
91
        $f3->set('REQUEST.key', $m->key);
92
93
        // these fields can't be modified
94
        return $this->save($f3, [
95
            'id', 'uuid'
96
        ]);
97
    }
98
99
100
    /**
101
     * Replace data
102
     *
103
     * @param \Base $f3
104
     * @param array $params
105
     * @return null|array|boolean
106
     */
107
    public function put(\Base $f3, array $params)
108
    {
109
        $isAdmin = $f3->get('isAdmin');
110
        $m = $this->getIdObjectIfUser($f3, $params, 'uuid', $params['id']);
111
        if (!is_object($m) || null == $m->uuid) {
112
            return;
113
        } elseif (!$isAdmin && $m->users_uuid !== $f3->get('uuid')) {
114
            $this->failure('authentication_error', "User does not have permission.", 401);
115
            return $this->setOAuthError('access_denied');
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 null|array|boolean
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
        $f3->set('REQUEST.users_uuid', $users_uuid);
146
147
        // this fields can't be modified
148
        $prohibitedFields = [
149
            'id', 'uuid'
150
        ];
151
152
        return $this->save($f3, $prohibitedFields);
153
    }
154
155
156
}
157