Completed
Pull Request — profile-edit-layout (#2313)
by
unknown
15:49 queued 07:54
created

start.php ➔ waiting_on_approval()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 1
dl 0
loc 50
rs 9.0909
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
    $address = array(
64
        'streetAddress' => copy_value_to_object_if_defined($profile_fields['streetaddress']),
65
        'city' => copy_value_to_object_if_defined($profile_fields['city']),
66
        'province' => copy_value_to_object_if_defined($profile_fields['province']),
67
        'postalCode' => copy_value_to_object_if_defined($profile_fields['postalcode']),
68
        'country' => copy_value_to_object_if_defined($profile_fields['country']),
69
    );
70
71
    $variables = array(
72
        '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...
73
        'data' => array(
74
            'name' => $profile_fields['name'],
75
            'titleEn' => copy_value_to_object_if_defined($profile_fields['job']),
76
            'titleFr' => copy_value_to_object_if_defined($profile_fields['jobfr']),
77
            'mobilePhone' => copy_value_to_object_if_defined($profile_fields['mobile']),
78
            'officePhone' => copy_value_to_object_if_defined($profile_fields['phone']),
79
            'address' => address_validation($address),
80
        )
81
    );
82
    
83
    // Send data to PaaS
84
    $response = $client->response($query, $variables, $headers);
85
86
    // Error check
87
    if($response->errors()){
88
        return false;
89
    }
90
91
    // Contact details
92
    $owner->name = $response->modifyProfile->name;
93
    $owner->job = $response->modifyProfile->titleEn;
94
    $owner->jobfr = $response->modifyProfile->titleFr;
95
    $owner->mobile = $response->modifyProfile->mobilePhone;
96
    $owner->phone = $response->modifyProfile->officePhone;
97
98
    // Address details
99
    $owner->streetaddress = $response->modifyProfile->address->streetAddress;
100
    $owner->city = $response->modifyProfile->address->city;
101
    $owner->province = $response->modifyProfile->address->province;
102
    $owner->postalcode = $response->modifyProfile->address->postalCode;
103
    $owner->country = $response->modifyProfile->address->country;
104
105
    $owner->save();
106
107
    // Mutation has gone through
108
    return true;
109
  }
110
111
  // Set value to null if not set
112
  function copy_value_to_object_if_defined($value){
113
    if($value != null && $value != "undefined"){
114
        return $value;
115
    } else {
116
        return null;
117
    }
118
  }
119
120
  // Set array to null if all fields are null
121
  function address_validation($address) {
122
123
    foreach($address as $k => $v ) {
124
        if($v != null) {
125
            return $address;
126
        }
127
    }
128
129
    return null;
130
  }
131
132
  function waiting_on_approval($guid){
133
134
    $dbprefix = elgg_get_config("dbprefix");
135
    $service_url = elgg_get_plugin_setting("graphql_client", "paas_integration");
136
137
    $result = get_data_row("SELECT pleio_guid FROM {$dbprefix}users_entity WHERE guid = $guid");
138
    if ($result->pleio_guid) {
139
        $gcID = $result->pleio_guid;
140
    }
141
142
    $client = new Client($service_url);
143
144
    $headers = [];
145
146
    $query = 'query getYourApproval($gcIDSubmitter: gcIDProfileInput!) {
147
        approvals(
148
          gcIDSubmitter: $gcIDSubmitter,
149
          status: Pending,
150
          changeType: Informational,
151
        ) {
152
            id
153
            requestedChange {
154
                titleEn
155
                titleFr
156
            }
157
        }
158
    }';
159
160
161
    $variables = array(
162
        'gcIDSubmitter' => array(
163
            '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...
164
        ),
165
    );
166
    
167
    // Send data to PaaS
168
    $response = $client->response($query, $variables, $headers);
169
170
    // Error check
171
    if($response->errors()){
172
        return null;
173
    } else if(!$response->approvals[0]->requestedChange) {
174
        return null;
175
    }
176
177
    return array(
178
        "titleEn" => $response->approvals[0]->requestedChange->titleEn,
179
        "titleFr" => $response->approvals[0]->requestedChange->titleFr
180
    );
181
  }