Completed
Branch master (220ce5)
by De Cramer
16:11
created

JukeboxService::addMap()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 0
cts 25
cp 0
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 18
nc 6
nop 3
crap 56
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
     * @param Map $map
55
     * @param null $login
56
     * @param bool $force
57
     * @return bool
58
     * @throws \Exception
59
     */
60
    public function addMap(Map $map, $login = null, $force = false)
61
    {
62
        $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...
63
        if (!$login) {
64
            throw new \Exception("login is mandatory");
65
        }
66
        $player = $this->playerStorage->getPlayerInfo($login);
67
        $jbMap = new JukeboxMap($map, $player);
68
69
        if ($force) {
70
            $this->add($jbMap);
71
72
            return true;
73
        }
74
        // no some restrictions for admin
75
        if ($this->adminGroups->hasPermission($login, "jukebox")) {
76
            if ($this->checkMap($map) === false) {
77
                $this->add($jbMap);
78
79
                return true;
80
            }
81
82
        } else {
83
            // restrict 1 map per 1 login
84
            if ($this->checkLogin($login) === false && $this->checkMap($map) === false) {
85
                $this->add($jbMap);
86
87
                return true;
88
            }
89
        }
90
91
        return false;
92
    }
93
94
    /**
95
     * @param Map $map
96
     * @param $login
97
     * @param bool $force
98
     * @return bool
99
     * @throws \Exception
100
     */
101
    public function removeMap(Map $map, $login = null, $force = false)
102
    {
103
        if ($force) {
104
            return $this->remove($map);
105
        }
106
107
        if (!$login) {
108
            throw new \Exception("login is mandatory, when not forced.");
109
        }
110
        // no some restrictions for admin
111
        if ($this->adminGroups->hasPermission($login, "jukebox")) {
112
            return $this->remove($map);
113
        } else {
114
            // restrict 1 map per 1 login
115
            $check = $this->getMap($login);
116
            if ($check && $check->getMap() === $map) {
117
                return $this->remove($map);
118
119
120
            }
121
        }
122
123
        return false;
124
    }
125
126
    /**
127
     * @param JukeboxMap $map
128
     */
129
    private function add(JukeboxMap $map)
130
    {
131
        array_push($this->mapQueue, $map);
132
    }
133
134
    /**
135
     * @param $login
136
     * @return bool|JukeboxMap
137
     */
138
    public function getMap($login)
139
    {
140
        foreach ($this->mapQueue as $jukeboxMap) {
141
            if ($jukeboxMap->getPlayer()->getLogin() == $login) {
142
                return $jukeboxMap;
143
            }
144
        }
145
146
        return false;
147
    }
148
149
    /**
150
     * @param Map $map
151
     *
152
     * @return bool
153
     */
154 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...
155
    {
156
        foreach ($this->mapQueue as $idx => $jukeboxMap) {
157
            if ($jukeboxMap->getMap() === $map) {
158
                unset($this->mapQueue[$idx]);
159
160
                return true;
161
            }
162
        }
163
164
        return false;
165
    }
166
167
168
    /**
169
     * checks if login exists on queue
170
     * @param string $login
171
     * @return bool
172
     */
173
    private function checkLogin($login)
174
    {
175
        foreach ($this->mapQueue as $jukeboxMap) {
176
            if ($jukeboxMap->getPlayer()->getLogin() == $login) {
177
                return true;
178
            }
179
        }
180
181
        return false;
182
    }
183
184
    /**
185
     * checks if map exists on queue
186
     * @param Map $map
187
     * @return bool
188
     */
189 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...
190
    {
191
        foreach ($this->mapQueue as $jukeboxMap) {
192
            if ($jukeboxMap->getMap() === $map) {
193
                return true;
194
            }
195
        }
196
197
        return false;
198
    }
199
200
    /**
201
     *
202
     */
203
    public function clearMapQueue()
204
    {
205
        $this->mapQueue = [];
206
    }
207
208
}
209