submit_registration.php ➔ submit_registration()   C
last analyzed

Complexity

Conditions 8
Paths 30

Size

Total Lines 72
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 43
nc 30
nop 1
dl 0
loc 72
rs 6.3883
c 0
b 0
f 0

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
 * The PHP Skeleton App
4
 *
5
 * @author      Goran Halusa <[email protected]>
6
 * @copyright   2015 Goran Halusa
7
 * @link        https://github.com/ghalusa/PHP-Skeleton-App
8
 * @license     https://github.com/ghalusa/PHP-Skeleton-App/wiki/License
9
 * @version     0.1.1
10
 * @package     PHP Skeleton App
11
 *
12
 * For the full copyright and license information, please view the LICENSE
13
 * file that was distributed with this source code.
14
 */
15
16
/**
17
 * Submit Registration
18
 *
19
 * Controller for the Authenticate module.
20
 *
21
 * @author      Goran Halusa <[email protected]>
22
 * @since       0.1.0
23
 * @param       array  $route  The route data array
24
 */
25
 
26
function submit_registration(\Slim\Route $route)
27
{
28
    $app = \Slim\Slim::getInstance();
29
    $final_global_template_vars = $app->config('final_global_template_vars');
30
    
31
    require_once $final_global_template_vars["default_module_list"]["user_account"]["absolute_path_to_this_module"] . "/models/user_account.class.php";
32
    require_once $final_global_template_vars["default_module_list"]["group"]["absolute_path_to_this_module"] . "/models/group.class.php";
33
    require_once $_SERVER["PATH_TO_VENDOR"] . "wixel/gump/gump.class.php";
34
    $env = $app->environment();
35
    $db_conn = new \PHPSkeleton\models\db($final_global_template_vars["db_connection"]);
36
    $db_resource = $db_conn->get_resource();
37
    $user_account = new \PHPSkeleton\UserAccount($db_resource, $final_global_template_vars["session_key"]);
38
    $gump = new GUMP();
39
    $errors = array();
40
41
    $user_account_id = $_SESSION[$final_global_template_vars["session_key"]]["user_account_id"];
42
43
    // Check to see if this user is already assigned to a group - they may have been added by another administrator.
44
    $current_groups = $user_account->get_user_account_groups($user_account_id);
45
    if (!$current_groups) {
46
        // Validate the group that they submitted.
47
        $rules = array(
48
            "group" => "required|integer"
49
        );
50
        $validated = $gump->validate($app->request()->post(), $rules);
51
        if ($validated !== true) {
52
            $errors = \phpskeleton\models\utility::gump_parse_errors($validated);
53
        }
54
    }
55
56
    // Validate the acceptable use policy.
57
    $rules = array(
58
        "acceptable_use_policy" => "required|integer"
59
    );
60
    $validated = $gump->validate($app->request()->post(), $rules);
61
    if ($validated !== true) {
62
        $errors = array_merge($errors, \phpskeleton\models\utility::gump_parse_errors($validated));
63
    }
64
65
    if (!$errors) {
66
        // Create the actual user account.
67
        $user_data = array(
68
            "group_data" => '{"0":{"group_id":"' . $app->request()->post("group") . '","roles":["' . $final_global_template_vars["default_role_id"] . '"]}}'
69
        );
70
        $update_groups = (!empty($current_groups)) ? false : true;
71
        // Get the existing user account info.
72
        $existing_user_data = $user_account->get_user_account_info($user_account_id);
73
        // Merge the data.
74
        $user_data = array_merge($user_data, $existing_user_data);
75
        // Insert/update
76
        $user_account->insert_update_user_account($user_data, $user_account_id, $update_groups);
77
78
        // Update acceptable use policy.
79
        $user_account->update_acceptable_use_policy($user_account_id, 1);
80
81
        $landing_page = $final_global_template_vars['landing_page'];
82
        if (isset($_COOKIE[$final_global_template_vars["redirect_cookie_key"]]) && $_COOKIE[$final_global_template_vars["redirect_cookie_key"]]) {
83
            $landing_page = $_COOKIE[$final_global_template_vars["redirect_cookie_key"]];
84
            setcookie($final_global_template_vars["redirect_cookie_key"], "", time()-3600, "/");
85
            unset($_COOKIE[$final_global_template_vars["redirect_cookie_key"]]);
86
        }
87
88
        // Add role list to session.
89
        $_SESSION[$final_global_template_vars["session_key"]][$final_global_template_vars["current_user_roles_session_key"]] = \phpskeleton\models\utility::array_flatten($user_account->get_user_roles_list($user_account_id));
90
91
        // Add group to session.
92
        $_SESSION[$final_global_template_vars["session_key"]]["associated_groups"] = array((int)$app->request()->post("group"));
93
        $app->redirect($landing_page);
94
    } else {
95
        $env["default_validation_errors"] = $errors;
96
    }
97
}
98