Completed
Pull Request — profile-edit-layout (#2304)
by
unknown
16:12 queued 07:53
created

start.php ➔ paas_integration_init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
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
    elgg_register_action('b_extended_profile/edit_profile', elgg_get_plugins_path() . 'paas_integration/actions/b_extended_profile/edit_profile.php');
13
}
14
15
/**
16
 * Send modifyProfile mutation to PaaS
17
 *
18
 * @return bool
19
 */
20
function edit_profile_paas_mutation($hook, $entity_type, $returnvalue, $params) {
21
    $guid = get_input('guid');
22
    $owner = get_entity($guid);
23
24
    $dbprefix = elgg_get_config("dbprefix");
25
    $service_url = elgg_get_plugin_setting("graphql_client", "paas_integration");
26
27
    $session = elgg_get_session();
28
    $token = $session->get('token');
29
30
    $result = get_data_row("SELECT pleio_guid FROM {$dbprefix}users_entity WHERE guid = $guid");
31
    if ($result->pleio_guid) {
32
        $gcID = $result->pleio_guid;
33
    }
34
35
    $client = new Client($service_url);
36
37
    $headers = [
38
        "Authorization" => "Bearer $token"
39
    ];
40
41
    $query = 'mutation ($gcID: ID!, $data: ModifyProfileInput!) {
42
        modifyProfile(gcID: $gcID, data: $data) {
43
            gcID
44
            name
45
            email
46
            titleEn
47
            titleFr
48
            avatar
49
            mobilePhone
50
            officePhone
51
            address {
52
                streetAddress
53
                city
54
                postalCode
55
                province
56
                country
57
            }
58
        }
59
    }';
60
61
    $profile_fields = get_input('profile');
62
63
    $variables = array(
64
        '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...
65
        'data' => array(
66
            'name' => $profile_fields['name'],
67
            'titleEn' => copy_value_to_object_if_defined('job', $profile_fields['job']),
68
            'titleFr' => copy_value_to_object_if_defined('jobfr', $profile_fields['jobfr']),
69
            'mobilePhone' => copy_value_to_object_if_defined('mobile', $profile_fields['mobile']),
70
            'officePhone' => copy_value_to_object_if_defined('phone', $profile_fields['phone']),
71
            'address' => array(
72
                'streetAddress' => copy_value_to_object_if_defined($profile_fields['streetaddress']),
0 ignored issues
show
Bug introduced by
The call to copy_value_to_object_if_defined() misses a required argument $value.

This check looks for function calls that miss required arguments.

Loading history...
73
                'city' => copy_value_to_object_if_defined($profile_fields['city']),
0 ignored issues
show
Bug introduced by
The call to copy_value_to_object_if_defined() misses a required argument $value.

This check looks for function calls that miss required arguments.

Loading history...
74
                'province' => copy_value_to_object_if_defined($profile_fields['province']),
0 ignored issues
show
Bug introduced by
The call to copy_value_to_object_if_defined() misses a required argument $value.

This check looks for function calls that miss required arguments.

Loading history...
75
                'postalCode' => copy_value_to_object_if_defined($profile_fields['postalcode']),
0 ignored issues
show
Bug introduced by
The call to copy_value_to_object_if_defined() misses a required argument $value.

This check looks for function calls that miss required arguments.

Loading history...
76
                'country' => copy_value_to_object_if_defined($profile_fields['country']),
0 ignored issues
show
Bug introduced by
The call to copy_value_to_object_if_defined() misses a required argument $value.

This check looks for function calls that miss required arguments.

Loading history...
77
            )
78
        )
79
    );
80
    
81
    // Send data to PaaS
82
    $response = $client->response($query, $variables, $headers);
83
84
    // Error check
85
    if($response->errors()){
86
        return false;
87
    }
88
89
    // Contact details
90
    $owner->name = $response->modifyProfile->name;
91
    $owner->job = $response->modifyProfile->titleEn;
92
    $owner->jobfr = $response->modifyProfile->titleFr;
93
    $owner->mobile = $response->modifyProfile->mobilePhone;
94
    $owner->phone = $response->modifyProfile->officePhone;
95
96
    // Address details
97
    $owner->streetaddress = $response->modifyProfile->address->streetAddress;
98
    $owner->city = $response->modifyProfile->address->city;
99
    $owner->province = $response->modifyProfile->address->province;
100
    $owner->postalcode = $response->modifyProfile->address->postalCode;
101
    $owner->country = $response->modifyProfile->address->country;
102
103
    $owner->save();
104
105
    // Mutation has gone through
106
    return true;
107
  }
108
109
  function copy_value_to_object_if_defined($field, $value){
110
    if($value != null && $value != "undefined"){
111
        return $value;
112
    } else {
113
        return null;
114
    }
115
  }
116