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

Vote::setStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace eXpansion\Bundle\VoteManager\Structures;
4
5
use eXpansion\Framework\Core\Storage\Data\Player;
6
7
class Vote
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 */
24
    protected $startTime = 0;
25
26
    /** @var array */
27
    protected $params;
28
29
    /** @var array */
30
    protected $votes = [];
31
32
    /** @var int */
33
    protected $status = self::STATUS_RUNNING;
34
35
    /**
36
     * Vote constructor.
37
     *
38
     * @param Player $player
39
     * @param string $type
40
     * @param array $params
41
     */
42 23
    public function __construct(Player $player, $type, $params = [])
43
    {
44 23
        $this->startTime = time();
45 23
        $this->type = $type;
46 23
        $this->player = $player;
47 23
        $this->params = $params;
48 23
        $this->votes = [];
49 23
    }
50
51
    /**
52
     * User votes yes
53
     *
54
     * @param string $login
55
     */
56 6
    public function castYes($login)
57
    {
58 6
        $this->votes[$login] = self::VOTE_YES;
59 6
    }
60
61
    /**
62
     * User votes no
63
     *
64
     * @param string $login
65
     */
66 4
    public function castNo($login)
67
    {
68 4
        $this->votes[$login] = self::VOTE_NO;
69 4
    }
70
71
    /**
72
     * Get number of yes votes.
73
     *
74
     * @return int
75
     */
76 7
    public function getYes()
77
    {
78 7
        return $this->countVote(self::VOTE_YES);
79
    }
80
81
    /**
82
     * Get number of no votes.
83
     *
84
     * @return int
85
     */
86 4
    public function getNo()
87
    {
88 4
        return $this->countVote(self::VOTE_NO);
89
    }
90
91
    /**
92
     * Count votes of a certain type.
93
     *
94
     * @param string $toCount
95
     *
96
     * @return int
97
     */
98 7
    protected function countVote($toCount)
99
    {
100 7
        $value = 0;
101 7
        foreach ($this->votes as $login => $vote) {
102 7
            if ($vote === $toCount) {
103 7
                $value++;
104
            }
105
        }
106
107 7
        return $value;
108
    }
109
110
    /**
111
     * Get timestamp at which votes started.
112
     *
113
     * @return int
114
     */
115 6
    public function getStartTime(): int
116
    {
117 6
        return $this->startTime;
118
    }
119
120
    /**
121
     * Get player that started the vote.
122
     *
123
     * @return Player
124
     */
125 5
    public function getPlayer(): Player
126
    {
127 5
        return $this->player;
128
    }
129
130
    /**
131
     * @return string
132
     */
133 4
    public function getType(): string
134
    {
135 4
        return $this->type;
136
    }
137
138
    /**
139
     * @return int
140
     */
141 13
    public function getStatus(): int
142
    {
143 13
        return $this->status;
144
    }
145
146
    /**
147
     * @param int $status
148
     */
149 10
    public function setStatus(int $status)
150
    {
151 10
        $this->status = $status;
152 10
    }
153
154
    /**
155
     * @return array
156
     */
157 1
    public function getParams(): array
158
    {
159 1
        return $this->params;
160
    }
161
}
162