Completed
Pull Request — master (#164)
by
unknown
02:49
created

JukeboxService   B

Complexity

Total Complexity 38

Size/Duplication

Total Lines 252
Duplicated Lines 35.71 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 38
lcom 1
cbo 4
dl 90
loc 252
ccs 0
cts 84
cp 0
rs 8.3999
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getMapQueue() 0 4 1
A getFirst() 0 6 1
C addMap() 34 34 7
C addMapFirst() 34 34 7
B removeMap() 0 25 6
A add() 0 4 1
A addFirst() 0 4 1
A getMap() 0 10 3
A remove() 12 12 3
A checkLogin() 0 10 3
A checkMap() 10 10 3
A clearMapQueue() 0 4 1

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
namespace eXpansion\Bundle\Maps\Services;
4
5
use eXpansion\Bundle\Maps\Structure\JukeboxMap;
6
use eXpansion\Framework\AdminGroups\Helpers\AdminGroups;
7
use eXpansion\Framework\Core\Storage\PlayerStorage;
8
use Maniaplanet\DedicatedServer\Structures\Map;
9
10
class JukeboxService
11
{
12
    /** @var JukeboxMap[] */
13
    protected $mapQueue = [];
14
15
    /**
16
     * @var PlayerStorage
17
     */
18
    private $playerStorage;
19
    /**
20
     * @var AdminGroups
21
     */
22
    private $adminGroups;
23
24
    /**
25
     * JukeboxService constructor.
26
     * @param PlayerStorage $playerStorage
27
     * @param AdminGroups $adminGroups
28
     */
29
    public function __construct(PlayerStorage $playerStorage, AdminGroups $adminGroups)
30
    {
31
        $this->playerStorage = $playerStorage;
32
        $this->adminGroups = $adminGroups;
33
    }
34
35
    /**
36
     * @return JukeboxMap[]
37
     */
38
    public function getMapQueue()
39
    {
40
        return $this->mapQueue;
41
    }
42
43
    /**
44
     * @return JukeboxMap|null
45
     */
46
    public function getFirst()
47
    {
48
        reset($this->mapQueue);
49
50
        return current($this->mapQueue);
51
    }
52
53
    /**
54
     * Adds map as last item
55
     * @param Map $map
56
     * @param null $login
57
     * @param bool $force
58
     * @return bool
59
     */
60
61 View Code Duplication
    public function addMap(Map $map, $login = null, $force = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
62
    {
63
        $player = null;
0 ignored issues
show
Unused Code introduced by
$player 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...
64
        if (!$login) {
65
            return false;
66
        }
67
68
        $player = $this->playerStorage->getPlayerInfo($login);
69
        $jbMap = new JukeboxMap($map, $player);
70
71
        if ($force) {
72
            $this->add($jbMap);
73
74
            return true;
75
        }
76
        // no some restrictions for admin
77
        if ($this->adminGroups->hasPermission($login, "jukebox")) {
78
            if ($this->checkMap($map) === false) {
79
                $this->add($jbMap);
80
81
                return true;
82
            }
83
84
        } else {
85
            // restrict 1 map per 1 login
86
            if ($this->checkLogin($login) === false && $this->checkMap($map) === false) {
87
                $this->add($jbMap);
88
89
                return true;
90
            }
91
        }
92
93
        return false;
94
    }
95
96
    /**
97
     * Adds Map as first item
98
     * @param Map $map
99
     * @param null $login
100
     * @param bool $force
101
     * @return bool
102
     */
103
104 View Code Duplication
    public function addMapFirst(Map $map, $login = null, $force = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
105
    {
106
        $player = null;
0 ignored issues
show
Unused Code introduced by
$player 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...
107
        if (!$login) {
108
            return false;
109
        }
110
111
        $player = $this->playerStorage->getPlayerInfo($login);
112
        $jbMap = new JukeboxMap($map, $player);
113
114
        if ($force) {
115
            $this->addFirst($jbMap);
116
117
            return true;
118
        }
119
        // no some restrictions for admin
120
        if ($this->adminGroups->hasPermission($login, "jukebox")) {
121
            if ($this->checkMap($map) === false) {
122
                $this->add($jbMap);
123
124
                return true;
125
            }
126
127
        } else {
128
            // restrict 1 map per 1 login
129
            if ($this->checkLogin($login) === false && $this->checkMap($map) === false) {
130
                $this->add($jbMap);
131
132
                return true;
133
            }
134
        }
135
136
        return false;
137
    }
138
139
    /**
140
     * @param Map $map
141
     * @param $login
142
     * @param bool $force
143
     * @return false;
0 ignored issues
show
Documentation introduced by
The doc-type false; could not be parsed: Expected "|" or "end of type", but got ";" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
144
     */
145
    public function removeMap(Map $map, $login = null, $force = false)
146
    {
147
        if ($force) {
148
            return $this->remove($map);
149
        }
150
151
        if (!$login) {
152
153
            return false;
154
        }
155
        // no some restrictions for admin
156
        if ($this->adminGroups->hasPermission($login, "jukebox")) {
157
            return $this->remove($map);
158
        } else {
159
            // restrict 1 map per 1 login
160
            $check = $this->getMap($login);
161
            if ($check && $check->getMap() === $map) {
162
                return $this->remove($map);
163
164
165
            }
166
        }
167
168
        return false;
169
    }
170
171
    /**
172
     * @param JukeboxMap $map
173
     */
174
    private function add(JukeboxMap $map)
175
    {
176
        array_push($this->mapQueue, $map);
177
    }
178
179
    /**
180
     * @param JukeboxMap $map
181
     */
182
    private function addFirst(JukeboxMap $map)
183
    {
184
        array_unshift($this->mapQueue, $map);
185
    }
186
187
    /**
188
     * @param $login
189
     * @return bool|JukeboxMap
190
     */
191
    public function getMap($login)
192
    {
193
        foreach ($this->mapQueue as $jukeboxMap) {
194
            if ($jukeboxMap->getPlayer()->getLogin() == $login) {
195
                return $jukeboxMap;
196
            }
197
        }
198
199
        return false;
200
    }
201
202
    /**
203
     * @param Map $map
204
     *
205
     * @return bool
206
     */
207 View Code Duplication
    private function remove(Map $map)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
208
    {
209
        foreach ($this->mapQueue as $idx => $jukeboxMap) {
210
            if ($jukeboxMap->getMap() === $map) {
211
                unset($this->mapQueue[$idx]);
212
213
                return true;
214
            }
215
        }
216
217
        return false;
218
    }
219
220
221
    /**
222
     * checks if login exists on queue
223
     * @param string $login
224
     * @return bool
225
     */
226
    private function checkLogin($login)
227
    {
228
        foreach ($this->mapQueue as $jukeboxMap) {
229
            if ($jukeboxMap->getPlayer()->getLogin() == $login) {
230
                return true;
231
            }
232
        }
233
234
        return false;
235
    }
236
237
    /**
238
     * checks if map exists on queue
239
     * @param Map $map
240
     * @return bool
241
     */
242 View Code Duplication
    private function checkMap(Map $map)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
243
    {
244
        foreach ($this->mapQueue as $jukeboxMap) {
245
            if ($jukeboxMap->getMap() === $map) {
246
                return true;
247
            }
248
        }
249
250
        return false;
251
    }
252
253
    /**
254
     *
255
     */
256
    public function clearMapQueue()
257
    {
258
        $this->mapQueue = [];
259
    }
260
261
}
262