Passed
Pull Request — profile-edit-layout (#2304)
by
unknown
10:38
created

start.php ➔ copy_value_to_object_if_defined()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
require_once __DIR__ . '/vendor/autoload.php';
3
use EUAutomation\GraphQL\Client;
4
5
elgg_register_event_handler('init', 'system', 'paas_integration_init');
6
7
function paas_integration_init() {
8
9
    elgg_register_action("avatar/upload", elgg_get_plugins_path() . "paas_integration/actions/avatar/upload.php");
10
    elgg_register_plugin_hook_handler("action", "b_extended_profile/edit_profile", "edit_profile_paas_mutation");
11
}
12
13
/**
14
 * Send modifyProfile mutation to PaaS
15
 *
16
 * @return bool
17
 */
18
function edit_profile_paas_mutation($hook, $entity_type, $returnvalue, $params) {
19
    $guid = get_input('guid');
20
    $owner = get_entity($guid);
21
22
    $dbprefix = elgg_get_config("dbprefix");
23
    $service_url = elgg_get_plugin_setting("graphql_client", "paas_integration");
24
    $dev_url = elgg_get_plugin_setting("dev_url", "paas_integration");
25
26
    $session = elgg_get_session();
27
    $token = $session->get('token');
28
29
    $result = get_data_row("SELECT pleio_guid FROM {$dbprefix}users_entity WHERE guid = $guid");
30
    if ($result->pleio_guid) {
31
        $gcID = $result->pleio_guid;
32
    }
33
34
    if($dev_url){
35
        $site_url = $dev_url;
36
    } else {
37
        $site_url = elgg_get_site_url();
38
    }
39
40
    $client = new Client($service_url);
41
42
    $headers = [
43
        "Authorization" => "Bearer $token"
44
    ];
45
46
    $query = 'mutation ($gcID: ID!, $data: ModifyProfileInput!) {
47
        modifyProfile(gcID: $gcID, data: $data) {
48
            gcID
49
            name
50
            email
51
            titleEn
52
            titleFr
53
            avatar
54
            mobilePhone
55
            officePhone
56
            address {
57
                streetAddress
58
                city
59
                postalCode
60
                province
61
                country
62
            }
63
        }
64
    }';
65
66
    $profile_fields = get_input('profile');
67
68
    $variables = array(
69
        'gcID' => $gcID,
0 ignored issues
show
Bug introduced by
The variable $gcID does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
70
        'data' => array(
71
            'name' => $profile_fields['name'],
72
            'titleEn' => copy_value_to_object_if_defined('job', $profile_fields['job']),
73
            'titleFr' => copy_value_to_object_if_defined('jobfr', $profile_fields['jobfr']),
74
            'mobilePhone' => copy_value_to_object_if_defined('mobile', $profile_fields['mobile']),
75
            'officePhone' => copy_value_to_object_if_defined('phone', $profile_fields['phone']),
76
            'address' => array(
77
                'streetAddress' => $profile_fields['streetaddress'],
78
                'city' => $profile_fields['city'],
79
                'province' => $profile_fields['province'],
80
                'postalCode' => $profile_fields['postalcode'],
81
                'country' => $profile_fields['country'],
82
            )
83
        )
84
    );
85
    
86
    // Send data to PaaS
87
    $response = $client->response($query, $variables, $headers);
88
89
    // Error check
90
    if($response->errors()){
91
        return false;
92
    }
93
94
    // Mutation has gone through
95
    return true;
96
  }
97
98
  function copy_value_to_object_if_defined($field, $value){
99
    if($value != null && $value != "undefined"){
100
        return $value;
101
    } else {
102
        return null;
103
    }
104
  }