Test Setup Failed
Push — master ( ec638a...cb9435 )
by Julito
51:10
created

WSUser::editUserHelper()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 58
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 45
nc 9
nop 13
dl 0
loc 58
rs 8.7274
c 0
b 0
f 0

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/* For licensing terms, see /license.txt */
3
/**
4
 * @package chamilo.webservices
5
 */
6
require_once __DIR__.'/../inc/global.inc.php';
7
require_once __DIR__.'/webservice.php';
8
9
/**
10
 * Web services available for the User module. This class extends the WS class
11
 */
12
class WSUser extends WS
13
{
14
    /**
15
     * Enables or disables a user
16
     *
17
     * @param string User id field name
18
     * @param string User id value
19
     * @param int Set to 1 to enable and to 0 to disable
20
     */
21
    protected function changeUserActiveState(
22
        $user_id_field_name,
23
        $user_id_value,
24
        $state
25
    ) {
26
        $user_id = $this->getUserId($user_id_field_name, $user_id_value);
27
        if ($user_id instanceof WSError) {
28
            return $user_id;
29 View Code Duplication
        } else {
30
            if ($state == 0) {
31
                UserManager::disable($user_id);
32
            } else {
33
                if ($state == 1) {
34
                    UserManager::enable($user_id);
35
                }
36
            }
37
        }
38
    }
39
40
    /**
41
     * Enables or disables multiple users
42
     *
43
     * @param array Users
44
     * @param int Set to 1 to enable and to 0 to disable
45
     * @return array Array of results
46
     */
47
    protected function changeUsersActiveState($users, $state)
48
    {
49
        $results = array();
50
        foreach ($users as $user) {
51
            $result_tmp = array();
52
            $result_op = $this->changeUserActiveState(
53
                $user['user_id_field_name'],
54
                $user['user_id_value'],
55
                $state
56
            );
57
            $result_tmp['user_id_value'] = $user['user_id_value'];
58
            if ($result_op instanceof WSError) {
59
                // Return the error in the results
60
                $result_tmp['result'] = $result_op->toArray();
61
            } else {
62
                $result_tmp['result'] = $this->getSuccessfulResult();
63
            }
64
            $results[] = $result_tmp;
65
        }
66
67
        return $results;
68
    }
69
70
    /**
71
     * Disables a user
72
     *
73
     * @param string API secret key
74
     * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
75
     * @param string User id value
76
     */
77
    public function DisableUser(
78
        $secret_key,
79
        $user_id_field_name,
80
        $user_id_value
81
    ) {
82
        $verifKey = $this->verifyKey($secret_key);
83
        if ($verifKey instanceof WSError) {
84
            // Let the implementation handle it
85
            $this->handleError($verifKey);
86
        } else {
87
            $result = $this->changeUserActiveState(
88
                $user_id_field_name,
89
                $user_id_value,
90
                0
91
            );
92
            if ($result instanceof WSError) {
93
                $this->handleError($result);
94
            }
95
        }
96
    }
97
98
    /**
99
     * Disables multiple users
100
     *
101
     * @param string API secret key
102
     * @param array Array of users with elements of the form array('user_id_field_name' => 'name_of_field', 'user_id_value' => 'value')
103
     * @return array Array with elements like array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). Note that if the result array contains a code different
104
     * than 0, an error occured
105
     */
106 View Code Duplication
    public function DisableUsers($secret_key, $users)
107
    {
108
        $verifKey = $this->verifyKey($secret_key);
109
        if ($verifKey instanceof WSError) {
110
            // Let the implementation handle it
111
            $this->handleError($verifKey);
112
        } else {
113
            return $this->changeUsersActiveState($users, 0);
114
        }
115
    }
116
117
    /**
118
     * Enables a user
119
     *
120
     * @param string API secret key
121
     * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
122
     * @param string User id value
123
     */
124
    public function EnableUser($secret_key, $user_id_field_name, $user_id_value)
125
    {
126
        $verifKey = $this->verifyKey($secret_key);
127
        if ($verifKey instanceof WSError) {
128
            $this->handleError($verifKey);
129
        } else {
130
            $result = $this->changeUserActiveState(
131
                $user_id_field_name,
132
                $user_id_value,
133
                1
134
            );
135
            if ($result instanceof WSError) {
136
                $this->handleError($result);
137
            }
138
        }
139
    }
140
141
    /**
142
     * Enables multiple users
143
     *
144
     * @param string API secret key
145
     * @param array Array of users with elements of the form array('user_id_field_name' => 'name_of_field', 'user_id_value' => 'value')
146
     * @return array Array with elements like array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). Note that if the result array contains a code different
147
     * than 0, an error occured
148
     */
149 View Code Duplication
    public function EnableUsers($secret_key, $users)
150
    {
151
        $verifKey = $this->verifyKey($secret_key);
152
        if ($verifKey instanceof WSError) {
153
            // Let the implementation handle it
154
            $this->handleError($verifKey);
155
        } else {
156
            return $this->changeUsersActiveState($users, 1);
157
        }
158
    }
159
160
    /**
161
     * Deletes a user (helper method)
162
     *
163
     * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
164
     * @param string User id value
165
     * @return mixed True if user was successfully deleted, WSError otherwise
166
     */
167
    protected function deleteUserHelper($user_id_field_name, $user_id_value)
168
    {
169
        $user_id = $this->getUserId($user_id_field_name, $user_id_value);
170
        if ($user_id instanceof WSError) {
171
            return $user_id;
172
        } else {
173
            if (!UserManager::delete_user($user_id)) {
174
                return new WSError(
175
                    101,
176
                    "There was a problem while deleting this user"
177
                );
178
            } else {
179
                return true;
180
            }
181
        }
182
    }
183
184
    /**
185
     * Deletes a user
186
     *
187
     * @param string API secret key
188
     * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
189
     * @param string User id value
190
     */
191
    public function DeleteUser($secret_key, $user_id_field_name, $user_id_value)
192
    {
193
        $verifKey = $this->verifyKey($secret_key);
194
        if ($verifKey instanceof WSError) {
195
            $this->handleError($verifKey);
196
        } else {
197
            $result = $this->deleteUserHelper(
198
                $user_id_field_name,
199
                $user_id_value
200
            );
201
            if ($result instanceof WSError) {
202
                $this->handleError($result);
203
            }
204
        }
205
    }
206
207
    /**
208
     * Deletes multiple users
209
     *
210
     * @param string API secret key
211
     * @param array Array of users with elements of the form array('user_id_field_name' => 'name_of_field', 'user_id_value' => 'value')
212
     * @return array Array with elements like array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). Note that if the result array contains a code different
213
     * than 0, an error occured
214
     */
215 View Code Duplication
    public function DeleteUsers($secret_key, $users)
216
    {
217
        $verifKey = $this->verifyKey($secret_key);
218
        if ($verifKey instanceof WSError) {
219
            $this->handleError($verifKey);
220
        } else {
221
            $results = array();
222
            foreach ($users as $user) {
223
                $result_tmp = array();
224
                $result_op = $this->deleteUserHelper(
225
                    $user['user_id_field_name'],
226
                    $user['user_id_value']
227
                );
228
                $result_tmp['user_id_value'] = $user['user_id_value'];
229
                if ($result_op instanceof WSError) {
230
                    // Return the error in the results
231
                    $result_tmp['result'] = $result_op->toArray();
232
                } else {
233
                    $result_tmp['result'] = $this->getSuccessfulResult();
234
                }
235
                $results[] = $result_tmp;
236
            }
237
238
            return $results;
239
        }
240
    }
241
242
    /**
243
     * Creates a user (helper method)
244
     *
245
     * @param string User first name
246
     * @param string User last name
247
     * @param int User status
248
     * @param string Login name
249
     * @param string Password (encrypted or not)
250
     * @param string Encrypt method. Leave blank if you are passing the password in clear text, set to the encrypt method used to encrypt the password otherwise. Remember
251
     * to include the salt in the extra fields if you are encrypting the password
252
     * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
253
     * @param string User id value. Leave blank if you are using the internal user_id
254
     * @param int Visibility.
255
     * @param string User email.
256
     * @param string Language.
257
     * @param string Phone.
258
     * @param string Expiration date
259
     * @param array Extra fields. An array with elements of the form ('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field').
260
     * @return mixed New user id generated by the system, WSError otherwise
261
     */
262
    protected function createUserHelper(
263
        $firstname,
264
        $lastname,
265
        $status,
266
        $login,
267
        $password,
268
        $encrypt_method,
269
        $user_id_field_name,
270
        $user_id_value,
271
        $visibility,
272
        $email,
273
        $language,
274
        $phone,
275
        $expiration_date,
276
        $extras = array()
277
    ) {
278
279
        // Add the original user id field name and value to the extra fields if needed
280
        $extras_associative = array();
281
        if ($user_id_field_name != "chamilo_user_id") {
282
            $extras_associative[$user_id_field_name] = $user_id_value;
283
        }
284 View Code Duplication
        if (!empty($extras)) {
285
            foreach ($extras as $extra) {
286
                $extras_associative[$extra['field_name']] = $extra['field_value'];
287
            }
288
        }
289
        $result = UserManager::create_user(
290
            $firstname,
291
            $lastname,
292
            $status,
293
            $email,
294
            $login,
295
            $password,
296
            '',
297
            $language,
298
            $phone,
299
            '',
300
            PLATFORM_AUTH_SOURCE,
301
            $expiration_date,
302
            $visibility,
303
            0,
304
            $extras_associative,
305
            $encrypt_method
306
        );
307
        if (!$result) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type false|integer is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
308
            return new WSError(104, 'There was an error creating the user');
309
310
            /*$failure = $api_failureList[0];
311
            if($failure == 'login-pass already taken') {
312
                return new WSError(102, 'This username is already taken');
313
            } else if($failure == 'encrypt_method invalid') {
314
                return new WSError(103, 'The encryption of the password is invalid');
315
            } else {
316
                return new WSError(104, 'There was an error creating the user');
317
            }*/
318
        } else {
319
            return $result;
320
        }
321
    }
