TrackLastMatch   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 56.25%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 35
ccs 9
cts 16
cp 0.5625
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A down() 0 8 1
A up() 0 23 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