Completed
Pull Request — master (#175)
by
unknown
05:30
created

AbstractVotePlugin::pass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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