Completed
Push — fm-support ( 0de470...22a3ee )
by Konstantinos
04:53
created

GroupTeamEvents   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 25 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 0
cbo 2
dl 17
loc 68
ccs 5
cts 5
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B up() 0 26 2
B down() 17 25 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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) {
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...
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) {
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...
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