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

Reports::put()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
c 0
b 0
f 0
nc 2
nop 2
dl 15
loc 15
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 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
    private function save(\Base $f3, array $prohibitedFields = [])
0 ignored issues
show
Bug introduced by
Consider using a different method name as you override a private method of the parent class.

Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.

Loading history...
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
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
        $oldMapper = clone($m);
49
        $m->copyfrom($data);
50
        $m->validationRequired([
51
            'users_uuid', 'key', 'name', 'query'
52
        ]);
53
        $errors = $m->validate(false);
54
        if (true !== $errors) {
55
            foreach ($errors as $error) {
56
                $this->setOAuthError('invalid_request');
57
                $this->failure($error['field'], $error['rule']);
58
            }
59
        } else {
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
                'users_uuid' => $m->users_uuid,
69
                'actor' => $m->client_id,
70
                'event' => 'Report Updated via API',
71
                'old' => $oldMapper->cast(),
72
                'new' => $m->cast()
73
            ]);
74
75
            // return raw data for object?
76
            $adminView = $f3->get('isAdmin') && 'admin' == $f3->get('REQUEST.view');
77
            $this->data = $adminView ? $m->castFields($f3->get('REQUEST.fields')) : $m->exportArray($f3->get('REQUEST.fields'));
78
        }
79
    }
80
81
}
82