322
323
    /**
324
     * Creates a user
325
     *
326
     * @param string API secret key
327
     * @param string User first name
328
     * @param string User last name
329
     * @param int User status
330
     * @param string Login name
331
     * @param string Password (encrypted or not)
332
     * @param string Encrypt method. Leave blank if you are passing the password in clear text, set to the encrypt method used to encrypt the password otherwise. Remember
333
     * to include the salt in the extra fields if you are encrypting the password
334
     * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
335
     * @param string User id value. Leave blank if you are using the internal user_id
336
     * @param int Visibility. Set by default to 1
337
     * @param string User email. Set by default to an empty string
338
     * @param string Language. Set by default to english
339
     * @param string Phone. Set by default to an empty string
340
     * @param string Expiration date. Set to null by default
341
     * @param array Extra fields. An array with elements of the form ('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field'). Set to an empty array by default
342
     * @return int New user id generated by the system
343
     */
344
    public function CreateUser(
345
        $secret_key,
346
        $firstname,
347
        $lastname,
348
        $status,
349
        $login,
350
        $password,
351
        $encrypt_method,
352
        $user_id_field_name,
353
        $user_id_value,
354
        $visibility = 1,
355
        $email = '',
356
        $language = 'english',
357
        $phone = '',
358
        $expiration_date = '0000-00-00 00:00:00',
359
        $extras = array()
360
    ) {
361
        // First, verify the secret key
362
        $verifKey = $this->verifyKey($secret_key);
363
        if ($verifKey instanceof WSError) {
364
            $this->handleError($verifKey);
365
        } else {
366
            $result = $this->createUserHelper(
367
                $firstname,
368
                $lastname,
369
                $status,
370
                $login,
371
                $password,
372
                $encrypt_method,
373
                $user_id_field_name,
374
                $user_id_value,
375
                $visibility,
376
                $email,
377
                $language,
378
                $phone,
379
                $expiration_date,
380
                $extras
381
            );
382
            if ($result instanceof WSError) {
383
                $this->handleError($result);
384
            } else {
385
                return $result;
386
            }
387
        }
388
    }
