Completed
Push — dev-master ( 8c1809...065bc3 )
by Vijay
03:22
created

ConfigData::post()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
c 0
b 0
f 0
nc 2
nop 1
dl 14
loc 14
rs 9.4285
1
<?php
2
3
namespace FFCMS\Controllers\API;
4
5
use FFMVC\Helpers;
6
use FFCMS\{Traits, Models, Mappers};
7
8
/**
9
 * Api ConfigData 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 ConfigData extends Mapper
16
{
17
    /**
18
     * Perform a create/update of the an item, used by POST, PUT, PATCH
19
     *
20
     * @param \Base $f3
21
     * @param array $prohibitedFields
22
     * @return void
23
     */
24
    private function save(\Base $f3, array $prohibitedFields = [])
25
    {
26
        // do not allow request to define these fields:
27
        $data = $f3->get('REQUEST');
28
        foreach ($prohibitedFields as $field) {
29
            if (array_key_exists($field, $data)) {
30
                unset($data[$field]);
31
            }
32
        }
33
34
        // load pre-existing value
35
        $db = \Registry::get('db');
36
        $m = $this->getMapper();
37
38
        // copy data and validate
39
        $oldMapper = clone($m);
40
        $m->copyfrom($data);
41
        $m->validationRequired([
42
            'key', 'value', 'type', 'rank'
43
        ]);
44
45
        $errors = $m->validate(false);
46
        if (true !== $errors) {
47
            foreach ($errors as $error) {
48
                $this->setOAuthError('invalid_request');
49
                $this->failure($error['field'], $error['rule']);
50
            }
51
        } else {
52
            // load original record, ovewrite
53
            if ($f3->get('VERB') == 'PUT') {
54
                $m->load(['uuid = ?', $data['uuid']]);
55
            } else {
56
                $m->load([$db->quotekey('key') . ' = ?', $data['key']]);
57
            }
58
            $m->copyfrom($data);
59
60
            // load in original data and then replace for save
61
            if (!$m->validateSave()) {
62
                $this->setOAuthError('invalid_request');
63
                $this->failure('error', 'Unable to update object.');
64
                return;
65
            }
66
67
            $this->audit([
68
                'event' => 'Config Updated via API',
69
                'old' => $oldMapper->cast(),
70
                'new' => $m->cast()
71
            ]);
72
73
            // return raw data for object?
74
            $adminView = $f3->get('isAdmin') && 'admin' == $f3->get('REQUEST.view');
75
            $this->data = $adminView ? $m->castFields($f3->get('REQUEST.fields')) : $m->exportArray($f3->get('REQUEST.fields'));
76
        }
77
    }
78
79
80
    /**
81
     * Update data
82
     *
83
     * @param \Base $f3
84
     * @param array $params
85
     * @return null|array|boolean
86
     */
87
    public function patch(\Base $f3, array $params)
88
    {
89
        $m = $this->getIdObjectIfAdmin($f3, $params, 'uuid', $params['id']);
90
        if (!is_object($m) || null == $m->uuid) {
91
            return;
92
        }
93
94
        $f3->set('REQUEST.key', $m->key);
95
96
        // these fields can't be modified
97
        return $this->save($f3, [
98
            'id', 'uuid'
99
        ]);
100
    }
101
102
}
103