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

AbstractVote::getElapsedTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace eXpansion\Bundle\VoteManager\Structures;
4
5
use eXpansion\Framework\Core\Storage\Data\Player;
6
7
abstract class AbstractVote
8
{
9
    const VOTE_YES = "yes";
10
    const VOTE_NO = "no";
11
12
    const STATUS_FAILED = -1;
13
    const STATUS_CANCEL = 0;
14
    const STATUS_RUNNING = 1;
15
    const STATUS_PASSED = 2;
16
17
    /** @var Player Player that started the vote */
18
    private $player;
19
20
    /** @var string */
21
    private $type;
22
23
    /** @var int Time the vote will take */
24
    protected $totalTime = 30;
25
26
    /** @var float Ration for the vote to pass. */
27
    protected $ratio = 0.57;
28
29
    /** @var int Current status of the vote. */
30
    protected $status = 1;
31
32
    /** @var int Time elapsed since vote started */
33
    protected $elapsedTime = 0;
34
35
    /** @var int */
36
    protected $startTime = 0;
37
38
    /** @var array */
39
    protected $votes = [];
40
41
42
43
    /**
44
     * Vote constructor.
45
     *
46
     * @param Player $player
47
     * @param string $type
48
     * @param int $duration
49
     * @param float $ration
50
     */
51 9
    public function __construct(Player $player, $type, $duration = 30, $ration = 0.57)
52
    {
53 9
        $this->startTime = time();
54 9
        $this->totalTime = $duration;
55 9
        $this->type = $type;
56 9
        $this->ratio = $ration;
57 9
        $this->player = $player;
58 9
    }
59
60
    /**
61
     * User votes yes
62
     *
63
     * @param string $login
64
     */
65 1
    public function castYes($login)
66
    {
67 1
        $this->votes[$login] = self::VOTE_YES;
68 1
    }
69
70
    /**
71
     * User votes no
72
     *
73
     * @param string $login
74
     */
75 2
    public function castNo($login)
76
    {
77 2
        $this->votes[$login] = self::VOTE_NO;
78 2
    }
79
80
    /**
81
     * Get number of yes votes.
82
     *
83
     * @return int
84
     */
85 3
    public function getYes()
86
    {
87 3
        return $this->countVote(self::VOTE_YES);
88
    }
89
90
    /**
91
     * Get number of no votes.
92
     *
93
     * @return int
94
     */
95 3
    public function getNo()
96
    {
97 3
        return $this->countVote(self::VOTE_NO);
98
    }
99
100
    /**
101
     * Count votes of a certain type.
102
     *
103
     * @param string $toCount
104
     *
105
     * @return int
106
     */
107 3
    protected function countVote($toCount)
108
    {
109 3
        $value = 0;
110 3
        foreach ($this->votes as $login => $vote) {
111 2
            if ($vote === $toCount) {
112 2
                $value++;
113
            }
114
        }
115
116 3
        return $value;
117
    }
118
119
    /**
120
     * Update status of the vite and change status if needed.
121
     *
122
     * @param $time
123
     */
124 3
    function updateVote($time)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
125
    {
126 3
        $this->elapsedTime = $time - $this->startTime;
0 ignored issues
show
Documentation Bug introduced by
It seems like $time - $this->startTime can also be of type double. However, the property $elapsedTime is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
127 3
        $total = $this->getYes() + $this->getNo();
128
129 3
        if ($this->elapsedTime >= $this->totalTime) {
130 3
            if ($total > 0) {
131 2
                if (($this->getYes() / $total) > $this->ratio) {
132 1
                    $this->status = self::STATUS_PASSED;
133
134 1
                    return;
135
                }
136
            }
137 2
            $this->status = self::STATUS_FAILED;
138
139 2
            return;
140
        }
141
    }
142
143
144
    /**
145
     * Get timestamp at which votes started.
146
     *
147
     * @return int
148
     */
149 2
    public function getStartTime(): int
150
    {
151 2
        return $this->startTime;
152
    }
153
154
    /**
155
     * Get duration of the votes.
156
     *
157
     * @return float
158
     */
159 1
    public function getTotalTime(): int
160
    {
161 1
        return $this->totalTime;
162
    }
163
164
    /**
165
     * Get time elapsed since vote started.
166
     *
167
     * @return int
168
     */
169 1
    public function getElapsedTime(): int
170
    {
171 1
        return $this->elapsedTime;
172
    }
173
174
    /**
175
     * Get ration to pass vote.
176
     *
177
     * @return float
178
     */
179 1
    public function getRatio(): float
180
    {
181 1
        return $this->ratio;
182
    }
183
184
    /**
185
     * Get player that started the vote.
186
     *
187
     * @return Player
188
     */
189 4
    public function getPlayer(): Player
190
    {
191 4
        return $this->player;
192
    }
193
194
    /**
195
     * Get current status of the vote.
196
     *
197
     * @return int
198
     */
199 3
    public function getStatus(): int
200
    {
201 3
        return $this->status;
202
    }
203
204
    /**
205
     * @return string
206
     */
207 1
    public function getType(): string
208
    {
209 1
        return $this->type;
210
    }
211
212
    public abstract function getQuestion() : string;
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
213
214
    public abstract function executeVotePassed();
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
215
216
    public abstract function executeVoteFailed();
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
217
}
218