389
390
    /**
391
     * Creates multiple users
392
     *
393
     * @param string API secret key
394
     * @param array Users array. Each member of this array must follow the structure imposed by the CreateUser method
395
     * @return array Array with elements of the form array('user_id_value' => 'original value sent', 'user_id_generated' => 'value_generated', 'result' => array('code' => 0, 'message' => 'Operation was successful'))
396
     */
397
    public function CreateUsers($secret_key, $users)
398
    {
399
        $verifKey = $this->verifyKey($secret_key);
400
        if ($verifKey instanceof WSError) {
401
            $this->handleError($verifKey);
402
        } else {
403
            $results = array();
404
            foreach ($users as $user) {
405
                $result_tmp = array();
406
                // re-initialize variables just in case
407
                $firstname = $lastname = $status = $login = $password = $encrypt_method = $user_id_field_name = $user_id_value = $visibility = $email = $language = $phone = $expiration_date = $extras = null;
408
                extract($user);
409
                $result = $this->createUserHelper(
410
                    $firstname,
411
                    $lastname,
412
                    $status,
413
                    $login,
414
                    $password,
415
                    $encrypt_method,
416
                    $user_id_field_name,
417
                    $user_id_value,
418
                    $visibility,
419
                    $email,
420
                    $language,
421
                    $phone,
422
                    $expiration_date,
423
                    $extras
424
                );
425
                if ($result instanceof WSError) {
426
                    $result_tmp['result'] = $result->toArray();
427
                    $result_tmp['user_id_value'] = $user_id_value;
428
                    $result_tmp['user_id_generated'] = 0;
429
                } else {
430
                    $result_tmp['result'] = $this->getSuccessfulResult();
431
                    $result_tmp['user_id_value'] = $user_id_value;
432
                    $result_tmp['user_id_generated'] = $result;
433
                }
434
                $results[] = $result_tmp;
435
            }
436
437
            return $results;
438
        }
439
    }
