JukeboxService::getMapQueue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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
     *
27
     * @param PlayerStorage $playerStorage
28
     * @param AdminGroups   $adminGroups
29
     */
30
    public function __construct(PlayerStorage $playerStorage, AdminGroups $adminGroups)
31
    {
32
        $this->playerStorage = $playerStorage;
33
        $this->adminGroups = $adminGroups;
34
    }
35
36
    /**
37
     * @return JukeboxMap[]
38
     */
39
    public function getMapQueue()
40
    {
41
        return $this->mapQueue;
42
    }
43
44
    /**
45
     * @return JukeboxMap|null
46
     */
47
    public function getFirst()
48
    {
49
        reset($this->mapQueue);
50
51
        return current($this->mapQueue);
52
    }
53
54
    /**
55
     * Adds map as first item
56
     *
57
     * @param Map  $map
58
     * @param null $login
59
     * @param bool $force
60
     *
61
     * @return bool
62
     */
63
    public function addMapFirst(Map $map, $login = null, $force = false)
64
    {
65
        return $this->addMap($map, $login, $force, true);
66
    }
67
68
    /**
69
     * Adds map as last item
70
     *
71
     * @param Map  $map
72
     * @param null $login
73
     * @param bool $force
74
     *
75
     * @return bool
76
     */
77
    public function addMapLast(Map $map, $login = null, $force = false)
78
    {
79
        return $this->addMap($map, $login, $force, false);
80
    }
81
82
    /**
83
     * Adds map as last or first item
84
     *
85
     * @param Map  $map
86
     * @param null $login
87
     * @param bool $force
88
     * @param bool $addFirst
89
     *
90
     * @return bool
91
     */
92
    public function addMap(Map $map, $login = null, $force = false, $addFirst = false)
93
    {
94
95
        if (!$login) {
96
            return false;
97
        }
98
99
        $player = $this->playerStorage->getPlayerInfo($login);
100
        $jbMap = new JukeboxMap($map, $player);
101
102
        if ($force) {
103
            $this->add($jbMap, $addFirst);
104
105
            return true;
106
        }
107
        // no some restrictions for admin
108
        if ($this->adminGroups->hasPermission($login, "jukebox")) {
109
            if ($this->checkMap($map) === false) {
110
                $this->add($jbMap, $addFirst);
111
112
                return true;
113
            }
114
115
        } else {
116
            // restrict 1 map per 1 login
117
            if ($this->checkLogin($login) === false && $this->checkMap($map) === false) {
118
                $this->add($jbMap, $addFirst);
119
120
                return true;
121
            }
122
        }
123
124
        return false;
125
    }
126
127
    /**
128
     * @param Map  $map
129
     * @param      $login
130
     * @param bool $force
131
     *
132
     * @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...
133
     */
134
    public function removeMap(Map $map, $login = null, $force = false)
135
    {
136
        if ($force) {
137
            return $this->remove($map);
138
        }
139
140
        if (!$login) {
141
142
            return false;
143
        }
144
        // no some restrictions for admin
145
        if ($this->adminGroups->hasPermission($login, "jukebox")) {
146
            return $this->remove($map);
147
        } else {
148
            // restrict 1 map per 1 login
149
            $check = $this->getMap($login);
150
            if ($check && $check->getMap() === $map) {
151
                return $this->remove($map);
152
153
154
            }
155
        }
156
157
        return false;
158
    }
159
160
    /**
161
     * @param JukeboxMap $map
162
     * @param bool       $addFirst
163
     *
164
     * @return void
165
     */
166
    private function add(JukeboxMap $map, $addFirst = false)
167
    {
168
        if ($addFirst) {
169
            array_unshift($this->mapQueue, $map);
170
        } else {
171
            array_push($this->mapQueue, $map);
172
        }
173
174
    }
175
176
    /**
177
     * @param $login
178
     *
179
     * @return bool|JukeboxMap
180
     */
181
    public function getMap($login)
182
    {
183
        foreach ($this->mapQueue as $jukeboxMap) {
184
            if ($jukeboxMap->getPlayer()->getLogin() == $login) {
185
                return $jukeboxMap;
186
            }
187
        }
188
189
        return false;
190
    }
191
192
    /**
193
     * @param Map $map
194
     *
195
     * @return bool
196
     */
197 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...
198
    {
199
        foreach ($this->mapQueue as $idx => $jukeboxMap) {
200
            if ($jukeboxMap->getMap() === $map) {
201
                unset($this->mapQueue[$idx]);
202
203
                return true;
204
            }
205
        }
206
207
        return false;
208
    }
209
210
211
    /**
212
     * checks if login exists on queue
213
     * @param string $login
214
     * @return bool
215
     */
216
    private function checkLogin($login)
217
    {
218
        foreach ($this->mapQueue as $jukeboxMap) {
219
            if ($jukeboxMap->getPlayer()->getLogin() == $login) {
220
                return true;
221
            }
222
        }
223
224
        return false;
225
    }
226
227
    /**
228
     * checks if map exists on queue
229
     * @param Map $map
230
     *
231
     * @return bool
232
     */
233 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...
234
    {
235
        foreach ($this->mapQueue as $jukeboxMap) {
236
            if ($jukeboxMap->getMap() === $map) {
237
                return true;
238
            }
239
        }
240
241
        return false;
242
    }
243
244
    /**
245
     *
246
     */
247
    public function clearMapQueue()
248
    {
249
        $this->mapQueue = [];
250
    }
251
252
}
253