Completed
Push — master ( 4a8d0f...5fbf19 )
by Konstantinos
04:38
created

Maps::updateMatches()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 20
rs 9.4286
cc 3
eloc 11
nc 4
nop 0
1
<?php
2
3
use Phinx\Db\Table\Column;
4
use Phinx\Migration\AbstractMigration;
5
6
class Maps extends AbstractMigration
7
{
8
    /**
9
     * Change Method.
10
     *
11
     * Write your reversible migrations using this method.
12
     *
13
     * More information on writing migrations is available here:
14
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
15
     *
16
     * The following commands can be used in this method and Phinx will
17
     * automatically reverse them when rolling back:
18
     *
19
     *    createTable
20
     *    renameTable
21
     *    addColumn
22
     *    renameColumn
23
     *    addIndex
24
     *    addForeignKey
25
     *
26
     * Remember to call "create()" or "update()" and NOT "save()" when working
27
     * with the Table class.
28
     */
29
    public function change()
30
    {
31
        $maps = $this->table('maps', array('id' => false, 'primary_key' => 'id'));
32
        $maps->addColumn('id', 'integer', array('limit' => 10, 'signed' => false, 'identity' => true))
33
            ->addColumn('name', 'string', array('limit' => 42, 'null' => false, 'comment' => 'The name of the map'))
34
            ->addColumn('alias', 'string', array('limit' => 42, 'null' => true, 'comment' => 'The alias of the map'))
35
            ->addColumn('avatar', 'string', array('limit' => 200, 'null' => true, 'comment' => 'The path to the map\'s image'))
36
            ->addColumn('description', 'text', array('null' => false, 'default' => '', 'comment' => 'A description of the map'))
37
            ->addColumn('status', 'set', array('values' => array('active', 'hidden', 'disabled', 'deleted'), 'null' => false,
38
                'default' => 'active', 'comment' => 'The status of the map'))
39
            ->addIndex('alias', array('unique' => true))
40
            ->create();
41
42
43
        $permissions = $this->table('permissions');
44
        $data = [
45
            [ 'name' => 'add_map',  'description' => 'The ability to add a map' ],
46
            [ 'name' => 'edit_map', 'description' => 'The ability to edit a map' ],
47
            [ 'name' => 'del_map',  'description' => 'The ability to mark a map as deleted' ],
48
            [ 'name' => 'wipe_map', 'description' => 'The ability to wipe a map from the database' ],
49
        ];
50
        $permissions->insert($data)->save();
51
52
        $ids = $this->getPermissionIDs();
53
        $rolePermissions = $this->table('role_permission');
54
        $data = [ // We use hardcoded role IDs because class constants might change
55
            [ 'role_id' => 1, 'perm_id' => $ids['add']], // Developers
56
            [ 'role_id' => 1, 'perm_id' => $ids['edit']],
57
            [ 'role_id' => 1, 'perm_id' => $ids['del']],
58
            [ 'role_id' => 1, 'perm_id' => $ids['wipe']],
59
60
            [ 'role_id' => 5, 'perm_id' => $ids['add']], // System Admins
61
            [ 'role_id' => 5, 'perm_id' => $ids['edit']],
62
            [ 'role_id' => 5, 'perm_id' => $ids['del']],
63
            [ 'role_id' => 5, 'perm_id' => $ids['wipe']],
64
        ];
65
        $rolePermissions->insert($data)->save();
66
67
68
69
        $matches = $this->table('matches');
70
        $matches
71
            ->addColumn('map', 'integer', array('limit' => 10, 'signed' => false, 'null' => true, 'comment' => 'The map that was played'))
72
            ->addForeignKey('map', 'maps', 'id', array('delete' => 'SET_NULL'))
73
            ->save();
74
75
        $this->updateMatches();
76
77
        // TODO: Uncomment this (commented to prevent loss of data in case of error)
78
        // $matches->removeColumn('map_played')
79
        //     ->save();
80
    }
81
82
    /**
83
     * Get the IDs of the newly entered permissions
84
     *
85
     * @return array
86
     */
87
    private function getPermissionIDs()
88
    {
89
        $return = array();
90
91
        foreach(array('add', 'edit', 'del', 'wipe') as $permission) {
92
            $row = $this->fetchRow("SELECT id FROM permissions WHERE name = '{$permission}_map'");
93
            $return[$permission] = $row['id'];
94
        }
95
96
        return $return;
97
    }
98
99
100
    private function updateMatches()
101
    {
102
        $matches = $this->table('matches');
0 ignored issues
show
Unused Code introduced by
$matches is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
103
        $maps = $this->table('maps');
104
105
        $insert = array();
106
107
        $storedMaps = $this->fetchAll("SELECT DISTINCT(map_played) AS map FROM matches WHERE map_played REGEXP '^[A-Za-z0-9]+$'");
108
        foreach($storedMaps as $map) {
109
            $insert[] = [ 'name' => $map[0], 'alias' => null, 'avatar' => null ];
110
        }
111
112
        $maps->insert($insert)->save();
113
114
        foreach($storedMaps as $map) {
115
            $name = $map[0];
116
            // $name contains only alphanumeric characters, meaning there can't be any MySQL injection
117
            $row = $this->fetchRow("SELECT id FROM maps WHERE name = '$name'");
0 ignored issues
show
Unused Code introduced by
$row is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
118
        }
119
    }
120
}
121
122