440
441
    /**
442
     * Edits user info (helper method)
443
     *
444
     * @param string User id field name. Use "chamilo_user_id" in order to use internal system id
445
     * @param string User id value
446
     * @param string First name
447
     * @param string Last name
448
     * @param int User status
449
     * @param string Login name
450
     * @param string Password. Leave blank if you don't want to update it
451
     * @param string Encrypt method
452
     * @param string User email
453
     * @param string Language. Set by default to english
454
     * @param string Phone. Set by default to an empty string
455
     * @param string Expiration date. Set to null by default
456
     * @param array Extra fields. An array with elements of the form ('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field'). Leave empty if you don't want to update
457
     * @return mixed True if user was successfully updated, WSError otherwise
458
     */
459
    protected function editUserHelper(
460
        $user_id_field_name,
461
        $user_id_value,
462
        $firstname,
463
        $lastname,
464
        $status,
465
        $loginname,
466
        $password,
467
        $encrypt_method,
468
        $email,
469
        $language,
470
        $phone,
471
        $expiration_date,
472
        $extras
473
    ) {
474
        $user_id = $this->getUserId($user_id_field_name, $user_id_value);
475
        if ($user_id instanceof WSError) {
476
            return $user_id;
477
        } else {
478
            if ($password == '') {
479
                $password = null;
480
            }
481
            $user_info = api_get_user_info($user_id);
482
            if (count($extras) == 0) {
483
                $extras = null;
484
            }
485
486
            $result = UserManager::update_user(
487
                $user_id,
488
                $firstname,
489
                $lastname,
490
                $loginname,
491
                $password,
492
                PLATFORM_AUTH_SOURCE,
493
                $email,
494
                $status,
495
                '',
496
                $phone,
497
                $user_info['picture_uri'],
498
                $expiration_date,
499
                $user_info['active'],
500
                null,
501
                $user_info['hr_dept_id'],
502
                $extras,
503
                $encrypt_method
504
            );
505
            if (!$result) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type false|integer is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
506
                /*if($failure == 'encrypt_method invalid') {
507
                    return new WSError(103, 'The encryption of the password is invalid');
508
                } else {
509
                    return new WSError(105, 'There was an error updating the user');
510
                }*/
511
                return new WSError(105, 'There was an error updating the user');
512
            } else {
513
                return $result;
514
            }
515
        }
