Completed
Push — newinternal ( 65a0f5...5b021c )
by Simon
08:29
created

MigrateToRoles::execute()   B

Complexity

Conditions 7
Paths 17

Size

Total Lines 51

Duplication

Lines 7
Ratio 13.73 %

Importance

Changes 0
Metric Value
cc 7
nc 17
nop 0
dl 7
loc 51
rs 8.1357
c 0
b 0
f 0

How to fix   Long Method   

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:

1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\ConsoleTasks;
10
11
use PDO;
12
use Waca\DataObjects\Log;
13
use Waca\DataObjects\User;
14
use Waca\DataObjects\UserRole;
15
use Waca\Tasks\ConsoleTaskBase;
16
17
class MigrateToRoles extends ConsoleTaskBase
18
{
19
    public function execute()
20
    {
21
        $communityUser = User::getCommunity();
22
23
        $database = $this->getDatabase();
24
        $statement = $database->query('SELECT id, status, checkuser FROM user;');
25
        $update = $database->prepare("UPDATE user SET status = 'Active' WHERE id = :id;");
26
27
        $users = $statement->fetchAll(PDO::FETCH_ASSOC);
28
29
        foreach ($users as $user) {
30
            $toAdd = array('user');
31
32
            if($user['status'] === 'Admin'){
33
                $toAdd[] = 'admin';
34
            }
35
36
            if($user['checkuser'] == 1){
37
                $toAdd[] = 'checkuser';
38
            }
39
40 View Code Duplication
            foreach ($toAdd as $x) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
                $a = new UserRole();
42
                $a->setUser($user['id']);
43
                $a->setRole($x);
44
                $a->setDatabase($database);
45
                $a->save();
46
            }
47
48
            $logData = serialize(array(
49
                'added' => $toAdd,
50
                'removed' => array(),
51
                'reason' => 'Initial migration'
52
            ));
53
54
            $log = new Log();
55
            $log->setDatabase($database);
56
            $log->setAction('RoleChange');
57
            $log->setObjectId($user['id']);
58
            $log->setObjectType('User');
59
            $log->setUser($communityUser);
60
            $log->setComment($logData);
61
            $log->save();
62
63
            if($user['status'] === 'Admin' || $user['status'] === 'User'){
64
                $update->execute(array('id' => $user['id']));
65
            }
66
        }
67
68
        $database->exec("UPDATE schemaversion SET version = 25;");
69
    }
70
}
71