Passed
Push — master ( 668bf0...77aaf8 )
by Julito
11:36 queued 12s
created

updateUserFromUsername()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 43
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 26
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 43
rs 9.504
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
require_once __DIR__.'/../../../../vendor/autoload.php';
6
/**
7
 * Test example to user API v2.php.
8
 *
9
 * Using Guzzle' HTTP client to call the API endpoint and make requests.
10
 * Change URL on the first lines of createUser() below to suit your needs.
11
 */
12
13
use GuzzleHttp\Client as Client;
14
15
// set your URL, username and password here to use it for all webservices in this test file.
16
$webserviceURL = 'https://YOURCHAMILO/main/webservices/api/';
17
$webserviceUsername = 'USERNAME';
18
$webservicePassword = 'PASSWORD';
19
20
/**
21
 * Make a request to get the API key for admin user.
22
 *
23
 * @throws Exception
24
 *
25
 * @return string
26
 */
27
function authenticate()
28
{
29
    global $webserviceURL;
30
    global $webserviceUsername;
31
    global $webservicePassword;
32
    $client = new Client([
33
        'base_uri' => $webserviceURL,
34
    ]);
35
36
    $response = $client->post('v2.php', [
37
        'form_params' => [
38
            'action' => 'authenticate',
39
            'username' => $webserviceUsername,
40
            'password' => $webservicePassword,
41
        ],
42
    ]);
43
44
    if ($response->getStatusCode() !== 200) {
45
        throw new Exception('Entry denied with code : '.$response->getStatusCode());
46
    }
47
48
    $jsonResponse = json_decode($response->getBody()->getContents());
49
50
    if ($jsonResponse->error) {
51
        throw new Exception('Authentication failed because : '.$jsonResponse->message);
52
    }
53
54
    return $jsonResponse->data->apiKey;
55
}
56
57
/**
58
 * @param $apiKey
59
 *
60
 * @throws Exception
61
 *
62
 * @return int
63
 */
64
function updateUserFromUsername($apiKey)
65
{
66
    global $webserviceURL;
67
    global $webserviceUsername;
68
    $client = new Client([
69
        'base_uri' => $webserviceURL,
70
    ]);
71
72
    $response = $client->post('v2.php', [
73
        'form_params' => [
74
            // data for the user who makes the request
75
            'action' => 'update_user_from_username',
76
            'username' => $webserviceUsername,
77
            'api_key' => $apiKey,
78
            // data for the user to be updated
79
            'loginName' => 'TestUser',
80
            'firstname' => 'Test User',
81
            'lastname' => 'Chamilo',
82
            'status' => 5, // student
83
            'email' => '[email protected]',
84
            'enabled' => 1,
85
            'extra' => [
86
                [
87
                    'field_name' => 'age', // The "age" user extra field needs to already be created on Chamilo
88
                    'field_value' => 35,
89
                ],
90
            ],
91
            'language' => 'english',
92
            'expiration_date' => '2025-12-31 23:59:59',
93
        ],
94
    ]);
95
96
    if ($response->getStatusCode() !== 200) {
97
        throw new Exception('Entry denied with code : '.$response->getStatusCode());
98
    }
99
100
    $jsonResponse = json_decode($response->getBody()->getContents());
101
102
    if ($jsonResponse->error) {
103
        throw new Exception('User not updated because : '.$jsonResponse->message);
104
    }
105
106
    return $jsonResponse->data[0];
107
}
108
109
$apiKey = authenticate();
0 ignored issues
show
Bug introduced by
The call to authenticate() has too few arguments starting with WSUser. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

109
$apiKey = /** @scrutinizer ignore-call */ authenticate();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
110
111
//update user TestUser
112
if (updateUserFromUsername($apiKey)) {
113
    echo 'User updated successfully';
114
}
115