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

Audit::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 Audit 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 Audit 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 = [])
1 ignored issue
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
25
    {
26
        // set audit user if not set
27
        $data = $f3->get('REQUEST');
28
        $user = $f3->get('user');
29
30
        // set uuid from user
31
        if (!array_key_exists('users_uuid', $data)) {
32
            $data['users_uuid'] = $user['uuid'];
33
        }
34
35
        // set actor from email
36
        if (!array_key_exists('actor', $data)) {
37
            $data['actor'] = $user['email'];
38
        }
39
40
        // do not allow request to define these fields:
41
        foreach ($prohibitedFields as $field) {
42
            if (array_key_exists($field, $data)) {
43
                unset($data[$field]);
44
            }
45
        }
46
47
        // load pre-existing value
48
        $m = $this->getMapper();
49
50
        // copy data and validate
51
        $oldMapper = clone($m);
52
        $m->copyfrom($data);
53
        $m->validationRequired([
54
            'users_uuid'
55
        ]);
56
57
        $errors = $m->validate(false);
58
        if (true !== $errors) {
59
            foreach ($errors as $error) {
60
                $this->setOAuthError('invalid_request');
61
                $this->failure($error['field'], $error['rule']);
62
            }
63
        } else {
64
            // load original record, ovewrite
65
            if (!empty($data['uuid'])) {
66
                $m->load(['uuid = ?', $data['uuid']]);
67
            }
68
            $m->copyfrom($data);
69
70
            // load in original data and then replace for save
71
            if (!$m->validateSave()) {
72
                $this->setOAuthError('invalid_request');
73
                $this->failure('error', 'Unable to update object.');
74
                return;
75
            }
76
77
            $this->audit([
78
                'event' => 'Audit Updated via API',
79
                'old' => $oldMapper->cast(),
80
                'new' => $m->cast()
81
            ]);
82
83
84
            // return raw data for object?
85
            $adminView = $f3->get('isAdmin') && 'admin' == $f3->get('REQUEST.view');
86
            $this->data = $adminView ? $m->castFields($f3->get('REQUEST.fields')) : $m->exportArray($f3->get('REQUEST.fields'));
87
        }
88
    }
89
90
}
91