Completed
Pull Request — master (#307)
by
unknown
03:53
created

VoteManager::onPreLoop()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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