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

getUserMessageRead()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 37
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 18
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 37
rs 9.6666
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
 * Make a request to get the API key for admin user.
21
 *
22
 * @throws Exception
23
 *
24
 * @return string
25
 */
26
function authenticate()
27
{
28
    global $webserviceURL;
29
    global $webserviceUsername;
30
    global $webservicePassword;
31
    $client = new Client([
32
        'base_uri' => $webserviceURL,
33
    ]);
34
35
    $response = $client->post('v2.php', [
36
        'form_params' => [
37
            'action' => 'authenticate',
38
            'username' => $webserviceUsername,
39
            'password' => $webservicePassword,
40
        ],
41
    ]);
42
43
    if ($response->getStatusCode() !== 200) {
44
        throw new Exception('Entry denied with code : '.$response->getStatusCode());
45
    }
46
47
    $jsonResponse = json_decode($response->getBody()->getContents());
48
49
    if ($jsonResponse->error) {
50
        throw new Exception('Authentication failed because : '.$jsonResponse->message);
51
    }
52
53
    return $jsonResponse->data->apiKey;
54
}
55
56
/**
57
 * @param $apiKey
58
 *
59
 * @throws Exception
60
 *
61
 * @return int
62
 */
63
function getUserMessageRead($apiKey)
64
{
65
    global $webserviceURL;
66
    global $webserviceUsername;
67
    $client = new Client([
68
        'base_uri' => $webserviceURL,
69
    ]);
70
71
    $response = $client->post(
72
        'v2.php',
73
        [
74
            'form_params' => [
75
                // data for the user who makes the request
76
                'action' => 'user_message_read',
77
                'username' => $webserviceUsername,
78
                'api_key' => $apiKey,
79
                'messages' => [
80
                    1,
81
                    2,
82
                    3,
83
                ],
84
            ],
85
        ]
86
    );
87
88
    if ($response->getStatusCode() !== 200) {
89
        throw new Exception('Entry denied with code : '.$response->getStatusCode());
90
    }
91
92
    $content = $response->getBody()->getContents();
93
    $jsonResponse = json_decode($content, true);
94
95
    if ($jsonResponse['error']) {
96
        throw new Exception('Cant get read messages because : '.$jsonResponse['message']);
97
    }
98
99
    return $jsonResponse['data'];
100
}
101
102
$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

102
$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...
103
104
//Mark a specific message (in the social network interface) as read.
105
$userMessages = getUserMessageRead($apiKey);
106
echo json_encode($userMessages);
107