Completed
Pull Request — master (#309)
by De Cramer
14:06
created

VoteManager::onVoteNew()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 3
crap 2
1
<?php
2
3
namespace eXpansion\Bundle\VoteManager\Plugins;
4
5
use eXpansion\Bundle\Maps\Services\JukeboxService;
6
use eXpansion\Bundle\VoteManager\Plugins\Gui\Widget\UpdateVoteWidgetFactory;
7
use eXpansion\Bundle\VoteManager\Plugins\Gui\Widget\VoteWidgetFactory;
8
use eXpansion\Bundle\VoteManager\Plugins\Votes\AbstractVotePlugin;
9
use eXpansion\Bundle\VoteManager\Services\VoteService;
10
use eXpansion\Bundle\VoteManager\Structures\Vote;
11
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceExpTimer;
12
use eXpansion\Framework\Core\Helpers\ChatNotification;
13
use eXpansion\Framework\Core\Model\UserGroups\Group;
14
use eXpansion\Framework\Core\Services\Console;
15
use eXpansion\Framework\Core\Storage\Data\Player;
16
use eXpansion\Framework\Core\Storage\MapStorage;
17
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ListenerInterfaceMpLegacyVote;
18
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ListenerInterfaceMpScriptPodium;
19
use Maniaplanet\DedicatedServer\Connection;
20
21
class VoteManager implements ListenerInterfaceMpLegacyVote, ListenerInterfaceExpTimer
22
{
23
    const YES = "yes";
24
    const NO = "no";
25
26
    /**
27
     * @var VoteWidgetFactory
28
     */
29
    private $voteWidgetFactory;
30
31
    /**
32
     * @var UpdateVoteWidgetFactory
33
     */
34
    private $updateVoteWidgetFactory;
35
36
    /**
37
     * @var Group
38
     */
39
    private $players;
40
41
    /**
42
     * @var VoteService
43
     */
44
    private $voteService;
45
46
    /**
47
     * VoteManager constructor.
48
     *
49
     * @param VoteWidgetFactory $voteWidgetFactory
50
     * @param UpdateVoteWidgetFactory $updateVoteWidgetFactory
51
     * @param Group $players
52
     * @param VoteService $voteService
53
     */
54 7
    public function __construct(
55
        VoteWidgetFactory $voteWidgetFactory,
56
        UpdateVoteWidgetFactory $updateVoteWidgetFactory,
57
        Group $players,
58
        VoteService $voteService
59
    ) {
60 7
        $this->voteWidgetFactory = $voteWidgetFactory;
61 7
        $this->players = $players;
62 7
        $this->voteService = $voteService;
63 7
        $this->updateVoteWidgetFactory = $updateVoteWidgetFactory;
64 7
    }
65
66
    /**
67
     * When a new vote is addressed
68
     *
69
     * @param Player $player
70
     * @param string $cmdName
71
     * @param string $cmdValue
72
     *
73
     * @return void
74
     */
75 2
    public function onVoteNew(Player $player, $cmdName, $cmdValue)
76
    {
77 2
        if ($cmdValue instanceof Vote) {
78 1
            $this->updateVoteWidgetFactory->create($this->players);
79 1
            $this->voteWidgetFactory->create($this->players);
80
        } else {
81 1
            $this->voteService->startVote($player, $cmdName, ['value' => $cmdValue]);
82
        }
83 2
    }
84
85
    /**
86
     * When vote gets cancelled
87
     *
88
     * @param Player $player
89
     * @param string $cmdName
90
     * @param string $cmdValue
91
     *
92
     * @return void
93
     */
94 1
    public function onVoteCancelled(Player $player, $cmdName, $cmdValue)
95
    {
96
97 1
        if ($cmdValue instanceof Vote) {
98 1
            $this->voteWidgetFactory->destroy($this->players);
99 1
            $this->updateVoteWidgetFactory->destroy($this->players);
100
        } else {
101
            $this->voteService->cancel();
102
        }
103 1
    }
104
105
    /**
106
     * When vote Passes
107
     * @param Player $player
108
     * @param string $cmdName
109
     * @param string $cmdValue
110
     *
111
     * @return void
112
     */
113 1
    public function onVotePassed(Player $player, $cmdName, $cmdValue)
114
    {
115 1
        if ($cmdValue instanceof Vote) {
116 1
            $this->voteWidgetFactory->destroy($this->players);
117 1
            $this->updateVoteWidgetFactory->destroy($this->players);
118
        }
119 1
    }
120
121
    /**
122
     * When vote Fails
123
     * @param Player $player
124
     * @param string $cmdName
125
     * @param string $cmdValue
126
     *
127
     * @return void
128
     */
129 1
    public function onVoteFailed(Player $player, $cmdName, $cmdValue)
130
    {
131 1
        if ($cmdValue instanceof Vote) {
132 1
            $this->voteWidgetFactory->destroy($this->players);
133 1
            $this->updateVoteWidgetFactory->destroy($this->players);
134
        }
135 1
    }
136
137 1
    public function onEverySecond()
138
    {
139 1
        if ($this->voteService->getCurrentVote() instanceof AbstractVotePlugin) {
140 1
            $this->voteService->update();
141
        }
142 1
    }
143
144
    /**
145
     * When vote Fails
146
     * @param Player $player
147
     * @param Vote   $vote
148
     * @return void
149
     */
150
    public function onVoteYes(Player $player, $vote)
0 ignored issues
show
Unused Code introduced by
The parameter $player is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
151
    {
152
        if ($this->voteService->getCurrentVote() instanceof AbstractVotePlugin) {
153
            $this->updateVoteWidgetFactory->updateVote($vote);
154
        }
155
    }
156
157
    /**
158
     * When vote Fails
159
     * @param Player $player
160
     * @param Vote   $vote
161
     * @return void
162
     */
163
    public function onVoteNo(Player $player, $vote)
0 ignored issues
show
Unused Code introduced by
The parameter $player is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
164
    {
165
        if ($this->voteService->getCurrentVote() instanceof AbstractVotePlugin) {
166
            $this->updateVoteWidgetFactory->updateVote($vote);
167
        }
168
    }
169
170 1
    public function onPreLoop()
171
    {
172
        // Nothing
173 1
    }
174
175 1
    public function onPostLoop()
176
    {
177
        // Nothing
178 1
    }
179
180
}
181
182