1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file contains a database migration |
4
|
|
|
* |
5
|
|
|
* @license https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
use BZIon\Migration\AbstractMigration; |
9
|
|
|
use BZIon\Migration\Delete; |
10
|
|
|
|
11
|
|
|
class GroupTeamEvents extends AbstractMigration |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Migration Method |
15
|
|
|
* |
16
|
|
|
* Changes in events: |
17
|
|
|
* - GroupJoinEvent can now also store teams |
18
|
|
|
* - GroupAbandonEvent and GroupKickEvent had the typehint of \Player |
19
|
|
|
* converted to \Model to support both players and teams leaving groups; |
20
|
|
|
* it is now necessary to store the type of the model in the appropriate |
21
|
|
|
* format |
22
|
|
|
*/ |
23
|
1 |
|
public function up() |
24
|
|
|
{ |
25
|
|
|
// Teams can now join groups |
26
|
|
|
$this->editGroupEvent('group.join', function (&$data) { |
27
|
|
|
if (!isset($data['teams'])) { |
28
|
|
|
$data['teams'] = array(); |
29
|
|
|
} |
30
|
1 |
|
}); |
31
|
|
|
|
32
|
|
|
// Teams can now get kicked from groups |
33
|
|
|
$this->editGroupEvent('group.kick', function (&$data) { |
34
|
|
|
$data['kicked'] = array( |
35
|
|
|
'id' => $data['kicked'], |
36
|
|
|
'type' => 'Player' |
37
|
|
|
); |
38
|
1 |
|
}); |
39
|
|
|
|
40
|
|
|
// Teams can now abandon groups |
41
|
|
|
$this->editGroupEvent('group.abandon', function (&$data) { |
42
|
|
|
$data['member'] = array( |
43
|
|
|
'id' => $data['player'], |
44
|
|
|
'type' => 'Player' |
45
|
|
|
); |
46
|
|
|
unset($data['player']); |
47
|
1 |
|
}); |
48
|
1 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Rollback Method |
52
|
|
|
*/ |
53
|
|
|
public function down() |
54
|
|
|
{ |
55
|
|
|
// No need to rollback the group.join event, team information can stay |
56
|
|
|
|
57
|
|
|
// Teams can't be kicked from groups |
58
|
|
View Code Duplication |
$this->editGroupEvent('group.kick', function (&$data) { |
|
|
|
|
59
|
|
|
if ($data['kicked']['type'] === 'Player') { |
60
|
|
|
$data['kicked'] = $data['kicked']['id']; |
61
|
|
|
} else { |
62
|
|
|
// Delete events where a team gets kicked |
63
|
|
|
return new Delete(); |
64
|
|
|
} |
65
|
|
|
}); |
66
|
|
|
|
67
|
|
|
// Teams can't abandon groups |
68
|
|
View Code Duplication |
$this->editGroupEvent('group.abandon', function (&$data) { |
|
|
|
|
69
|
|
|
if ($data['member']['type'] === 'Player') { |
70
|
|
|
$data['player'] = $data['member']['id']; |
71
|
|
|
unset($data['member']); |
72
|
|
|
} else { |
73
|
|
|
// Delete events where a team abandons the group |
74
|
|
|
return new Delete(); |
75
|
|
|
} |
76
|
|
|
}); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
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.