TrackLastMatch::down()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
crap 2
1
<?php
2
3
use Phinx\Migration\AbstractMigration;
4
5
class TrackLastMatch extends AbstractMigration
6
{
7 1
    public function up()
8
    {
9 1
        $playersTable = $this->table('players');
10
        $playersTable
11 1
            ->addColumn('last_match', 'integer', array(
12 1
                'signed' => false,
13
                'limit' => 10,
14
                'null' => true,
15
                'comment' => 'The timestamp of the last match this player participated in'
16
            ))
17 1
            ->addForeignKey('last_match', 'matches', 'id', array('delete' => 'SET_NULL'))
18 1
            ->update();
19
20 1
        $players = $this->fetchAll('SELECT id FROM players');
21
22 1
        foreach ($players as $player) {
23
            $this->query(
24
                "UPDATE players SET last_match = (
25
                    SELECT id FROM matches WHERE FIND_IN_SET(${player['id']}, team_a_players) OR FIND_IN_SET(${player['id']}, team_b_players) ORDER BY timestamp DESC LIMIT 1
26
                ) WHERE id = " . $player['id']
27
            );
28
        }
29 1
    }
30
31
    public function down()
32
    {
33
        $players = $this->table('players');
34
        $players
35
            ->dropForeignKey('last_match')
36
            ->removeColumn('last_match')
37
            ->update();
38
    }
39
}
40