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

Apps::appPost()   B

Complexity

Conditions 8
Paths 18

Size

Total Lines 60
Code Lines 38

Duplication

Lines 5
Ratio 8.33 %

Importance

Changes 0
Metric Value
cc 8
eloc 38
c 0
b 0
f 0
nc 18
nop 1
dl 5
loc 60
rs 7.0677

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace FFCMS\Controllers\User;
4
5
use FFMVC\Helpers;
6
use FFCMS\{Controllers, Models, Mappers, Traits};
7
8
/**
9
 * Use Apps Controller Class.
10
 *
11
 * OAuth2 WWW Handler
12
 *
13
 * @author Vijay Mahrra <[email protected]>
14
 * @copyright (c) Copyright 2016 Vijay Mahrra
15
 * @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html)
16
 */
17
class Apps extends Controllers\User\Base
18
{
19
    /**
20
     * User's own API applications
21
     *
22
     * @param \Base $f3
23
     * @return void
24
     */
25
    public function apps(\Base $f3)
26
    {
27
        $this->redirectLoggedOutUser();
28
        $this->csrf();
29
30
        $oAuth2Model = Models\OAuth2::instance();
31
32
        // fetch the user's apps
33
        $f3->set('apps', $oAuth2Model->getUserApps($f3->get('uuid')));
34
35
        $f3->set('breadcrumbs', [
36
            _('My Account') => 'user',
37
            _('Apps') => 'api_apps',
38
        ]);
39
40
        $f3->set('form', $f3->get('REQUEST'));
41
        echo \View::instance()->render('apps/apps.phtml');
42
    }
43
44
45
    /**
46
     * register app
47
     *
48
     * @param \Base $f3
49
     * @return void
50
     */
51
    public function appPost(\Base $f3)
52
    {
53
        $this->csrf('@api_apps');
54
        $this->redirectLoggedOutUser();
55
56
        $view = 'apps/apps.phtml';
57
        $oAuth2Model = Models\OAuth2::instance();
58
        $appsMapper = $oAuth2Model->getAppsMapper();
59
60
        // filter input vars of request, set back into REQUEST
61
        $appsMapper->copyfrom($f3->get('REQUEST'));
62
        $data = $appsMapper->filter();
63
        $request = $f3->get('REQUEST');
64
        foreach ($data as $k => $v) {
65
            if (array_key_exists($k, $request)) {
66
                $f3->set('REQUEST.' . $k, $v);
67
            }
68
        }
69
70
        // check app name exists
71
        $db = \Registry::get('db');
72
        $m = clone $appsMapper;
73
        if ($m->load(['LOWER('.$db->quotekey('name').') = LOWER(?)', $m->name]) && null !== $m->client_id) {
74
            $this->notify(_('An app with that name is already in use!'), 'warning');
75
            $f3->set('form', $f3->get('REQUEST'));
76
            echo \View::instance()->render($view);
77
            return;
78
        }
79
80
        // check required fields
81
        $appsMapper->validationRequired([
82
            'name',
83
            'description',
84
            'callback_uri',
85
        ]);
86
87
        // at this point the app can be validated
88
        if (true !== $appsMapper->validate()) {
89
            $this->notify(['info' => $appsMapper->validationErrors($appsMapper->validate(false))]);
90
            $f3->set('form', $f3->get('REQUEST'));
91
            echo \View::instance()->render($view);
92
            return;
93
        }
94
95
        $appsMapper->client_id = $appsMapper->setUUID('client_id');
96
        $appsMapper->client_secret = $appsMapper->setUUID('client_secret');
97
        $appsMapper->users_uuid = $f3->get('uuid');
98
99
        // admin group auto-approved
100
        $scopes = $f3->get('userScopes');
101
        $appsMapper->status = in_array('admin', $scopes) ? 'approved' : 'registered';
102
103 View Code Duplication
        if ($appsMapper->save()) {
104
            $this->notify(_('Your new app has been registered!'), 'success');
105
        } else {
106
            $this->notify(_('App registration failed!'), 'error');
107
        }
108
109
        $f3->reroute('@api_apps');
110
    }
111
112
113
    /**
114
     * register app
115
     *
116
     * @param \Base $f3
117
     * @return void
118
     */
119
    public function updateAppPost(\Base $f3)
120
    {
121
        $this->csrf('@user_api');
122
        $this->redirectLoggedOutUser();
123
124
        $oAuth2Model = Models\OAuth2::instance();
125
        $appsMapper = $oAuth2Model->getAppsMapper();
126
127
        // filter input vars of request, set back into REQUEST
128
        $appsMapper->copyfrom($f3->get('REQUEST'));
129
        $data = $appsMapper->filter();
130
        $request = $f3->get('REQUEST');
131
        foreach ($data as $k => $v) {
132
            if (array_key_exists($k, $request)) {
133
                $f3->set('REQUEST.' . $k, $v);
134
            }
135
        }
136
137
        // check app name exists
138 View Code Duplication
        if (!$appsMapper->load(['LOWER(client_id) = LOWER(?) AND users_uuid = ?',
139
                $request['client_id'], $f3->get('uuid')])) {
140
            $this->notify(_('The app does not exist!'), 'warning');
141
            $f3->reroute('@api_apps');
142
            return;
143
        }
144
145
        // check required fields
146
        $appsMapper->copyfrom($f3->get('REQUEST'));
147
        $appsMapper->validationRequired([
148
            'name',
149
            'description',
150
            'callback_uri',
151
        ]);
152
153
        // at this point the app can be validated
154 View Code Duplication
        if (true !== $appsMapper->validate()) {
155
            $this->notify(['info' => $appsMapper->validationErrors($appsMapper->validate(false))]);
156
            $f3->reroute('@api_apps');
157
        }
158
159 View Code Duplication
        if ($appsMapper->save()) {
160
            $this->notify(_('Your app has been updated!'), 'success');
161
        } else {
162
            $this->notify(_('App update failed!'), 'error');
163
        }
164
165
        $f3->reroute('@api_apps');
166
    }
167
}
168