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

AbstractVotePlugin::getCurrentVote()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace eXpansion\Bundle\VoteManager\Plugins\Votes;
4
5
use eXpansion\Bundle\VoteManager\Structures\Vote;
6
use eXpansion\Framework\Core\Services\Application\Dispatcher;
7
use eXpansion\Framework\Core\Storage\Data\Player;
8
use eXpansion\Framework\Core\Storage\PlayerStorage;
9
10
/**
11
 * Class AbstractVotePlugin
12
 *
13
 * @author    de Cramer Oliver<[email protected]>
14
 * @copyright 2017 eXpansion
15
 * @package eXpansion\Bundle\VoteManager\Plugins\Votes
16
 */
17
abstract class AbstractVotePlugin
18
{
19
    /** @var PlayerStorage */
20
    protected $playerStorage;
21
22
    /** @var int */
23
    protected $duration;
24
25
    /** @var float */
26
    protected $ratio;
27
28
    /** @var Vote|null */
29
    protected $currentVote = null;
30
    /**
31
     * @var Dispatcher
32
     */
33
    private $dispatcher;
34
35
    /**
36
     * AbstractVotePlugin constructor.
37
     *
38
     * @param Dispatcher    $dispatcher
39
     * @param PlayerStorage $playerStorage
40
     * @param int           $duration
41
     * @param float         $ratio
42
     */
43
    public function __construct(
44
        Dispatcher $dispatcher,
45
        PlayerStorage $playerStorage,
46
        int $duration,
47
        float $ratio)
48
    {
49
        $this->playerStorage = $playerStorage;
50
        $this->duration = $duration;
51
        $this->ratio = $ratio;
52
        $this->dispatcher = $dispatcher;
53
    }
54
55
    /**
56
     * Start a new vote.
57
     *
58
     * @param Player $player
59
     *
60
     * @return Vote|null
61
     */
62
    public function start(Player $player, $params)
63
    {
64
        $this->currentVote = new Vote($player, $this->getCode(), $params);
65
        return $this->currentVote;
66
    }
67
68
    /**
69
     * Reset current vote session.
70
     */
71
    public function reset()
72
    {
73
        $this->currentVote = null;
74
    }
75
76
    /**
77
     * User votes yes
78
     *
79
     * @param string $login
80
     */
81 View Code Duplication
    public function castYes($login)
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...
82
    {
83
        if ($this->currentVote) {
84
            $player = $this->playerStorage->getPlayerInfo($login);
85
            $this->currentVote->castYes($login);
86
            $this->dispatcher->dispatch("votemanager.vote.yes", [$player, $this->currentVote]);
87
        }
88
    }
89
90
    /**
91
     * User votes no
92
     *
93
     * @param string $login
94
     */
95 View Code Duplication
    public function castNo($login)
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...
96
    {
97
        if ($this->currentVote) {
98
            $player = $this->playerStorage->getPlayerInfo($login);
99
            $this->currentVote->castNo($login);
100
            $this->dispatcher->dispatch("votemanager.vote.no", [$player, $this->currentVote]);
101
        }
102
    }
103
104
    /**
105
     * Cancel ongoing vote.
106
     */
107
    public function cancel()
108
    {
109
        if (!$this->currentVote) {
110
            return;
111
        }
112
113
        $this->currentVote->setStatus(Vote::STATUS_CANCEL);
114
    }
115
116
    /**
117
     * Update the status of the vote, and execute actions if vote passed.
118
     *
119
     * @param int $time
120
     */
121
    public function update($time = null)
122
    {
123
        if (!$this->currentVote || $this->currentVote->getStatus() == Vote::STATUS_CANCEL) {
124
            return;
125
        }
126
127
        if (is_null($time)) {
128
            $time = time();
129
        }
130
131
        $playerCount = count($this->playerStorage->getOnline());
132
133
        // Check if vote passes when we suppose that all palyers that didn't vote would vote NO.
134 View Code Duplication
        if ($playerCount > 0 && ($this->currentVote->getYes() / $playerCount) > $this->ratio) {
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...
135
            $this->votePassed();
136
            return;
137
        }
138
139
        // If the vote is still not decided wait for the end to decide.
140
        if (($time - $this->currentVote->getStartTime()) > $this->duration) {
141
            $totalVotes = $this->currentVote->getYes() + $this->currentVote->getNo() * 1.0;
142
143 View Code Duplication
            if ($totalVotes >= 1 && ($this->currentVote->getYes() / $totalVotes) > $this->ratio) {
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...
144
                $this->votePassed();
145
            } else {
146
                $this->voteFailed();
147
            }
148
        }
149
    }
150
151
    /**
152
     * @return Vote|null
153
     */
154
    public function getCurrentVote()
155
    {
156
        return $this->currentVote;
157
    }
158
159
    /**
160
     *  Pass a vote
161
     */
162
    public function pass()
163
    {
164
        $this->currentVote->setStatus(Vote::STATUS_PASSED);
165
        $this->executeVotePassed();
166
    }
167
168
    /**
169
     * Called when a vote passed.
170
     */
171
    protected function votePassed()
172
    {
173
        $this->currentVote->setStatus(Vote::STATUS_PASSED);
174
        $this->executeVotePassed();
175
    }
176
177
    /**
178
     * Called when a vote failed.
179
     */
180
    protected function voteFailed()
181
    {
182
        $this->currentVote->setStatus(Vote::STATUS_FAILED);
183
        $this->executeVoteFailed();
184
    }
185
186
    /**
187
     * @return int
188
     */
189
    public function getDuration(): int
190
    {
191
        return $this->duration;
192
    }
193
194
    /**
195
     * @return float
196
     */
197
    public function getRatio(): float
198
    {
199
        return $this->ratio;
200
    }
201
202
    /**
203
     * @return int
204
     */
205
    public function getElapsedTime() : int
206
    {
207
        return time() - $this->currentVote->getStartTime();
208
    }
209
210
    /**
211
     * Get question text to display for this vote.
212
     *
213
     * @return string
214
     */
215
    abstract public function getQuestion(): string;
216
217
    /**
218
     * Get type code of this vote.
219
     *
220
     * @return string
221
     */
222
    abstract public function getCode(): string;
223
224
    /**
225
     * Get native votes this votes replaces.
226
     *
227
     * @return string[]
228
     */
229
    abstract public function getReplacementTypes(): array;
230
231
    /**
232
     * Called when vote is passed.
233
     *
234
     * @return void
235
     */
236
    abstract protected function executeVotePassed();
237
238
    /**
239
     * Called when vote is failed.
240
     */
241
    abstract protected function executeVoteFailed();
242
}
243