Completed
Push — master ( 206125...47c10a )
by De Cramer
13s
created

VoteService   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 268
Duplicated Lines 4.48 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 36
c 0
b 0
f 0
lcom 1
cbo 5
dl 12
loc 268
rs 8.8
ccs 0
cts 86
cp 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 3
A onVoteNew() 0 10 3
B onVoteCancelled() 0 10 5
A onVotePassed() 0 6 2
A onVoteFailed() 0 6 2
A onPreLoop() 0 4 1
A onPostLoop() 0 4 1
A updateVote() 0 13 4
B onEverySecond() 12 23 5
A getCurrentVote() 0 4 1
A startVote() 0 19 4
A onStartMapStart() 0 4 1
A onStartMapEnd() 0 4 1
A onEndMapStart() 0 6 2
A onEndMapEnd() 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\VoteManager\Services;
4
5
use eXpansion\Bundle\VoteManager\Services\VoteFactories\AbstractFactory;
6
use eXpansion\Bundle\VoteManager\Structures\AbstractVote;
7
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceExpTimer;
8
use eXpansion\Framework\Core\Helpers\ChatNotification;
9
use eXpansion\Framework\Core\Services\Application\Dispatcher;
10
use eXpansion\Framework\Core\Services\Console;
11
use eXpansion\Framework\Core\Storage\Data\Player;
12
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ListenerInterfaceMpLegacyVote;
13
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ListenerInterfaceMpScriptMap;
14
use Maniaplanet\DedicatedServer\Connection;
15
use Maniaplanet\DedicatedServer\Structures\Map;
16
17
class VoteService implements ListenerInterfaceMpLegacyVote, ListenerInterfaceExpTimer, ListenerInterfaceMpScriptMap
18
{
19
    /** @var Console */
20
    protected $console;
21
22
    /** @var Connection */
23
    protected $connection;
24
25
    /** @var ChatNotification */
26
    protected $chatNotification;
27
28
    /** @var Dispatcher */
29
    protected $dispatcher;
30
31
    /** @var AbstractFactory[] */
32
    protected $voteFactories = [];
33
34
    /** @var array mapping between native MP votes and equivalent expansion votes. */
35
    protected $voteMapping = [];
36
37
    /** @var null|AbstractVote */
38
    protected $currentVote = null;
39
40
    /** @var array */
41
    protected $votesStarted = [];
42
43
44
45
    /**
46
     * VoteManager constructor.
47
     * @param Console $console
48
     * @param Connection $connection
49
     * @param ChatNotification $chatNotification
50
     * @param Dispatcher $dispatcher
51
     * @param AbstractFactory[] $voteFactories
52
     */
53
    public function __construct(
54
        Console $console,
55
        Connection $connection,
56
        ChatNotification $chatNotification,
57
        Dispatcher $dispatcher,
58
        $voteFactories
59
    )
60
    {
61
        $this->console = $console;
62
        $this->connection = $connection;
63
        $this->dispatcher = $dispatcher;
64
        $this->chatNotification = $chatNotification;
65
66
        foreach ($voteFactories as $voteFactory) {
67
            $this->voteFactories[$voteFactory->getVoteCode()] = $voteFactory;
68
69
            foreach ($voteFactory->getReplacementTypes() as $replaces) {
70
                $this->voteMapping[$replaces] = $voteFactory->getVoteCode();
71
            }
72
        }
73
    }
74
75
    /**
76
     * When a new vote is addressed
77
     *
78
     * @param Player $player
79
     * @param string $cmdName
80
     * @param string $cmdValue
81
     *
82
     * @return void
83
     */
84
    public function onVoteNew(Player $player, $cmdName, $cmdValue)
85
    {
86
        if (!($cmdValue instanceof AbstractVote)) {
87
            if (isset($this->voteMapping[$cmdName])) {
88
                // disable default vote
89
                $this->connection->cancelVote();
90
                $this->startVote($player, $this->voteMapping[$cmdName]);
91
            }
92
        }
93
    }
94
95
    /**
96
     * When vote gets cancelled
97
     *
98
     * @param Player $player
99
     * @param string $cmdName
100
     * @param string $cmdValue
101
     *
102
     * @return void
103
     */
104
    public function onVoteCancelled(Player $player, $cmdName, $cmdValue)
105
    {
106
        if ($cmdName == null && $cmdValue == null && $this->currentVote instanceof AbstractVote) {
107
            $this->currentVote->setStatus(Vote::STATUS_CANCEL);
0 ignored issues
show
Bug introduced by
The method setStatus() does not seem to exist on object<eXpansion\Bundle\...tructures\AbstractVote>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
108
        }
109
110
        if ($cmdValue instanceof AbstractVote) {
111
            $this->currentVote = null;
112
        }
113
    }
114
115
    /**
116
     * When vote Passes
117
     * @param Player $player
118
     * @param string $cmdName
119
     * @param string $cmdValue
120
     *
121
     * @return void
122
     */
123
    public function onVotePassed(Player $player, $cmdName, $cmdValue)
124
    {
125
        if ($cmdValue instanceof AbstractVote) {
126
            $this->currentVote = null;
127
        }
128
    }
129
130
    /**
131
     * When vote Fails
132
     * @param Player $player
133
     * @param string $cmdName
134
     * @param string $cmdValue
135
     *
136
     * @return void
137
     */
138
    public function onVoteFailed(Player $player, $cmdName, $cmdValue)
139
    {
140
        if ($cmdValue instanceof AbstractVote) {
141
            $this->currentVote = null;
142
        }
143
    }
144
145
    public function onPreLoop()
146
    {
147
        // do nothing
148
    }
149
150
    public function onPostLoop()
151
    {
152
        //do nothing
153
    }
154
155
    public function updateVote($login, $type)
156
    {
157
        if ($this->currentVote instanceof AbstractVote) {
158
            switch ($type) {
159
                case AbstractVote::VOTE_YES:
160
                    $this->currentVote->castYes($login);
161
                    break;
162
                case AbstractVote::VOTE_NO:
163
                    $this->currentVote->castNo($login);
164
                    break;
165
            }
166
        }
167
    }
168
169
    public function onEverySecond()
170
    {
171
        $vote = $this->currentVote;
172
173
        if ($vote !== null) {
174
            $vote->updateVote(time());
175
176
            switch ($vote->getStatus()) {
177 View Code Duplication
                case AbstractVote::STATUS_CANCEL:
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...
178
                    $this->dispatcher->dispatch("votemanager.votecancelled",
179
                        [$vote->getPlayer(), $vote->getType(), $this->currentVote]);
180
                    break;
181 View Code Duplication
                case AbstractVote::STATUS_FAILED:
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...
182
                    $this->dispatcher->dispatch("votemanager.votefailed",
183
                        [$vote->getPlayer(), $vote->getType(), $this->currentVote]);
184
                    break;
185 View Code Duplication
                case AbstractVote::STATUS_PASSED:
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...
186
                    $this->dispatcher->dispatch("votemanager.votepassed",
187
                        [$vote->getPlayer(), $vote->getType(), $this->currentVote]);
188
                    break;
189
            }
190
        }
191
    }
192
193
    /**
194
     * @return AbstractVote
195
     */
196
    public function getCurrentVote()
197
    {
198
        return $this->currentVote;
199
    }
200
201
202
    public function startVote(Player $player, $type)
203
    {
204
        if ($this->getCurrentVote() !== null) {
205
            $this->chatNotification->sendMessage("expansion_votemanager.error.in_progress");
206
            return;
207
        }
208
209
        if (!isset($this->voteFactories[$type])) {
210
            $this->chatNotification->sendMessage("|error| Unknown vote type : $type");
211
        }
212
213
        if (array_key_exists($type, $this->votesStarted) == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
214
            $this->votesStarted[$type] = "yep";
215
            $this->currentVote = $this->voteFactories[$type]->create($player);
216
            $this->dispatcher->dispatch("votemanager.votenew", [$player, $type, $this->currentVote]);
217
        } else {
218
            $this->chatNotification->sendMessage("expansion_votemanager.error.already_started");
219
        }
220
    }
221
222
223
    /**
224
     * Callback sent when the "StartMap" section start.
225
     *
226
     * @param int $count Each time this section is played, this number is incremented by one
227
     * @param int $time Server time when the callback was sent
228
     * @param boolean $restarted true if the map was restarted, false otherwise
229
     * @param Map $map Map started with.
230
     *
231
     * @return void
232
     */
233
    public function onStartMapStart($count, $time, $restarted, Map $map)
234
    {
235
        $this->votesStarted = [];
236
    }
237
238
    /**
239
     * Callback sent when the "StartMap" section end.
240
     *
241
     * @param int $count Each time this section is played, this number is incremented by one
242
     * @param int $time Server time when the callback was sent
243
     * @param boolean $restarted true if the map was restarted, false otherwise
244
     * @param Map $map Map started with.
245
     *
246
     * @return void
247
     */
248
    public function onStartMapEnd($count, $time, $restarted, Map $map)
249
    {
250
        // Nothing
251
    }
252
253
    /**
254
     * Callback sent when the "EndMap" section start.
255
     *
256
     * @param int $count Each time this section is played, this number is incremented by one
257
     * @param int $time Server time when the callback was sent
258
     * @param boolean $restarted true if the map was restarted, false otherwise
259
     * @param Map $map Map started with.
260
     *
261
     * @return void
262
     */
263
    public function onEndMapStart($count, $time, $restarted, Map $map)
264
    {
265
        if ($this->currentVote instanceof Vote) {
0 ignored issues
show
Bug introduced by
The class eXpansion\Bundle\VoteManager\Services\Vote does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
266
            $this->currentVote->setStatus(Vote::STATUS_CANCEL);
267
        }
268
    }
269
270
    /**
271
     * Callback sent when the "EndMap" section end.
272
     *
273
     * @param int $count Each time this section is played, this number is incremented by one
274
     * @param int $time Server time when the callback was sent
275
     * @param boolean $restarted true if the map was restarted, false otherwise
276
     * @param Map $map Map started with.
277
     *
278
     * @return void
279
     */
280
    public function onEndMapEnd($count, $time, $restarted, Map $map)
281
    {
282
        // Nothing
283
    }
284
}
285