516
    }
517
518
    /**
519
     * Edits user info
520
     *
521
     * @param string API secret key
522
     * @param string User id field name. Use "chamilo_user_id" in order to use internal system id
523
     * @param string User id value
524
     * @param string First name
525
     * @param string Last name
526
     * @param int User status
527
     * @param string Login name
528
     * @param string Password. Leave blank if you don't want to update it
529
     * @param string Encrypt method
530
     * @param string User email
531
     * @param string Language. Set by default to english
532
     * @param string Phone. Set by default to an empty string
533
     * @param string Expiration date. Set to null by default
534
     * @param array Extra fields. An array with elements of the form ('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field'). Leave empty if you don't want to update
535
     */
536
    public function EditUser(
537
        $secret_key,
538
        $user_id_field_name,
539
        $user_id_value,
540
        $firstname,
541
        $lastname,
542
        $status,
543
        $loginname,
544
        $password,
545
        $encrypt_method,
546
        $email,
547
        $language,
548
        $phone,
549
        $expiration_date,
550
        $extras
551
    ) {
552
        // First, verify the secret key
553
        $verifKey = $this->verifyKey($secret_key);
554
        if ($verifKey instanceof WSError) {
555
            $this->handleError($verifKey);
556
        } else {
557
            $extras_associative = array();
558 View Code Duplication
            if (!empty($extras)) {
559
                foreach ($extras as $extra) {
560
                    $extras_associative[$extra['field_name']] = $extra['field_value'];
561
                }
562
            }
563
564
            $result = $this->editUserHelper(
565
                $user_id_field_name,
566
                $user_id_value,
567
                $firstname,
568
                $lastname,
569
                $status,
570
                $loginname,
571
                $password,
572
                $encrypt_method,
573
                $email,
574
                $language,
575
                $phone,
576
                $expiration_date,
577
                $extras_associative
578
            );
579
            if ($result instanceof WSError) {
580
                $this->handleError($result);
581
            }
582
        }
583
    }
584
585
    /**
586
     * Edits multiple users
587
     *
588
     * @param string API secret key
589
     * @param array Users array. Each member of this array must follow the structure imposed by the EditUser method
590
     * @return array Array with elements like array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). Note that if the result array contains a code different
591
     * than 0, an error occured
592
     */
593
    public function EditUsers($secret_key, $users)
594
    {
595
        $verifKey = $this->verifyKey($secret_key);
596
        if ($verifKey instanceof WSError) {
597
            $this->handleError($verifKey);
598
        } else {
599
            $results = array();
600
            foreach ($users as $user) {
601
                $result_tmp = array();
602
                // re-initialize variables just in case
603
                $user_id_field_name = $user_id_value = $firstname = $lastname = $status = $loginname = $password = $encrypt_method = $email = $language = $phone = $expiration_date = $extras = null;
604
                extract($user);
605
                $result_op = $this->editUserHelper(
606
                    $user_id_field_name,
607
                    $user_id_value,
608
                    $firstname,
609
                    $lastname,
610
                    $status,
611
                    $loginname,
612
                    $password,
613
                    $encrypt_method,
614
                    $email,
615
                    $language,
616
                    $phone,
617
                    $expiration_date,
618
                    $extras
619
                );
620
                $result_tmp['user_id_value'] = $user['user_id_value'];
621
                if ($result_op instanceof WSError) {
622
                    // Return the error in the results
623
                    $result_tmp['result'] = $result_op->toArray();
624
                } else {
625
                    $result_tmp['result'] = $this->getSuccessfulResult();
626
                }
627
                $results[] = $result_tmp;
628
            }
629
630
            return $results;
631
        }
632
    }
633
}
634