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

OAuth2Apps::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 OAuth2Apps 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 OAuth2Apps extends Mapper
16
{
17
    protected $table = 'oauth2_apps';
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
    private function save(\Base $f3, array $prohibitedFields = [])
28
    {
29
        // set audit user if not set
30
        $data = $f3->get('REQUEST');
31
        $user = $f3->get('user');
32
33
        if (!array_key_exists('users_uuid', $data)) {
34
            $data['users_uuid'] = $user['uuid'];
35
        }
36
37
        if (!array_key_exists('status', $data)) {
38
            $data['status'] = 'approved';
39
        }
40
41
        if (!array_key_exists('client_id', $data)) {
42
            $data['client_id'] = Helpers\Str::uuid();
43
        }
44
45
        if (!array_key_exists('client_secret', $data)) {
46
            $data['client_secret'] = Helpers\Str::uuid();
47
        }
48
49
        // do not allow request to define these fields:
50
        foreach ($prohibitedFields as $field) {
51
            if (array_key_exists($field, $data)) {
52
                unset($data[$field]);
53
            }
54
        }
55
56
        // load pre-existing value
57
        $m = $this->getMapper();
58
59
        // copy data and validate
60
        $oldMapper = clone($m);
61
        $m->copyfrom($data);
62
        $m->validationRequired([
63
            'users_uuid', 'name'
64
        ]);
65
66
        $errors = $m->validate(false);
67 View Code Duplication
        if (true !== $errors) {
68
            foreach ($errors as $error) {
69
                $this->setOAuthError('invalid_request');
70
                $this->failure($error['field'], $error['rule']);
71
            }
72
        } else {
73
            // load original record, ovewrite
74
            if (!empty($data['uuid'])) {
75
                $m->load(['uuid = ?', $data['uuid']]);
76
            }
77
            $m->copyfrom($data);
78
79
            // load in original data and then replace for save
80
            if (!$m->validateSave()) {
81
                $this->setOAuthError('invalid_request');
82
                $this->failure('error', 'Unable to update object.');
83
                return;
84
            }
85
86
            $this->audit([
87
                'users_uuid' => $m->users_uuid,
88
                'actor' => $m->client_id,
89
                'event' => ' App Updated via API',
90
                'old' => $oldMapper->cast(),
91
                'new' => $m->cast()
92
            ]);
93
94
            // return raw data for object?
95
            $adminView = $f3->get('isAdmin') && 'admin' == $f3->get('REQUEST.view');
96
            $this->data = $adminView ? $m->castFields($f3->get('REQUEST.fields')) : $m->exportArray($f3->get('REQUEST.fields'));
97
        }
98
    }
99
100
101
    /**
102
     * Update data
103
     *
104
     * @param \Base $f3
105
     * @param array $params
106
     * @return null|array|boolean
107
     */
108
    public function patch(\Base $f3, array $params)
109
    {
110
        $m = $this->getIdObjectIfAdmin($f3, $params, 'client_id', $params['id']);
111
        if (!is_object($m) || null == $m->client_id) {
112
            return;
113
        }
114
115
        $f3->set('REQUEST.client_id', $m->client_id);
116
        $f3->set('REQUEST.client_secret', $m->client_secret);
117
118
        // these fields can't be modified
119
        return $this->save($f3, [
120
            'id'
121
        ]);
122
    }
123
124
125
    /**
126
     * Replace data
127
     *
128
     * @param \Base $f3
129
     * @param array $params
130
     * @return null|array|boolean
131
     */
132
    public function put(\Base $f3, array $params)
133
    {
134
        $m = $this->getIdObjectIfAdmin($f3, $params, 'client_id', $params['id']);
135
        if (!is_object($m) || null == $m->cilent_id) {
136
            return;
137
        }
138
139
        return $this->save($f3, [
140
            'id'
141
        ]);
142
    }
143
144
}
145